Visual Studio 2005 初体验之二:My命名空间中的音频播放

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窗体中。

design.JPG

3、程序运行时需要在systemSoundsComboBox中加载系统声音,代码如下:
        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
 
5、然后就可以Play/Stop本地的音频文件了。
        My.Computer.Audio.Play(fileName)
        My.Computer.Audio.Stop()
 
6、完整的代码如下:
Imports System.Media
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
    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
    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
    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
    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
    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
    End Sub
End Class
 
7、运行后的结果如图所示:

sound.JPG

转载于:https://www.cnblogs.com/xiongeee/archive/2006/12/11/589195.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值