我正在使用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
答案 0 :(得分: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
如果有错误,您将获得有关错误的反馈。