System.Media.SoundPlayer Class

Controls playback of a sound from a .wav file.

See Also: SoundPlayer Members

Syntax

[System.ComponentModel.ToolboxItem(false)]
public class SoundPlayer : System.ComponentModel.Component, System.Runtime.Serialization.ISerializable

Remarks

The System.Media.SoundPlayer class provides a simple interface for loading and playing a .wav file. The System.Media.SoundPlayer class supports loading a .wav file from a file path, a URL, a System.IO.Stream that contains a .wav file, or an embedded resource that contains a .wav file.

To play a sound using the System.Media.SoundPlayer class, configure a System.Media.SoundPlayer with a path to the .wav file and call one of the play methods. You can identify the .wav file to play by using one of the constructors or by setting either the SoundPlayer.SoundLocation or SoundPlayer.Stream property. The file can be loaded prior to playing by using one of the load methods, or loading can be deferred until one of the play methods is called. A System.Media.SoundPlayer configured to load a .wav file from a System.IO.Stream or URL must load the .wav file into memory before playback begins.

You can load or play a .wav file synchronously or asynchronously. If you call a synchronous load or play method, the calling thread will wait until the method returns, which may cause painting and other events to be interrupted. Calling an asynchronous load or play method will allow the calling thread to continue without interruption. For more information on asynchronous method calls, see How to: Run an Operation in the Background.

When a System.Media.SoundPlayer has finished loading a .wav file, it raises the SoundPlayer.LoadCompleted event. You can examine the System.ComponentModel.AsyncCompletedEventArgs in your event handler to determine if the load succeeded or failed. The SoundPlayer.SoundLocationChanged event is raised when the audio source is set to a new file path or URL. The SoundPlayer.StreamChanged event is raised when the audio source is set to a new System.IO.Stream. For more information about handling events, see Consuming Events.

For more information about System.Media.SoundPlayer, see SoundPlayer Class Overview (Windows Forms).

Note:

   The System.Media.SoundPlayer class cannot play other file types, such as .wma or .mp3. If you want to play other file types, you can use the Windows Media Player control. For more information, see tp://go.microsoft.com/fwlink/?LinkId=131267 and tp://go.microsoft.com/fwlink/?LinkId=131269 in the Windows Media Player SDK.

Requirements

Namespace: System.Media
Assembly: System (in System.dll)
Assembly Versions: 2.0.0.0, 4.0.0.0