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

前端之家收集整理的这篇文章主要介绍了.net – 确定窗口当前是否正在播放声音前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
所以我一直在思考这个问题一段时间,我无法弄清楚正确的方法是什么.我想确定 Windows是否使用Powershell脚本在特定时间输出声音.我可以确定音频驱动程序是否有错误,但我不能在我的生活中弄清楚系统是否正在播放声音.

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

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

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

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

运行以下代码

  1. Add-Type -TypeDefinition @'
  2. using System;
  3. using System.Runtime.InteropServices;
  4.  
  5. namespace Foo
  6. {
  7. public class Bar
  8. {
  9. public static bool IsWindowsPlayingSound()
  10. {
  11. IMMDeviceEnumerator enumerator = (IMMDeviceEnumerator)(new MMDeviceEnumerator());
  12. IMMDevice speakers = enumerator.GetDefaultAudioEndpoint(EDataFlow.eRender,ERole.eMultimedia);
  13. IAudioMeterInformation meter = (IAudioMeterInformation)speakers.Activate(typeof(IAudioMeterInformation).GUID,IntPtr.Zero);
  14. float value = meter.GetPeakValue();
  15.  
  16. // this is a bit tricky. 0 is the official "no sound" value
  17. // 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)
  18. // so,depending on your context,it is up to you to decide
  19. // if you want to test for 0 or for a small value
  20. return value > 1E-08;
  21. }
  22.  
  23. [ComImport,Guid("BCDE0395-E52F-467C-8E3D-C4579291692E")]
  24. private class MMDeviceEnumerator
  25. {
  26. }
  27.  
  28. private enum EDataFlow
  29. {
  30. eRender,eCapture,eAll,}
  31.  
  32. private enum ERole
  33. {
  34. eConsole,eMultimedia,eCommunications,}
  35.  
  36. [InterfaceType(ComInterfaceType.InterfaceIsIUnknown),Guid("A95664D2-9614-4F35-A746-DE8DB63617E6")]
  37. private interface IMMDeviceEnumerator
  38. {
  39. void NotNeeded();
  40. IMMDevice GetDefaultAudioEndpoint(EDataFlow dataFlow,ERole role);
  41. // the rest is not defined/needed
  42. }
  43.  
  44. [InterfaceType(ComInterfaceType.InterfaceIsIUnknown),Guid("D666063F-1587-4E43-81F1-B948E807363F")]
  45. private interface IMMDevice
  46. {
  47. [return: MarshalAs(UnmanagedType.IUnknown)]
  48. object Activate([MarshalAs(UnmanagedType.LPStruct)] Guid iid,int dwClsCtx,IntPtr pActivationParams);
  49. // the rest is not defined/needed
  50. }
  51.  
  52. [InterfaceType(ComInterfaceType.InterfaceIsIUnknown),Guid("C02216F6-8C67-4B5B-9D00-D008E73E0064")]
  53. private interface IAudioMeterInformation
  54. {
  55. float GetPeakValue();
  56. // the rest is not defined/needed
  57. }
  58. }
  59. }
  60. '@

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

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

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

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

但有一些警告:

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

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

猜你在找的Windows相关文章