vb.net2005中声音文件的播放方法小结

本文介绍使用VB.NET进行声音播放的方法,包括如何通过不同模式播放声音文件、如何停止播放以及如何实现声音的连续播放。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 1
my.Computer.Audio.Play()
支持不同的模式
AudioPlayMode.Background 
在后台播放声音。调用代码继续执行。
AudioPlayMode.BackgroundLoop
在后台播放声音,直到调用 My.Computer.Audio.Stop 方法。调用代码继续执行。
AudioPlayMode.WaitToComplete
播放声音并等待播放完毕,然后再继续执行调用代码。
http://msdn.microsoft.com/zh-cn/asp.net/cf1shcah(VS.80).aspx有详细说明
2
http://dobon.net/vb/dotnet/programing/playwavfile.html
.Net Framework2.0以后

Private player As System.Media.SoundPlayer = Nothing

'WAVEファイルを再生する
Private Sub PlaySound(ByVal waveFile As String)
    '再生されているときは止める
    If Not (player Is Nothing) Then
        StopSound()
    End If

    '読み込む
    player = New System.Media.SoundPlayer(waveFile)
    '非同期再生する
    player.Play()

    '次のようにすると、ループ再生される
    'player.PlayLooping()

    '次のようにすると、最後まで再生し終えるまで待機する
    'player.PlaySync()
End Sub

'再生されている音を止める
Private Sub StopSound()
    If Not (player Is Nothing) Then
        player.Stop()
        player.Dispose()
        player = Nothing
    End If
End Sub

'Button1のClickイベントハンドラ
Private Sub Button1_Click(ByVal sender As Object, _
        ByVal e As EventArgs) Handles Button1.Click
    PlaySound("C:/music.wav")
End Sub

'Button2のClickイベントハンドラ
Private Sub Button2_Click(ByVal sender As Object, _
        ByVal e As System.EventArgs) Handles Button2.Click
    StopSound()
End Sub

3 声音的连续播放
'該当再生リストの再生
Dim myProcess As Process = New Process
Dim a As New ProcessStartInfo("mplay32.exe")
Try
‘PlayList ,you can create by MediaPlay
‘You can play a wav file certainly
    myProcess.StartInfo.FileName = "c:/temp.wpl"  
    myProcess.Start()
Catch
   
End Try

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值