【问题标题】:How to know if a sound is playing using c#?如何知道是否正在使用 c# 播放声音?
【发布时间】:2014-07-12 20:00:20
【问题描述】:

我有一个 TTS 程序(第三方),我编写了一个使用该程序的 c# 应用程序。 (输入我的应用程序并按下按钮移动鼠标并单击第三方应用程序)。

我需要知道演讲是否结束。关于如何确定声卡是否播放任何声音有什么想法吗?

【问题讨论】:

    标签: c# audio


    【解决方案1】:

    我按照 this power-shell script. 使用 C# 成功地做到了,你只需要删除第一行和最后一行,就可以从这个 power-shell 脚本中创建一个 C# 类

    【讨论】:

      【解决方案2】:

      您可以使用 CSCore。
      在这里下载 -> https://github.com/filoe/cscore

      将这些行粘贴到控制台项目中。

      using System;
      using CSCore.CoreAudioAPI;
      
      namespace AudioDetector
      {
          class Program
          {
              static void Main(string[] args)
              {
                  Console.WriteLine(IsAudioPlaying(GetDefaultRenderDevice()));
                  Console.ReadLine();
              }
      
              public static MMDevice GetDefaultRenderDevice()
              {
                  using (var enumerator = new MMDeviceEnumerator())
                  {
                      return enumerator.GetDefaultAudioEndpoint(DataFlow.Render, Role.Console);
                  }
              }
      
              public static bool IsAudioPlaying(MMDevice device)
              {
                  using (var meter = AudioMeterInformation.FromDevice(device))
                  {
                      return meter.PeakValue > 0;
                  }
              }
          }
      }
      

      在 YouTube、音乐播放器等上播放音乐...
      运行程序。
      如果当前正在播放音频,它会自动通知(true/false)。

      【讨论】:

      • 但是可以看到哪个应用程序正在播放音频吗?
      【解决方案3】:

      您可以检查应用程序是否发出声音。看看这个:Getting individual windows application current volume output level as visualized in audio Mixer

      【讨论】:

        【解决方案4】:

        您可以通过使用 Direct X 的包装器来做到这一点。有很多示例,只需 google 即可。例如,C# 代码可以在herehere 找到。

        【讨论】:

        • 有一大堆关于仅链接答案的元讨论 - meta.stackexchange.com/questions/8231/…。就目前而言,这很糟糕。
        • 好吧。该方法的问题是您正在检查整个系统的主音量峰值。例如,如果从另一个应用程序播放音乐,此方法将失败。
        猜你喜欢
        • 1970-01-01
        • 2019-06-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多