gpt4 book ai didi

.net - 确定 Windows 当前是否正在播放声音

转载 作者:可可西里 更新时间:2023-11-01 12:44:40 26 4
gpt4 key购买 nike

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

我查看了 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, 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 上编译的问题。

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

[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, 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 更改为 return value > 0,您将在视频暂停时得到 true。

关于.net - 确定 Windows 当前是否正在播放声音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45422255/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com