8.10 Play a WAV or an MP3 File


Problem

You need to play a WAV or MP3 audio file.

Solution

Use the unmanaged sndPlaySound API function for basic WAV file support, or use the ActiveMovie COM component included with Windows Media Player, which supports WAV and MP3 audio.

Discussion

To play any sound in a .NET application, you need to enlist the help of an outside library or system call. Fortunately, two easy options are readily available.

  • The winmm.dll included with Windows includes a function named sndPlaySound that accepts the name of a WAV file and a parameter indicating how to play it. You can choose to play a sound synchronously (interrupting the execution of the program until the sound is complete), asynchronously, or in a continuous background loop.

  • The Quartz type library provides a COM component that can play various types of audio files, including the WAV and MP3 formats. The Quartz type library is provided through quartz.dll and is included as a part of Microsoft DirectX with Windows Media Player and the Windows operating system.

In this example, we'll use the latter approach. The first step is to generate an interop class that can manage the interaction between your .NET application and the unmanaged Quartz library. You can generate a C# class with this interop code using the Type Library Importer utility (tlbimp.exe) and the following command line, where [WindowsDir] is the path for your installation of Windows:

 tlbimp [WindowsDir]\system32\quartz.dll /out:QuartzTypeLib.dll 

Alternatively, you can generate the interop class using Visual Studio .NET by adding a reference. Simply right-click your project in the Solution Explorer, and choose Add Reference from the context menu. Then select the COM tab, and scroll down to select ActiveMovie control type library.

Once the interop class is generated, you can work with the IMediaControl interface. You can specify the file you want to play using RenderFile , and you can control playback using methods such as Run , Stop , and Pause . The actual playback takes place on a separate thread, so it won't block your code.

The following example shows a simple Console utility that plays the audio file that's specified as the first command-line argument.

 using System; class PlayAudio {     public static void Main(string[] args) {              // Get the filename specified in the first parameter.         string filename = args[0];               // Access the IMediaControl interface.         QuartzTypeLib.FilgraphManager graphManager =            new QuartzTypeLib.FilgraphManager();         QuartzTypeLib.IMediaControl mc =           (QuartzTypeLib.IMediaControl)graphManager;         // Specify the file.         mc.RenderFile(filename);         // Start playing the audio asynchronously.         mc.Run();         Console.WriteLine("Press Enter to continue.");          Console.ReadLine();         mc.Stop();     } } 

You can also use the Quartz library to show movie files, as demonstrated in recipe 8.11.




C# Programmer[ap]s Cookbook
C# Programmer[ap]s Cookbook
ISBN: 735619301
EAN: N/A
Year: 2006
Pages: 266

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net