如何检查播放的声音何时结束?
我正在使用C#.Net Winforms,我播放声音的代码是:
SoundPlayer s = new SoundPlayer(@"c:\intro.wav");
s.Play();
对于任何需要了解导入的人,它都是
using System.Media;
所以我需要知道声音何时完成播放,然后我需要运行一些代码。我知道我可能会使用计时器,但出于特定原因,我想避免这样做。
先感谢您。
最佳答案
默认情况下,SoundPlayer在单独的线程上运行,但是有一种方法可以在当前线程上播放声音。参见PlaySync
private SoundPlayer Player = new SoundPlayer();
private void loadSoundAsync()
{
// Note: You may need to change the location specified based on
// the location of the sound to be played.
this.Player.SoundLocation = "http://www.tailspintoys.com/sounds/stop.wav";
this.Player.LoadAsync();
}
private void Player_LoadCompleted (
object sender,
System.ComponentModel.AsyncCompletedEventArgs e)
{
if (this.Player.IsLoadCompleted)
{
this.Player.PlaySync();
}
}
关于c# - 检查声音何时结束C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37367104/