Section 3.8. Play Simple WAV Audio


3.8. Play Simple WAV Audio

Neither .NET 1.0 or .NET 1.1 provided a managed way to play audio. This shortcoming is finally addressed in .NET 2.0 with the new SoundPlayer class, which allows you to play audio synchronously or asynchronously.


Note: Using the SoundPlayer class, you can play WAV files without diving into the Windows API.

3.8.1. How do I do that?

You can instantiate a SoundPlayer object programmatically, or you can add one to the component tray by dragging it from the toolbox at design time. Once you've created the SoundPlayer, you need to point it to the sound content you want to play. You do this by setting one of two properties:


SoundLocation

If you have a file path or URL that points to a WAV file, specify this information in the SoundLocation property.


Stream

If you have a Stream-based object that contains WAV audio content, use the Stream property.

Once you've set the Stream or SoundLocation property, you need to tell SoundPlayer to actually load the audio data by calling the Load( ) or LoadAsync( ) method. The Load( ) method pauses your code until all the audio is loaded into memory. On the other hand, LoadAsync( ) carries out its work on another thread and fires the LoadCompleted event once it's finished and the audio's available. Usually, you'll use Load() unless you have an extremely large audio file or it takes a long time to read the whole audio file (for example, when retrieving the audio over a slow network or Internet connection).

Finally, once the audio is available, you can call one of the following methods:


PlaySync( )

Pauses your code until the audio playback is finished.


Play( )

Plays the audio on another thread, allowing your code to continue with other tasks and making sure that your application's interface remains responsive.


PlayLooping( )

Similar to Play( ), except that it loops the audio, repeating it continuously.

To halt asynchronous playback at any time, just call Stop( ).

The following code snippet shows an example that plays a sample sound synchronously:

Dim Player As New SoundPlayer( ) Player.SoundLocation = Application.StartupPath & "\mysound.wav" Try     Player.Load( )     Player.PlaySync( ) Catch Err As Exception     ' An error will occur here if the file can't be read     ' or if it has the wrong format. End Try

3.8.2. What about...

...other types of audio? Unfortunately, the SoundPlayer can only play the WAV audio format. If you want to play other types of multimedia, like MP3 or WMA files, you need to use a different solution, and there are no managed classes to help you out.

Two options include:

  • Use COM Interop to access the Quartz library, which is a part of DirectX. The Quartz library allows you to play any file type supported by Windows Media Player, including MP3, WMA, and video formats like MPEG and AVI. For an example in C# code, refer to Microsoft's sample project at http://msdn.microsoft.com/library/en-us/csref/html/vcwlkcominteroppart1cclienttutorial.asp

  • Use the managed DirectX 9.0 libraries. You'll need to install the DirectX client and SDK on the computer for this to work, but it gives you a great deal of power, including the ability to render three-dimensional graphics. See http://msdn.microsoft.com/library/en-us/directx9_m/directx/dx9intro.asp for an introduction.



Visual Basic 2005(c) A Developer's Notebook
Visual Basic 2005: A Developers Notebook
ISBN: 0596007264
EAN: 2147483647
Year: 2006
Pages: 123

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