开发者

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.

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.

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 Sou开发者_高级运维ndPlayer 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. However, it only support playing one sound at a time. It only support wav, no volume changes, no pausing. 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