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
    包含声音文件名称的 String

  • data
    表示声音文件的 Byte 数组。

  • stream
    表示声音文件的 Stream

  • playMode
    适合播放声音的 AudioPlayMode 枚举模式。默认情况下,为 AudioPlayMode.Background。

异常

下面的情况可能会导致异常:

备注

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 方法的任务示例。

要执行的操作

请参见

播放声音一次

如何:在 Visual Basic 中播放声音并等待完成

播放声音多次

如何:在 Visual Basic 中播放循环声音

在后台播放声音

如何:在 Visual Basic 中播放声音

示例

指定 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 服务

网站

权限

以下权限可能是必需的:

权限

说明

FileIOPermission

控制访问文件和文件夹的能力。关联的枚举:Unrestricted

SecurityPermission

描述应用于代码的安全权限集。关联的枚举:ControlThread

有关更多信息,请参见代码访问安全性请求权限

请参见

任务

如何:在 Visual Basic 中播放声音并等待完成

如何:在 Visual Basic 中播放循环声音

如何:在 Visual Basic 中播放声音

参考

My.Computer.Audio 对象

My.Computer.Audio.Stop 方法

AudioPlayMode 枚举

System.IO.Stream

Audio.Play