我正在用VB.NET编写一个简单的小程序,它必须同时播放两种声音。
问题是,当我立即连续两次调用SoundPlayer.Play()
时,似乎后者会调用“接管”第一次调用创建的线程,而不是创建一个新线程,就像它说它应该in the docs。
知道我做错了吗?
我正在使用以下代码:
Private Sub Button_OFD_Sound1_Browse_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_OFD_Sound1_Browse.Click
LoadSound(OFD_Sound1, TextBox_OFD_Sound1_SelectedFile, SoundPlayer_Sound1)
End Sub
Private Sub Button_OFD_Sound2_Browse_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_OFD_Sound2_Browse.Click
LoadSound(OFD_Sound2, TextBox_OFD_Sound2_SelectedFile, SoundPlayer_Sound2)
End Sub
Private Sub Button_PlaySounds_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_PlaySounds.Click
If SoundPlayer_Sound1.IsLoadCompleted And SoundPlayer_Sound2.IsLoadCompleted Then
SoundPlayer_Sound1.Play()
SoundPlayer_Sound2.Play()
Else
MsgBox("Der er ikke indlæst nogen lydfil.", MsgBoxStyle.Exclamation, "Der opstod en fejl")
End If
End Sub
Private Sub LoadSound(ByVal FileSelectorDialog As Windows.Forms.OpenFileDialog, ByVal SelectedFileTextBox As Windows.Forms.TextBox, ByVal SoundPlayer As Media.SoundPlayer)
If FileSelectorDialog.ShowDialog() = Windows.Forms.DialogResult.Cancel Then Return
If IO.File.Exists(FileSelectorDialog.FileName) Then
SoundPlayer.SoundLocation = FileSelectorDialog.FileName
SoundPlayer.Load()
If SoundPlayer.IsLoadCompleted Then
SelectedFileTextBox.Text = FileSelectorDialog.FileName
Else
MsgBox("Den valgte lydfil kunne ikke indlæses. Det skyldes muligvis, at filen ikke er i det understøttede WAVE-format. Prøv at vælge en anden.", MsgBoxStyle.Exclamation, "Der opstod en fejl")
End If
Else
MsgBox("Den valgte fil eksisterer ikke. Vælg en anden fil.", MsgBoxStyle.Exclamation, "Der opstod en fejl")
End If
End Sub
答案 0 :(得分:3)
它在文档中说将在后台线程中播放,而不是每个声音将使用单独的线程播放。这样在播放声音时不会冻结GUI,因此可以确保它不会在GUI线程上播放。但是,它只支持一次播放一个声音。它只支持wav,没有音量变化,没有暂停。所以它只支持非常基础知识,例如播放错误的警告,警告等。
如果您需要更多功能(通常也是如此),请查看MediaPlayer
课程。