<tfoot id='9luRd'></tfoot>
    <bdo id='9luRd'></bdo><ul id='9luRd'></ul>

        <i id='9luRd'><tr id='9luRd'><dt id='9luRd'><q id='9luRd'><span id='9luRd'><b id='9luRd'><form id='9luRd'><ins id='9luRd'></ins><ul id='9luRd'></ul><sub id='9luRd'></sub></form><legend id='9luRd'></legend><bdo id='9luRd'><pre id='9luRd'><center id='9luRd'></center></pre></bdo></b><th id='9luRd'></th></span></q></dt></tr></i><div id='9luRd'><tfoot id='9luRd'></tfoot><dl id='9luRd'><fieldset id='9luRd'></fieldset></dl></div>
      1. <legend id='9luRd'><style id='9luRd'><dir id='9luRd'><q id='9luRd'></q></dir></style></legend>

        <small id='9luRd'></small><noframes id='9luRd'>

        判断windows当前是否正在播放声音

        时间:2023-10-24
      2. <legend id='fD7GY'><style id='fD7GY'><dir id='fD7GY'><q id='fD7GY'></q></dir></style></legend>

            <bdo id='fD7GY'></bdo><ul id='fD7GY'></ul>
          • <small id='fD7GY'></small><noframes id='fD7GY'>

            <i id='fD7GY'><tr id='fD7GY'><dt id='fD7GY'><q id='fD7GY'><span id='fD7GY'><b id='fD7GY'><form id='fD7GY'><ins id='fD7GY'></ins><ul id='fD7GY'></ul><sub id='fD7GY'></sub></form><legend id='fD7GY'></legend><bdo id='fD7GY'><pre id='fD7GY'><center id='fD7GY'></center></pre></bdo></b><th id='fD7GY'></th></span></q></dt></tr></i><div id='fD7GY'><tfoot id='fD7GY'></tfoot><dl id='fD7GY'><fieldset id='fD7GY'></fieldset></dl></div>

              <tfoot id='fD7GY'></tfoot>
                  <tbody id='fD7GY'></tbody>
                  本文介绍了判断windows当前是否正在播放声音的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  所以我一直在思考这个问题一段时间,但我无法弄清楚解决这个问题的正确方法是什么.我想确定 Windows 是否在某个时间使用 Powershell 脚本输出声音.我可以确定音频驱动程序是否有错误,但我无法确定系统是否正在播放声音.

                  So I've been pondering on this problem for a while and I can't figure out what the right way to go about this is. I want to determine if Windows is outputting sound at a certain time using a Powershell script. I can determine whether or not the audio driver has an error, but I cannot for the life of me figure out if the system is playing sound.

                  我查看了 System.Media.NET 类,里面的三个类都与播放声音或操纵系统声音有关.

                  I looked at the .NET class for System.Media and the three classes inside all had to do with playing sound or manipulating the system sounds.

                  我不是要求为我编写代码,我只需要知道从哪里开始检查 windows 系统当前是否正在播放声音.

                  I'm not asking for code to be written for me, I just need to know where to start to check if the windows system is currently playing sound.

                  我有一个声音监视器,它在 Node.js 平台上持续监控声音,当它失去声音时,它会向我发送一条文本.好吧,我还希望它检查它所连接的所有系统,看看故障在哪里.这就是为什么我想看看windows电脑是否在播放声音.

                  I have a sound monitor that is continuously monitoring sound on the Node.js platform and when it loses sound it sends me a text. Well, I also want it to go through all the systems its hooked up to and see where the fault lies. That's why I want to see whether or not the windows computer is playing sound.

                  推荐答案

                  下面是如何使用 Simon Mourier 提供的代码.

                  Here's how to use the code that Simon Mourier provided.

                  运行以下代码:

                  Add-Type -TypeDefinition @'
                  using System;
                  using System.Runtime.InteropServices;
                  
                  namespace Foo
                  {
                      public class Bar
                      {
                          public static bool IsWindowsPlayingSound()
                          {
                              IMMDeviceEnumerator enumerator = (IMMDeviceEnumerator)(new MMDeviceEnumerator());
                              IMMDevice speakers = enumerator.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eMultimedia);
                              IAudioMeterInformation meter = (IAudioMeterInformation)speakers.Activate(typeof(IAudioMeterInformation).GUID, 0, IntPtr.Zero);
                              float value = meter.GetPeakValue();
                  
                              // this is a bit tricky. 0 is the official "no sound" value
                              // but for example, if you open a video and plays/stops with it (w/o killing the app/window/stream),
                              // the value will not be zero, but something really small (around 1E-09)
                              // so, depending on your context, it is up to you to decide
                              // if you want to test for 0 or for a small value
                              return value > 1E-08;
                          }
                  
                          [ComImport, Guid("BCDE0395-E52F-467C-8E3D-C4579291692E")]
                          private class MMDeviceEnumerator
                          {
                          }
                  
                          private enum EDataFlow
                          {
                              eRender,
                              eCapture,
                              eAll,
                          }
                  
                          private enum ERole
                          {
                              eConsole,
                              eMultimedia,
                              eCommunications,
                          }
                  
                          [InterfaceType(ComInterfaceType.InterfaceIsIUnknown), Guid("A95664D2-9614-4F35-A746-DE8DB63617E6")]
                          private interface IMMDeviceEnumerator
                          {
                              void NotNeeded();
                              IMMDevice GetDefaultAudioEndpoint(EDataFlow dataFlow, ERole role);
                              // the rest is not defined/needed
                          }
                  
                          [InterfaceType(ComInterfaceType.InterfaceIsIUnknown), Guid("D666063F-1587-4E43-81F1-B948E807363F")]
                          private interface IMMDevice
                          {
                              [return: MarshalAs(UnmanagedType.IUnknown)]
                              object Activate([MarshalAs(UnmanagedType.LPStruct)] Guid iid, int dwClsCtx, IntPtr pActivationParams);
                              // the rest is not defined/needed
                          }
                  
                          [InterfaceType(ComInterfaceType.InterfaceIsIUnknown), Guid("C02216F6-8C67-4B5B-9D00-D008E73E0064")]
                          private interface IAudioMeterInformation
                          {
                              float GetPeakValue();
                              // the rest is not defined/needed
                          }
                      }
                  }
                  '@
                  

                  我替换了所有 var 类型,因为这似乎解决了代码无法在 PowerShell 版本 2 上编译的问题.

                  I replaced all var types as that seems to fix the issue with the code not compiling on PowerShell version 2.

                  加载后,您可以像这样检查状态:

                  Once loaded you can check the state like so:

                  [Foo.Bar]::IsWindowsPlayingSound()
                  True or False
                  

                  我已经在 PowerShell 5.1 上使用 Windows 10 1703 进行了测试

                  I've tested this working with Windows 10 1703 on PowerShell 5.1

                  但有一些警告:

                  this is a bit tricky. 0 is the official "no sound" value
                  but for example, if you open a video and plays/stops with it (w/o killing the app/window/stream),
                  the value will not be zero, but something really small (around 1E-09)
                  so, depending on your context, it is up to you to decide
                  if you want to test for 0 or for a small value
                  

                  所以如果你改变返回值>1E-08返回值>0当视频暂停时你会得到真实的.

                  So if you change return value > 1E-08 to return value > 0 you will get true when a video is paused.

                  这篇关于判断windows当前是否正在播放声音的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:Screen.AllScreen 没有给出正确的监视器计数 下一篇:C# 开发中可能出现的 64 位操作系统问题

                  相关文章

                    <small id='PRPnM'></small><noframes id='PRPnM'>

                    1. <legend id='PRPnM'><style id='PRPnM'><dir id='PRPnM'><q id='PRPnM'></q></dir></style></legend>
                    2. <tfoot id='PRPnM'></tfoot>

                      • <bdo id='PRPnM'></bdo><ul id='PRPnM'></ul>
                      <i id='PRPnM'><tr id='PRPnM'><dt id='PRPnM'><q id='PRPnM'><span id='PRPnM'><b id='PRPnM'><form id='PRPnM'><ins id='PRPnM'></ins><ul id='PRPnM'></ul><sub id='PRPnM'></sub></form><legend id='PRPnM'></legend><bdo id='PRPnM'><pre id='PRPnM'><center id='PRPnM'></center></pre></bdo></b><th id='PRPnM'></th></span></q></dt></tr></i><div id='PRPnM'><tfoot id='PRPnM'></tfoot><dl id='PRPnM'><fieldset id='PRPnM'></fieldset></dl></div>