Edit

Share via


Audio.Play Method

Definition

Plays a .wav sound file.

Overloads

Play(String)

Plays a .wav sound file.

Play(Byte[], AudioPlayMode)

Plays a .wav sound file.

Play(Stream, AudioPlayMode)

Plays a .wav sound file.

Play(String, AudioPlayMode)

Plays a .wav sound file.

Play(String)

Plays a .wav sound file.

public:
 void Play(System::String ^ location);
public void Play (string location);
member this.Play : string -> unit
Public Sub Play (location As String)

Parameters

location
String

A String containing the name of the sound file.

Exceptions

location is an empty string.

The user does not have sufficient permissions to access the file named by location.

The file path is malformed in location.

The path name in location is too long.

A partial-trust situation exists in which the user lacks necessary permissions.

Examples

The My.Computer.Audio.Play method plays the specified sound in the background when PlayMode.Background is specified.

Sub PlayBackgroundSoundFile()
    My.Computer.Audio.Play("C:\Waterfall.wav", 
        AudioPlayMode.WaitToComplete)
End Sub

This code example can only run within a Windows Forms application.

The file name should reference a .wav sound file on your system.

To simplify the management of your sound files, consider storing the files as application resources. They can then be accessed through the My.Resources object.

Remarks

The Play method plays the .wav sound file stored as a file at location, as a byte array in data, or as a stream in stream.

If the overload that takes only the location parameter is used, the Play method plays the sound in the background. Otherwise, the playMode parameter determines how the sound will play.

playMode Description
AudioPlayMode.Background Plays the sound in the background. The calling code continues to execute.
AudioPlayMode.BackgroundLoop Plays the sound in the background until the Stop method is called. The calling code continues to execute.
AudioPlayMode.WaitToComplete Plays the sound and waits until it completes before the calling code continues.

Background playing lets the application execute other code while the sound plays.

For more information, see Playing Sounds.

Availability by Project Type

Project type Available
Windows Application Yes
Class Library Yes
Console Application Yes
Windows Control Library Yes
Web Control Library No
Windows Service Yes
Web Site No

See also

Applies to

Play(Byte[], AudioPlayMode)

Plays a .wav sound file.

public:
 void Play(cli::array <System::Byte> ^ data, Microsoft::VisualBasic::AudioPlayMode playMode);
public void Play (byte[] data, Microsoft.VisualBasic.AudioPlayMode playMode);
member this.Play : byte[] * Microsoft.VisualBasic.AudioPlayMode -> unit
Public Sub Play (data As Byte(), playMode As AudioPlayMode)

Parameters

data
Byte[]

Byte array that represents the sound file.

playMode
AudioPlayMode

AudioPlayMode mode for playing the sound. By default, AudioPlayMode.Background.

Exceptions

data is Nothing.

playMode is not one of the AudioPlayMode enumeration values.

A partial-trust situation exists in which the user lacks necessary permissions.

Examples

The My.Computer.Audio.Play method plays the specified sound in the background when PlayMode.Background is specified.

Sub PlayBackgroundSoundFile()
    My.Computer.Audio.Play("C:\Waterfall.wav", 
        AudioPlayMode.WaitToComplete)
End Sub

This code example can only run within a Windows Forms application.

The file name should reference a .wav sound file on your system.

To simplify the management of your sound files, consider storing the files as application resources. They can then be accessed through the My.Resources object.

Remarks

The Play method plays the .wav sound file stored as a file at location, as a byte array in data, or as a stream in stream.

If the overload that takes only the location parameter is used, the Play method plays the sound in the background. Otherwise, the playMode parameter determines how the sound will play.

playMode Description
AudioPlayMode.Background Plays the sound in the background. The calling code continues to execute.
AudioPlayMode.BackgroundLoop Plays the sound in the background until the Stop method is called. The calling code continues to execute.
AudioPlayMode.WaitToComplete Plays the sound and waits until it completes before the calling code continues.

Background playing lets the application execute other code while the sound plays.

For more information, see Playing Sounds.

Availability by Project Type

Project type Available
Windows Application Yes
Class Library Yes
Console Application Yes
Windows Control Library Yes
Web Control Library No
Windows Service Yes
Web Site No

See also

Applies to

Play(Stream, AudioPlayMode)

