Subscribe

RSS Feed (xml)

Playing WAV or MP3

To play a WAV or MP3 audio file.

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 next post.

Technorati :

1 comment:

  1. Lots of informative tutorials. This is, in my opinion, a very helpful tutorial. Anyone can easily gain complete knowledge about wav to mp3 through this blog post. JavaScript needs a lot of work right now.

    ReplyDelete

Archives

LocalsAdda.com-Variety In Web World

Fun Mail - Fun in the Mail