如何从Web远程播放Visual Basic中的随机wav文件

时间:2012-11-20 17:44:16

标签: vb.net audio random

我正在制作一个程序而不是嵌入wav文件(程序变得很大);我决定将歌曲上传到我的网站。该程序是一个孩子的游戏,一旦游戏结束,我希望它从URL播放随机歌曲。最初,我使用代码从嵌入式资源中执行此操作。这是代码:

        Public Sub PlayRandomTrack()
           Randomize()

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

    Select Case trackNum
        Case 1
            My.Computer.Audio.Play(My.Resources.CallingMonsters, AudioPlayMode.Background)
        Case 2
            My.Computer.Audio.Play(My.Resources.McclainSisters, AudioPlayMode.Background)
        Case 3
            My.Computer.Audio.Play(My.Resources.Mendler, AudioPlayMode.Background)
        Case 4
            My.Computer.Audio.Play(My.Resources.Pray, AudioPlayMode.Background)
        Case Else
            My.Computer.Audio.Play(My.Resources.WillowWhip, AudioPlayMode.Background)

    End Select
End Sub

有没有办法简单地将URL放在某处而不是整个新代码?如果它是一个需要新代码的新代码?我在网上找不到任何相关信息。

1 个答案:

答案 0 :(得分:0)

Option Strict On
Imports System.Net
Imports System.IO
Public Class Form1
    Dim SongURLs As New List(Of String)
    Dim R As New Random
    Private Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
        GameOver()
    End Sub
    Sub GameOver()
        PlayRandomInternetSong()
    End Sub
    Sub PlayRandomInternetSong()
        Randomize()
        Dim RandomSongIndex As Integer = R.Next(0, SongURLs.Count)
        PlayOnlineWaveFile(SongURLs(RandomSongIndex))
    End Sub
    Sub PlayOnlineWaveFile(ByVal Address As String)
        Dim WavBytes As Byte()
        Using W As New WebClient
            WavBytes = W.DownloadData(Address)
        End Using
        Using mStream As New MemoryStream(WavBytes, False)
            My.Computer.Audio.Play(mStream, AudioPlayMode.Background)
        End Using
    End Sub
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        SongURLs.Add("http://www.villagegeek.com/downloads/webwavs/adios.wav")
        SongURLs.Add("http://www.villagegeek.com/downloads/webwavs/excellent.wav")
        SongURLs.Add("http://www.villagegeek.com/downloads/webwavs/failure1.wav")
        SongURLs.Add("http://www.villagegeek.com/downloads/webwavs/Austin_Powers_groovy.wav")
        SongURLs.Add("http://www.villagegeek.com/downloads/webwavs/american_pie_bless_net.wav")
        SongURLs.Add("http://www.villagegeek.com/downloads/webwavs/BluesBros_106miles.wav")
    End Sub
End Class