Plays a .wav sound file.

public:
 void Play(System::IO::Stream ^ stream, Microsoft::VisualBasic::AudioPlayMode playMode);
public void Play (System.IO.Stream stream, Microsoft.VisualBasic.AudioPlayMode playMode);
member this.Play : System.IO.Stream * Microsoft.VisualBasic.AudioPlayMode -> unit
Public Sub Play (stream As Stream, playMode As AudioPlayMode)

Parameters

stream
Stream

Stream that represents the sound file.

playMode
AudioPlayMode

AudioPlayMode mode for playing the sound. By default, AudioPlayMode.Background.

Exceptions

stream is Nothing.

playMode is not one of the AudioPlayMode enumeration values.

A partial-trust situation exists in which the user lacks necessary permissions.

Examples

The My.Computer.Audio.Play method plays the specified sound in the background when PlayMode.Background is specified.

Sub PlayBackgroundSoundFile()
    My.Computer.Audio.Play("C:\Waterfall.wav", 
        AudioPlayMode.WaitToComplete)
End Sub

This code example can only run within a Windows Forms application.

The file name should reference a .wav sound file on your system.

To simplify the management of your sound files, consider storing the files as application resources. They can then be accessed through the My.Resources object.

Remarks

The Play method plays the .wav sound file stored as a file at location, as a byte array in data, or as a stream in stream.

If the overload that takes only the location parameter is used, the Play method plays the sound in the background. Otherwise, the playMode parameter determines how the sound will play.

playMode Description
AudioPlayMode.Background Plays the sound in the background. The calling code continues to execute.
AudioPlayMode.BackgroundLoop Plays the sound in the background until the Stop method is called. The calling code continues to execute.
AudioPlayMode.WaitToComplete Plays the sound and waits until it completes before the calling code continues.

Background playing lets the application execute other code while the sound plays.

For more information, see Playing Sounds.

Availability by Project Type

Project type Available
Windows Application Yes
Class Library Yes
Console Application Yes
Windows Control Library Yes
Web Control Library No
Windows Service Yes
Web Site No

See also

Applies to

Play(String, AudioPlayMode)

Plays a .wav sound file.

public:
 void Play(System::String ^ location, Microsoft::VisualBasic::AudioPlayMode playMode);
public void Play (string location, Microsoft.VisualBasic.AudioPlayMode playMode);
member this.Play : string * Microsoft.VisualBasic.AudioPlayMode -> unit
Public Sub Play (location As String, playMode As AudioPlayMode)

Parameters

location
String

A String containing the name of the sound file.

playMode
AudioPlayMode

AudioPlayMode mode for playing the sound. By default, AudioPlayMode.Background.

Exceptions

location is an empty string.

The user does not have sufficient permissions to access the file named by location.

The file path is malformed in location.

The path name in location is too long.

playMode is not one of the AudioPlayMode enumeration values.

A partial-trust situation exists in which the user lacks necessary permissions.

Examples

The My.Computer.Audio.Play method plays the specified sound in the background when PlayMode.Background is specified.

Sub PlayBackgroundSoundFile()
    My.Computer.Audio.Play("C:\Waterfall.wav", 
        AudioPlayMode.WaitToComplete)
End Sub

This code example can only run within a Windows Forms application.

The file name should reference a .wav sound file on your system.

To simplify the management of your sound files, consider storing the files as application resources. They can then be accessed through the My.Resources object.

Remarks

The Play method plays the .wav sound file stored as a file at location, as a byte array in data, or as a stream in stream.

If the overload that takes only the location parameter is used, the Play method plays the sound in the background. Otherwise, the playMode parameter determines how the sound will play.

playMode Description
AudioPlayMode.Background Plays the sound in the background. The calling code continues to execute.
AudioPlayMode.BackgroundLoop Plays the sound in the background until the Stop method is called. The calling code continues to execute.
AudioPlayMode.WaitToComplete Plays the sound and waits until it completes before the calling code continues.

Background playing lets the application execute other code while the sound plays.

For more information, see Playing Sounds.

Availability by Project Type

Project type Available
Windows Application Yes
Class Library Yes
Console Application Yes
Windows Control Library Yes
Web Control Library No
Windows Service Yes
Web Site No

See also

Applies to