Audio.Play 方法

定义

播放 .wav 声音文件。Plays a .wav sound file.

重载

Play(String)

播放 .wav 声音文件。Plays a .wav sound file.

Play(Byte[], AudioPlayMode)

播放 .wav 声音文件。Plays a .wav sound file.

Play(Stream, AudioPlayMode)

播放 .wav 声音文件。Plays a .wav sound file.

Play(String, AudioPlayMode)

播放 .wav 声音文件。Plays a .wav sound file.

Play(String)

播放 .wav 声音文件。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)

参数

location
String

一个包含声音文件的名称的 StringA String containing the name of the sound file

异常

location 是一个空字符串。location is an empty string.

用户没有足够的权限访问由 location 命名的文件。The user does not have sufficient permissions to access the file named by location.

文件路径在 location 中的格式不正确。The file path is malformed in location.

location 中的路径名太长。The path name in location is too long.

存在部分信任的情况,此时用户缺少足够的权限。A partial-trust situation exists in which the user lacks necessary permissions.

示例

指定My.Computer.Audio.PlayPlayMode.Background ,方法在后台播放指定的声音。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

此代码示例只能在 Windows 窗体应用程序中运行。This code example can only run within a Windows Forms application.

文件名应引用您的系统上的 .wav 声音文件。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. 然后, My.Resources可以通过对象访问它们。They can then be accessed through the My.Resources object.

注解

方法在中作为location文件、中data的字节数组或以流stream的形式播放 .wav 声音文件。 PlayThe 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.

如果使用只location带参数的重载,则Play方法会在后台播放声音。If the overload that takes only the location parameter is used, the Play method plays the sound in the background. 否则, playMode参数将确定播放声音的方式。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 在调用Stop方法之前,在后台播放声音。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 应用程序Windows Application Yes
类库Class Library Yes
控制台应用程序Console Application Yes
Windows 控件库Windows Control Library Yes
Web 控件库Web Control Library NoNo
Windows 服务Windows Service Yes
网站Web Site NoNo

安全性

FileIOPermission
控制文件和文件夹的访问权限。Controls the ability to access files and folders. 关联的枚举:UnrestrictedAssociated enumeration: Unrestricted.

SecurityPermission
描述应用于代码的安全权限集。Describes a set of security permissions applied to code. 关联的枚举:ControlThreadAssociated enumeration: ControlThread.

另请参阅

Play(Byte[], AudioPlayMode)

播放 .wav 声音文件。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)

参数

data
Byte[]

表示声音文件的 Byte 数组。Byte array that represents the sound file.

playMode
AudioPlayMode

用于播放声音的 AudioPlayMode 模式。AudioPlayMode mode for playing the sound. 默认为 AudioPlayMode.BackgroundBy default, AudioPlayMode.Background.

异常

dataNothingdata is Nothing.

playMode 不是 AudioPlayMode 枚举值之一。playMode is not one of the AudioPlayMode enumeration values.

存在部分信任的情况,此时用户缺少足够的权限。A partial-trust situation exists in which the user lacks necessary permissions.

示例

指定My.Computer.Audio.PlayPlayMode.Background ,方法在后台播放指定的声音。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

此代码示例只能在 Windows 窗体应用程序中运行。This code example can only run within a Windows Forms application.

文件名应引用您的系统上的 .wav 声音文件。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. 然后, My.Resources可以通过对象访问它们。They can then be accessed through the My.Resources object.

注解

方法在中作为location文件、中data的字节数组或以流stream的形式播放 .wav 声音文件。 PlayThe 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.

如果使用只location带参数的重载,则Play方法会在后台播放声音。If the overload that takes only the location parameter is used, the Play method plays the sound in the background. 否则, playMode参数将确定播放声音的方式。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 在调用Stop方法之前,在后台播放声音。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 应用程序Windows Application Yes
类库Class Library Yes
控制台应用程序Console Application Yes
Windows 控件库Windows Control Library Yes
Web 控件库Web Control Library NoNo
Windows 服务Windows Service Yes
网站Web Site NoNo

安全性

SecurityPermission
描述应用于代码的安全权限集。Describes a set of security permissions applied to code. 关联的枚举:ControlThreadAssociated enumeration: ControlThread.

