Updated: October 2008
Controls playback of a sound from a .wav file.
Assembly: System (in System.dll)
The HostProtectionAttribute attribute applied to this type or member has the following Resources property value: UI. The HostProtectionAttribute does not affect desktop applications (which are typically started by double-clicking an icon, typing a command, or entering a URL in a browser). For more information, see the HostProtectionAttribute class or SQL Server Programming and Host Protection Attributes.
The class provides a simple interface for loading and playing a .wav file. The class supports loading a .wav file from a file path, a URL, a Stream that contains a .wav file, or an embedded resource that contains a .wav file.
To play a sound using the class, configure a 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 SoundLocation or 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 configured to load a .wav file from a 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 has finished loading a .wav file, it raises the LoadCompleted event. You can examine the AsyncCompletedEventArgs in your event handler to determine if the load succeeded or failed. The SoundLocationChanged event is raised when the audio source is set to a new file path or URL. The StreamChanged event is raised when the audio source is set to a new Stream. For more information about handling events, see Consuming Events.
For more information about , see SoundPlayer Class Overview.
The 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 Using the Windows Media Player Control in a .NET Framework Solution and Windows Media Player Object Model Reference for Visual Basic .NET and C# in the Windows Media Player SDK.
Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC
The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.