0

我正在制作一个程序,而不是嵌入 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 放在某个地方而不是一个全新的代码?如果它是需要的新代码,它会是什么?我在网上找不到这方面的任何信息。

4

1 回答 1

1
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
于 2012-11-21T05:37:52.407 回答