c# - 检查声音何时结束C#

标签 c# audio

如何检查播放的声音何时结束?
我正在使用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/

相关文章:

c# - 检查机器上的第三方防火墙

c# - 脱机时未分配 Azure 移动服务标识列

android - 从 React Native 中的变量播放音频

c# - 不用 Crystal 报表设计器创建rpt文件

c# - 带有日期参数的 Angular 6 服务调用

c# - 为什么 TagHelper HtmlAttributeName 在 ASP.NET Core 2.2 中未解析

java - 如何在java中重复播放音频文件

python - LED矩阵上的音频可视化器

c++ - 最小化应用程序时停止播放声音和音量

arrays - as3 URLRequest声音存储在数组中吗?