另请参阅

Play(Stream, AudioPlayMode)

播放 .wav 声音文件。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

参数

stream
Stream

表示声音文件的 StreamStream that represents the sound file.

playMode
AudioPlayMode

用于播放声音的 AudioPlayMode 模式。AudioPlayMode mode for playing the sound. 默认为 AudioPlayMode.BackgroundBy default, AudioPlayMode.Background.

异常

streamNothingstream is Nothing.

playMode 不是 AudioPlayMode 枚举值之一。playMode is not one of the AudioPlayMode enumeration values.

存在部分信任的情况,此时用户缺少足够的权限。A partial-trust situation exists in which the user lacks necessary permissions.

示例

指定My.Computer.Audio.PlayPlayMode.Background ,方法在后台播放指定的声音。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

此代码示例只能在 Windows 窗体应用程序中运行。This code example can only run within a Windows Forms application.

文件名应引用您的系统上的 .wav 声音文件。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. 然后, My.Resources可以通过对象访问它们。They can then be accessed through the My.Resources object.

注解

方法在中作为location文件、中data的字节数组或以流stream的形式播放 .wav 声音文件。 PlayThe 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.

如果使用只location带参数的重载,则Play方法会在后台播放声音。If the overload that takes only the location parameter is used, the Play method plays the sound in the background. 否则, playMode参数将确定播放声音的方式。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 在调用Stop方法之前,在后台播放声音。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 应用程序Windows Application Yes
类库Class Library Yes
控制台应用程序Console Application Yes
Windows 控件库Windows Control Library Yes
Web 控件库Web Control Library NoNo
Windows 服务Windows Service Yes
网站Web Site NoNo

安全性

SecurityPermission
描述应用于代码的安全权限集。Describes a set of security permissions applied to code. 关联枚举:ControlThreadAssociated enumeration: ControlThread.

另请参阅

Play(String, AudioPlayMode)

播放 .wav 声音文件。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)

参数

location
String

一个包含声音文件的名称的 StringA String containing the name of the sound file

playMode
AudioPlayMode

用于播放声音的 AudioPlayMode 模式。AudioPlayMode mode for playing the sound. 默认为 AudioPlayMode.BackgroundBy default, AudioPlayMode.Background.

异常

location 是一个空字符串。location is an empty string.

用户没有足够的权限访问由 location 命名的文件。The user does not have sufficient permissions to access the file named by location.

文件路径在 location 中的格式不正确。The file path is malformed in location.

location 中的路径名太长。The path name in location is too long.

playMode 不是 AudioPlayMode 枚举值之一。playMode is not one of the AudioPlayMode enumeration values.

存在部分信任的情况,此时用户缺少足够的权限。A partial-trust situation exists in which the user lacks necessary permissions.

示例

指定My.Computer.Audio.PlayPlayMode.Background ,方法在后台播放指定的声音。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

此代码示例只能在 Windows 窗体应用程序中运行。This code example can only run within a Windows Forms application.

文件名应引用您的系统上的 .wav 声音文件。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. 然后, My.Resources可以通过对象访问它们。They can then be accessed through the My.Resources object.

注解

方法在中作为location文件、中data的字节数组或以流stream的形式播放 .wav 声音文件。 PlayThe 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.

如果使用只location带参数的重载,则Play方法会在后台播放声音。If the overload that takes only the location parameter is used, the Play method plays the sound in the background. 否则, playMode参数将确定播放声音的方式。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 在调用Stop方法之前,在后台播放声音。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 应用程序Windows Application Yes
类库Class Library Yes
控制台应用程序Console Application Yes
Windows 控件库Windows Control Library Yes
Web 控件库Web Control Library NoNo
Windows 服务Windows Service Yes
网站Web Site NoNo

安全性

FileIOPermission
控制文件和文件夹的访问权限。Controls the ability to access files and folders. 关联枚举:UnrestrictedAssociated enumeration: Unrestricted.

SecurityPermission
描述应用于代码的安全权限集。Describes a set of security permissions applied to code. 关联的枚举:ControlThreadAssociated enumeration: ControlThread.

另请参阅

适用于