[英]Why doesn't Media.SoundPlayer.Play() actually start a two new threads, when called twice?
I'm writing a simple little program in VB.NET that has to play two sounds - at the same time. 我正在VB.NET中编写一个简单的小程序,该程序必须同时播放两种声音。
The problem is, that when I call SoundPlayer.Play()
twice in immediate succesion, it seems that the latter call sorts of "takes over" the thread that is created by the first call, instead of creating a new one, like it says it should do in the docs . 问题是,当我立即成功两次调用SoundPlayer.Play()
,似乎后者调用SoundPlayer.Play()
“接管”了第一次调用创建的线程,而不是像说的那样创建新线程它应该在文档中执行 。
Any idea what I'm doing wrong? 知道我在做什么错吗?
I'm using the following code: 我正在使用以下代码:
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
It says in the docs that is will play in a background thread, not that each sound will be played using a seperate thread. 它说在文档中将在后台线程中播放,而不是每个声音都将在单独的线程中播放。 This is so it won't freeze the GUI while playing the sound, so it assures that it will not be played on the GUI thread. 这样一来,在播放声音时它不会冻结GUI,从而确保它不会在GUI线程上播放。 However, it only support playing one sound at a time. 但是,它仅支持一次播放一种声音。 It only support wav, no volume changes, no pausing. 它仅支持wav,无音量变化,无暂停。 So it supports only the very basics like playing a short sound for error, warning etc. 因此,它仅支持非常基本的功能,例如播放简短的错误,警告等声音。
If you need more functionality than that (and you usually do), check out the MediaPlayer
class instead. 如果您需要的功能更多(通常需要这样做),请改用MediaPlayer
类。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.