My.Computer.Audio.Play 方法
更新:2007 年 11 月
播放 .wav 声音文件。
' Usage
My.Computer.Audio.Play(location)
My.Computer.Audio.Play(location ,playMode)
My.Computer.Audio.Play(data ,playMode)
My.Computer.Audio.Play(stream ,playMode)
' Declaration
Public Sub Play( _
ByVal location As String _
)
' -or-
Public Sub Play( _
ByVal location As String, _
ByVal playMode As AudioPlayMode _
)
' -or-
Public Sub Play( _
ByVal data As Byte(), _
ByVal playMode As AudioPlayMode _
)
' -or-
Public Sub Play( _
ByVal stream As System.IO.Stream, _
ByVal playMode As AudioPlayMode _
)
参数
location
包含声音文件名称的 Stringdata
表示声音文件的 Byte 数组。stream
表示声音文件的 Stream。playMode
适合播放声音的 AudioPlayMode 枚举模式。默认情况下,为 AudioPlayMode.Background。
异常
下面的情况可能会导致异常:
data 或 stream 为 Nothing,或者 location 为空字符串 (ArgumentNullException)。
playMode 参数不是 AudioPlayMode 枚举值之一 (InvalidEnumArgumentException)。
用户没有足够的权限访问由 location 命名的文件 (IOException)。
文件路径在 location 中的格式不正确 (DirectoryNotFoundException)。
location 中的路径名太长 (PathTooLongException)。
存在部分信任的情况,此时用户缺少足够的权限 (SecurityException)。
备注
Play 方法播放 .wav 声音文件,该文件共有三种形式,存储在 location 中为文件,存储在 data 中为字节数组,存储在 stream 中为流。
如果使用的重载只带 location 参数,则 Play 方法将在后台播放声音。否则,由 playMode 参数确定声音的播放方式。
playMode |
说明 |
---|---|
AudioPlayMode.Background |
在后台播放声音。调用代码继续执行。 |
AudioPlayMode.BackgroundLoop |
在后台播放声音,直到调用 My.Computer.Audio.Stop 方法。调用代码继续执行。 |
AudioPlayMode.WaitToComplete |
播放声音并等待播放完毕,然后再继续执行调用代码。 |
后台播放使得应用程序可以在播放声音时执行其他代码。有关更多信息,请参见如何:在 Visual Basic 中播放循环声音和如何:在 Visual Basic 中播放声音。
任务
下表列出了涉及 My.Computer.Audio.Play 方法的任务示例。
要执行的操作 |
请参见 |
---|---|
播放声音一次 |
|
播放声音多次 |
|
在后台播放声音 |
示例
指定 PlayMode.Background 时,My.Computer.Audio.Play 方法在后台播放指定声音。
Sub PlayBackgroundSoundFile()
My.Computer.Audio.Play("C:\Waterfall.wav", _
AudioPlayMode.Background)
End Sub
此代码示例只能在 Windows 窗体应用程序中运行。
文件名应引用系统上的 .wav 声音文件。
若要简化声音文件的管理,请考虑将这些文件存储为应用程序资源。之后,就能通过 My.Resources 对象访问它们。
要求
命名空间:Microsoft.VisualBasic.Devices
类:Audio
**程序集:**Visual Basic Runtime Library(位于 Microsoft.VisualBasic.dll 中)
按项目类型列出可用性
项目类型 |
可用 |
---|---|
Windows 应用程序 |
是 |
类库 |
是 |
控制台应用程序 |
是 |
Windows 控件库 |
是 |
Web 控件库 |
否 |
Windows 服务 |
是 |
网站 |
否 |
权限
以下权限可能是必需的:
权限 |
说明 |
---|---|
控制访问文件和文件夹的能力。关联的枚举:Unrestricted。 |
|
描述应用于代码的安全权限集。关联的枚举:ControlThread。 |