SoundPlayer Class

 
System_CAPS_noteNote

The .NET API Reference documentation has a new home. Visit the .NET API Browser on docs.microsoft.com to see the new experience.

Controls playback of a sound from a .wav file.

Namespace:   System.Media
Assembly:  System (in System.dll)

System.Object
  System.MarshalByRefObject
    System.ComponentModel.Component
      System.Media.SoundPlayer

[<SerializableAttribute>]
[<HostProtectionAttribute(SecurityAction.LinkDemand, UI = true)>]
type SoundPlayer = 
    class
        inherit Component
        interface ISerializable
    end

NameDescription
System_CAPS_pubmethodSoundPlayer()

Initializes a new instance of the SoundPlayer class.

System_CAPS_protmethodSoundPlayer(SerializationInfo, StreamingContext)

Initializes a new instance of the SoundPlayer class.

System_CAPS_pubmethodSoundPlayer(Stream)

Initializes a new instance of the SoundPlayer class, and attaches the .wav file within the specified Stream.

System_CAPS_pubmethodSoundPlayer(String)

Initializes a new instance of the SoundPlayer class, and attaches the specified .wav file.

NameDescription
System_CAPS_protpropertyCanRaiseEvents

Gets a value indicating whether the component can raise an event.(Inherited from Component.)

System_CAPS_pubpropertyContainer

Gets the IContainer that contains the Component.(Inherited from Component.)

System_CAPS_protpropertyDesignMode

Gets a value that indicates whether the Component is currently in design mode.(Inherited from Component.)

System_CAPS_protpropertyEvents

Gets the list of event handlers that are attached to this Component.(Inherited from Component.)

System_CAPS_pubpropertyIsLoadCompleted

Gets a value indicating whether loading of a .wav file has successfully completed.

System_CAPS_pubpropertyLoadTimeout

Gets or sets the time, in milliseconds, in which the .wav file must load.

System_CAPS_pubpropertySite

Gets or sets the ISite of the Component.(Inherited from Component.)

System_CAPS_pubpropertySoundLocation

Gets or sets the file path or URL of the .wav file to load.

System_CAPS_pubpropertyStream

Gets or sets the Stream from which to load the .wav file.

System_CAPS_pubpropertyTag

Gets or sets the Object that contains data about the SoundPlayer.

NameDescription
System_CAPS_pubmethodCreateObjRef(Type)

Creates an object that contains all the relevant information required to generate a proxy used to communicate with a remote object.(Inherited from MarshalByRefObject.)

System_CAPS_pubmethodDispose()

Releases all resources used by the Component.(Inherited from Component.)

System_CAPS_protmethodDispose(Boolean)

Releases the unmanaged resources used by the Component and optionally releases the managed resources.(Inherited from Component.)

System_CAPS_pubmethodEquals(Object)

Determines whether the specified object is equal to the current object.(Inherited from Object.)

System_CAPS_protmethodFinalize()

Releases unmanaged resources and performs other cleanup operations before the Component is reclaimed by garbage collection.(Inherited from Component.)

System_CAPS_pubmethodGetHashCode()

Serves as the default hash function. (Inherited from Object.)

System_CAPS_pubmethodGetLifetimeService()

Retrieves the current lifetime service object that controls the lifetime policy for this instance.(Inherited from MarshalByRefObject.)

System_CAPS_protmethodGetService(Type)

Returns an object that represents a service provided by the Component or by its Container.(Inherited from Component.)

System_CAPS_pubmethodGetType()

Gets the Type of the current instance.(Inherited from Object.)

System_CAPS_pubmethodInitializeLifetimeService()

Obtains a lifetime service object to control the lifetime policy for this instance.(Inherited from MarshalByRefObject.)

System_CAPS_pubmethodLoad()

Loads a sound synchronously.

System_CAPS_pubmethodLoadAsync()

Loads a .wav file from a stream or a Web resource using a new thread.

System_CAPS_protmethodMemberwiseClone()

Creates a shallow copy of the current Object.(Inherited from Object.)

System_CAPS_protmethodMemberwiseClone(Boolean)

Creates a shallow copy of the current MarshalByRefObject object.(Inherited from MarshalByRefObject.)

System_CAPS_protmethodOnLoadCompleted(AsyncCompletedEventArgs)

Raises the LoadCompleted event.

System_CAPS_protmethodOnSoundLocationChanged(EventArgs)

Raises the SoundLocationChanged event.

System_CAPS_protmethodOnStreamChanged(EventArgs)

Raises the StreamChanged event.

System_CAPS_pubmethodPlay()

Plays the .wav file using a new thread, and loads the .wav file first if it has not been loaded.

System_CAPS_pubmethodPlayLooping()

Plays and loops the .wav file using a new thread, and loads the .wav file first if it has not been loaded.

System_CAPS_pubmethodPlaySync()

Plays the .wav file and loads the .wav file first if it has not been loaded.

System_CAPS_pubmethodStop()

Stops playback of the sound if playback is occurring.

System_CAPS_pubmethodToString()

Returns a String containing the name of the Component, if any. This method should not be overridden.(Inherited from Component.)

NameDescription
System_CAPS_pubeventDisposed

Occurs when the component is disposed by a call to the Dispose method. (Inherited from Component.)

System_CAPS_pubeventLoadCompleted

Occurs when a .wav file has been successfully or unsuccessfully loaded.

System_CAPS_pubeventSoundLocationChanged

Occurs when a new audio source path for this SoundPlayer has been set.

System_CAPS_pubeventStreamChanged

Occurs when a new Stream audio source for this SoundPlayer has been set.

The SoundPlayer class provides a simple interface for loading and playing a .wav file. The SoundPlayer 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 SoundPlayer class, configure a 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 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 SoundPlayer 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 SoundPlayer 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 NIB: Consuming Events.

For more information about SoundPlayer, see SoundPlayer Class Overview.

System_CAPS_noteNote

   The 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 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.

The following code example demonstrates the use of the SoundPlayer class for playing .wav files from a local path or a Uniform Resource Identifier (URI).

No code example is currently available or this language may not be supported.

.NET Framework
Available since 2.0

Any public static ( Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Return to top
Show: