简体   繁体   English

为什么Media.SoundPlayer.Play()实际上没有启动两次调用的两个新线程?

[英]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.

相关问题 为什么停止 IHost 时会调用两次 StopAsync? - Why is StopAsync called twice when stopping IHost? 为什么Process.Threads不包含等待线程? - Why Process.Threads doesn't contain waiting threads? 使用 SoundPlayer 播放多个声音 - Play multiple sounds using SoundPlayer 为什么ValidateValueCallback被调用两次? - Why is ValidateValueCallback called twice? 为什么 htmlagilitypack 在创建新节点时不包含属性? - Why doesn't htmlagilitypack include attribute when creating a new node? 实际上是什么导致 Session_Start 被调用? - What actually causes Session_Start to be called? 为什么PLinq不支持超过63个并行线程? - Why PLinq doesn't support more than 63 parallel threads? 当声明一个类型的变量但实际上没有实例化时,它在.NET中被称为什么? - What is it called in .NET when a variable of a type is declared, but isn't actually instantiated? 为什么不是C#.NET SortedList <T1, T2> 实际上有ElementAt? - Why doesn't C# .NET SortedList<T1, T2> actually have ElementAt? 为什么列表 <T> 。添加和列表 <T> 。删除将元素复制到新数组而不是实际添加和删除? - Why do List<T>.Add and List<T>.Remove copy the elements into a new array instead of actually adding and removing?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM