Export (0) Print
Expand All

SoundPlayer Class

Controls playback of a sound from a .wav file.

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

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

The SoundPlayer type exposes the following members.

  NameDescription
Public methodSoundPlayer()Initializes a new instance of the SoundPlayer class.
Public methodSoundPlayer(Stream)Initializes a new instance of the SoundPlayer class, and attaches the .wav file within the specified Stream.
Public methodSoundPlayer(String)Initializes a new instance of the SoundPlayer class, and attaches the specified .wav file.
Protected methodSoundPlayer(SerializationInfo, StreamingContext)Initializes a new instance of the SoundPlayer class.
Top

  NameDescription
Protected propertyCanRaiseEventsGets a value indicating whether the component can raise an event. (Inherited from Component.)
Public propertyContainerGets the IContainer that contains the Component. (Inherited from Component.)
Protected propertyDesignModeGets a value that indicates whether the Component is currently in design mode. (Inherited from Component.)
Protected propertyEventsGets the list of event handlers that are attached to this Component. (Inherited from Component.)
Public propertyIsLoadCompletedGets a value indicating whether loading of a .wav file has successfully completed.
Public propertyLoadTimeoutGets or sets the time, in milliseconds, in which the .wav file must load.
Public propertySiteGets or sets the ISite of the Component. (Inherited from Component.)
Public propertySoundLocationGets or sets the file path or URL of the .wav file to load.
Public propertyStreamGets or sets the Stream from which to load the .wav file.
Public propertyTagGets or sets the Object that contains data about the SoundPlayer.
Top

  NameDescription
Public methodCreateObjRefCreates an object that contains all the relevant information required to generate a proxy used to communicate with a remote object. (Inherited from MarshalByRefObject.)
Public methodDispose()Releases all resources used by the Component. (Inherited from Component.)
Protected methodDispose(Boolean)Releases the unmanaged resources used by the Component and optionally releases the managed resources. (Inherited from Component.)
Public methodEquals(Object)Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Protected methodFinalizeReleases unmanaged resources and performs other cleanup operations before the Component is reclaimed by garbage collection. (Inherited from Component.)
Public methodGetHashCodeServes as a hash function for a particular type. (Inherited from Object.)
Public methodGetLifetimeServiceRetrieves the current lifetime service object that controls the lifetime policy for this instance. (Inherited from MarshalByRefObject.)
Protected methodGetServiceReturns an object that represents a service provided by the Component or by its Container. (Inherited from Component.)
Public methodGetTypeGets the Type of the current instance. (Inherited from Object.)
Public methodInitializeLifetimeServiceObtains a lifetime service object to control the lifetime policy for this instance. (Inherited from MarshalByRefObject.)
Public methodLoadLoads a sound synchronously.
Public methodLoadAsyncLoads a .wav file from a stream or a Web resource using a new thread.
Protected methodMemberwiseClone()Creates a shallow copy of the current Object. (Inherited from Object.)
Protected methodMemberwiseClone(Boolean)Creates a shallow copy of the current MarshalByRefObject object. (Inherited from MarshalByRefObject.)
Protected methodOnLoadCompletedRaises the LoadCompleted event.
Protected methodOnSoundLocationChangedRaises the SoundLocationChanged event.
Protected methodOnStreamChangedRaises the StreamChanged event.
Public methodPlayPlays the .wav file using a new thread, and loads the .wav file first if it has not been loaded.
Public methodPlayLoopingPlays and loops the .wav file using a new thread, and loads the .wav file first if it has not been loaded.
Public methodPlaySyncPlays the .wav file and loads the .wav file first if it has not been loaded.
Public methodStopStops playback of the sound if playback is occurring.
Public methodToStringReturns a String containing the name of the Component, if any. This method should not be overridden. (Inherited from Component.)
Top

  NameDescription
Public eventDisposedOccurs when the component is disposed by a call to the Dispose method. (Inherited from Component.)
Public eventLoadCompletedOccurs when a .wav file has been successfully or unsuccessfully loaded.
Public eventSoundLocationChangedOccurs when a new audio source path for this SoundPlayer has been set.
Public eventStreamChangedOccurs when a new Stream audio source for this SoundPlayer has been set.
Top

  NameDescription
Explicit interface implemetationPrivate methodISerializable.GetObjectDataFor a description of this member, see the ISerializable.GetObjectData method.
Top

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 Consuming Events.

For more information about SoundPlayer, see SoundPlayer Class Overview.

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.

NoteNote

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

Supported in: 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

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

Community Additions

ADD
Show:
© 2014 Microsoft