Visual Studio 2005 初体验之二:My命名空间中的音频播放
以前在使用VB或者.net2003的时候,播放音频文件需要调用DirectX or Win32 API,VS2005则免去了这个麻烦。
播放系统自带声音:My.Computer.Audio.PlaySystemSound(System.Media.SystemSounds.Asterisk)
播放本地音频文件:My.Computer.Audio.Play(wavfileName)
实例:
1、创建一个Project,名为PlaySound。
2、根据图片所示,把相应控件拖放到Form1窗体中。
3、程序运行时需要在systemSoundsComboBox中加载系统声音,代码如下:
systemSoundsComboBox.Items.AddRange(New String() {"Asterisk", "Beep", "Exclamation", "Hand"})
systemSoundsComboBox.Items.AddRange(New String() {"Asterisk", "Beep", "Exclamation", "Hand"})
4、单击Browse button,会弹出对话框来选择本地音频文件。
' Provide a file dialog for browsing for the sound file.
Dim openFileDialog1 As New OpenFileDialog()
openFileDialog1.InitialDirectory = "c:\\windows\\media"
openFileDialog1.Filter = "WAV files (*.wav)|*.wav|All files (*.*)|*.*"
openFileDialog1.FilterIndex = 1
openFileDialog1.RestoreDirectory = True
If (openFileDialog1.ShowDialog() = DialogResult.OK) Then
fileName = openFileDialog1.FileName
Me.soundFileNameTextBox.Text = fileName
End If
Dim openFileDialog1 As New OpenFileDialog()
openFileDialog1.InitialDirectory = "c:\\windows\\media"
openFileDialog1.Filter = "WAV files (*.wav)|*.wav|All files (*.*)|*.*"
openFileDialog1.FilterIndex = 1
openFileDialog1.RestoreDirectory = True
If (openFileDialog1.ShowDialog() = DialogResult.OK) Then
fileName = openFileDialog1.FileName
Me.soundFileNameTextBox.Text = fileName
End If
5、然后就可以Play/Stop本地的音频文件了。
My.Computer.Audio.Play(fileName)
My.Computer.Audio.Stop()
6、完整的代码如下:
Imports System.Media
Imports System.ComponentModel
Imports System.ComponentModel
Public Class PlaySound
Private fileName As String
' A SoundPlayer control is used for playing system sounds
' and .wav files.
Public Sub New()
' This call is required by the Windows Form Designer.
InitializeComponent()
' Display available system sounds on the form.
LoadSystemSounds()
End Sub
Private fileName As String
' A SoundPlayer control is used for playing system sounds
' and .wav files.
Public Sub New()
' This call is required by the Windows Form Designer.
InitializeComponent()
' Display available system sounds on the form.
LoadSystemSounds()
End Sub
Public Sub LoadSystemSounds()
' These are the system sounds currently supported.
' There is no list avaliable for enumerating,
' so load them manually
systemSoundsComboBox.Items.AddRange( _
New String() {"Asterisk", "Beep", "Exclamation", "Hand"})
End Sub
' These are the system sounds currently supported.
' There is no list avaliable for enumerating,
' so load them manually
systemSoundsComboBox.Items.AddRange( _
New String() {"Asterisk", "Beep", "Exclamation", "Hand"})
End Sub
Private Sub browseButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles browseButton.Click
' Provide a file dialog for browsing for the sound file.
Dim openFileDialog1 As New OpenFileDialog()
openFileDialog1.InitialDirectory = "c:\\windows\\media"
openFileDialog1.Filter = "WAV files (*.wav)|*.wav|All files (*.*)|*.*"
openFileDialog1.FilterIndex = 1
openFileDialog1.RestoreDirectory = True
If (openFileDialog1.ShowDialog() = DialogResult.OK) Then
fileName = openFileDialog1.FileName
Me.soundFileNameTextBox.Text = fileName
End If
End Sub
' Provide a file dialog for browsing for the sound file.
Dim openFileDialog1 As New OpenFileDialog()
openFileDialog1.InitialDirectory = "c:\\windows\\media"
openFileDialog1.Filter = "WAV files (*.wav)|*.wav|All files (*.*)|*.*"
openFileDialog1.FilterIndex = 1
openFileDialog1.RestoreDirectory = True
If (openFileDialog1.ShowDialog() = DialogResult.OK) Then
fileName = openFileDialog1.FileName
Me.soundFileNameTextBox.Text = fileName
End If
End Sub
Private Sub playSyncButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles playSyncButton.Click
If fileName IsNot Nothing Then
My.Computer.Audio.Play(fileName)
End If
End Sub
If fileName IsNot Nothing Then
My.Computer.Audio.Play(fileName)
End If
End Sub
Private Sub playAsyncButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles playAsyncButton.Click
If loopCheckBox.Checked Then
'You can also play the sound asynchronously and loop the sound using My.
My.Computer.Audio.Play(fileName, AudioPlayMode.BackgroundLoop)
Else
'You can play the sound synchronously but using the WaitToComplete argument
My.Computer.Audio.Play(fileName, AudioPlayMode.Background)
End If
End Sub
Private Sub stopAsyncPlayButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles stopAsyncPlayButton.Click
'You can stop the audio from playing by simply using the Stop Method.
My.Computer.Audio.Stop()
End Sub
If loopCheckBox.Checked Then
'You can also play the sound asynchronously and loop the sound using My.
My.Computer.Audio.Play(fileName, AudioPlayMode.BackgroundLoop)
Else
'You can play the sound synchronously but using the WaitToComplete argument
My.Computer.Audio.Play(fileName, AudioPlayMode.Background)
End If
End Sub
Private Sub stopAsyncPlayButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles stopAsyncPlayButton.Click
'You can stop the audio from playing by simply using the Stop Method.
My.Computer.Audio.Stop()
End Sub
Private Sub playSystemSoundButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles playSystemSoundButton.Click
Select Case systemSoundsComboBox.Text
Case "Asterisk"
My.Computer.Audio.PlaySystemSound(System.Media.SystemSounds.Asterisk)
Exit Select
Case "Beep"
My.Computer.Audio.PlaySystemSound(System.Media.SystemSounds.Beep)
Exit Select
Case "Exclamation"
My.Computer.Audio.PlaySystemSound(System.Media.SystemSounds.Exclamation)
Exit Select
Case "Hand"
My.Computer.Audio.PlaySystemSound(System.Media.SystemSounds.Hand)
Exit Select
Case Else
Throw New ApplicationException("Invalid system sound.")
End Select
Select Case systemSoundsComboBox.Text
Case "Asterisk"
My.Computer.Audio.PlaySystemSound(System.Media.SystemSounds.Asterisk)
Exit Select
Case "Beep"
My.Computer.Audio.PlaySystemSound(System.Media.SystemSounds.Beep)
Exit Select
Case "Exclamation"
My.Computer.Audio.PlaySystemSound(System.Media.SystemSounds.Exclamation)
Exit Select
Case "Hand"
My.Computer.Audio.PlaySystemSound(System.Media.SystemSounds.Hand)
Exit Select
Case Else
Throw New ApplicationException("Invalid system sound.")
End Select
End Sub
End Class
7、运行后的结果如图所示: