0

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

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

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

2 回答 2

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

希望这可以帮助。

于 2012-11-01T06:56:29.423 回答
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))
于 2012-11-01T18:14:51.017 回答