如何在VB.NET中使用一个按钮添加播放两个声音文件

时间:2018-11-30 11:04:22

标签: vb.net

我想在VB.NET中创建一个按钮,该按钮具有两个可变的图片和两个可变的声音,例如:如果单击该按钮,则按钮的背景将显示pic1并播放sound1.wav,然后如果再次单击该按钮,将pic2显示为按钮背景并播放sound2.wav

我可以使用此代码更改按钮背景,但是我不知道如何添加两种播放声音:

 Public Class Form1
 Dim Index As Integer = 0
 Dim Image1 As New List(Of Image)


Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Index = Index Xor 1
    Button1.BackgroundImage = Image1(Index)

End Sub

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Image1.Add(Image.FromFile("D:\pic1.png"))
    Image1.Add(Image.FromFile("D:\pic2.png"))
End Sub


End Class

1 个答案:

答案 0 :(得分:0)

Image类具有tag属性,因此您可以使用它存储列表中要加载的每个图像的声音路径。

Public Class Form1
    Dim Index As Integer = 0
    Dim Image1 As New List(Of Image)

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Index = Index Xor 1
        Button1.BackgroundImage = Image1(Index)
        My.Computer.Audio.Play(Image1(Index).Tag, AudioPlayMode.WaitToComplete)
    End Sub

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Image1.Add(Image.FromFile("D:\pic1.png"))
        Image1(0).tag = "d:\sound1.wav"
        Image1.Add(Image.FromFile("D:\pic2.png"))
        Image1(1).tag = "d:\sound2.wav"
    End Sub
End Class

希望这会有所帮助