0

我正在使用 Visual Basic 2010,我正在用 c 编写我正在构建的程序,我的问题是我设置了 Media.SoundPlayer,我希望它在表单加载时激活,这样我的代码看起来像这样(编辑:这段代码不起作用,在调试中加载时声音不会播放,我没有错误)

Private Sub BGMusic(ByVal x As Integer)
        Dim msp As New Media.SoundPlayer
        Dim Music As String = Install + "\Music\innmusic.wav"
        msp.SoundLocation = Music
        If x = 1 Then
            msp.Play()
        Else
            msp.Stop()
        End If
    End Sub

Private Sub Innmenu_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            BGMusic(1)
    End Sub

我还想指出 Install + "\Music\innmusic.wav" 是一个有效路径,因为我可以设置一个按钮来运行 BGMusic(1) 并且还可以播放音乐。就我的目的而言,form.shown 会起作用,但我想知道我未来的程序出了什么问题,以便我可以使用 form.load

4

1 回答 1

0

有可能您的Install变量没有显示您声明它的位置,它是空的或未正确初始化。下面我给大家做个说明:

Public Class Innmenu

Dim Install As String = ""

Private Sub Innmenu_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    BGMusic(1) ' It will not play because Install is still empty
End Sub

Private Sub BGMusic(ByVal x As Integer)
    Dim msp As New Media.SoundPlayer
    Dim Music As String = Install + "\Music\innmusic.wav"
    msp.SoundLocation = Music
    If x = 1 Then
        msp.Play()
    Else
        msp.Stop()
    End If
End Sub


Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Install = "C:\" 'Let us assume that your folder Music and file innmusic.wav is in C:
BGMusic(1) ' Music will now play because Install has now the right value assigned

End Sub

尝试Try Catch顺便为您的BGMusic方法做,例如:

Private Sub BGMusic(ByVal x As Integer)
    Dim msp As New Media.SoundPlayer
    Dim Music As String = Install + "\Music\innmusic.wav"
    msp.SoundLocation = Music

    Try
       If x = 1 Then
        msp.Play()
       Else
        msp.Stop()
       End If

    Catch ex As Exception
        MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
    End Try

End Sub

如果有任何错误,您将获得有关错误的反馈。

于 2013-07-08T02:53:36.403 回答