.net – 确定窗口当前是否正在播放声音

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

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

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

我有一个声音监视器,持续监视Node.js平台上的声音,当它失去声音时它会发给我一个文本.好吧,我也想让它通过它所连接的所有系统,看看故障所在.这就是我想看看Windows电脑是否播放声音的原因.

以下是使用Simon Mourier提供的代码方法.

运行以下代码

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,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上没有编译的代码的问题.

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

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

我在PowerShell 5.1上测试了这个与Windows 10 1703一起使用的方法

但有一些警告:

this is a bit tricky. 0 is the official "no sound" value
but for example,the value will not be zero,but something really small (around 1E-09)
so,it is up to you to decide
if you want to test for 0 or for a small value

因此,如果您更改返回值> 1E-08返回值> 0视频暂停时你会得到真实的.

相关文章

Windows2012R2备用域控搭建 前置操作 域控主域控的主dns:自...
主域控角色迁移和夺取(转载) 转载自:http://yupeizhi.blo...
Windows2012R2 NTP时间同步 Windows2012R2里没有了internet时...
Windows注册表操作基础代码 Windows下对注册表进行操作使用的...
黑客常用WinAPI函数整理之前的博客写了很多关于Windows编程的...
一个简单的Windows Socket可复用框架说起网络编程,无非是建...