VB播放随机声音wav文件

时间:2012-11-01 06:09:09

标签: vb.net background audio

我如何在vb中播放随机声音,我有3个波形文件,我想在使用我的应用程序时在后台随机播放?

这是我如何只播放一首歌。

My.Computer.Audio.Play(My.Resources.music1, AudioPlayMode.Background)

2 个答案:

答案 0 :(得分:0)

您可以这样做:

Public Sub PlayRandomTrack()

    Dim trackNum As Integer = CInt(Rnd() * 3 + 0.5)

    Select Case trackNum

        Case 1
            My.Computer.Audio.Play(My.Resources.music1, AudioPlayMode.Background)

        Case 2
            My.Computer.Audio.Play(My.Resources.music2, AudioPlayMode.Background)

        Case Else
            My.Computer.Audio.Play(My.Resources.music3, AudioPlayMode.Background)

    End Select

End Sub

希望这有帮助。

答案 1 :(得分:0)

我认为这是正确的方法:

Public Function GetRandom(ByVal Min As Integer, ByVal Max As Integer) As Integer
    Dim Generator As System.Random = New System.Random()
    Return Generator.Next(Min, Max)
End Function

Public Function Play(Byval Num as integer)
if num = 1 then
My.Computer.Audio.Play(My.Resources.music1, AudioPlayMode.Background)
      Threading.Thread.Sleep(5000)
return true
elseif num = 2 then
My.Computer.Audio.Play(My.Resources.music2, AudioPlayMode.Background)
      Threading.Thread.Sleep(5000)
return true
end if
return false
End Function

以这种方式使用它:

play(GetRandom(1,2))