From ad2a2a5aefe4559a4da892a6a4e705dd8c55e401 Mon Sep 17 00:00:00 2001 From: ZouJin Date: Sun, 7 Apr 2024 15:00:34 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E9=9F=B3=E4=B9=90=E8=AF=BB?= =?UTF-8?q?=E5=8F=96=E5=AE=B9=E9=94=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- VPet-Simulator.Windows/MainWindow.cs | 33 +++++++++++++++++++++++++--- 1 file changed, 30 insertions(+), 3 deletions(-) diff --git a/VPet-Simulator.Windows/MainWindow.cs b/VPet-Simulator.Windows/MainWindow.cs index da67258..f41b5a3 100644 --- a/VPet-Simulator.Windows/MainWindow.cs +++ b/VPet-Simulator.Windows/MainWindow.cs @@ -961,16 +961,43 @@ namespace VPet_Simulator.Windows } } } -#pragma warning disable CS0414 // 字段“MainWindow.AudioPlayingVolumeOK”已被赋值,但从未使用过它的值 private bool? AudioPlayingVolumeOK = null; -#pragma warning restore CS0414 // 字段“MainWindow.AudioPlayingVolumeOK”已被赋值,但从未使用过它的值 /// /// 获得当前系统音乐播放音量 /// public float AudioPlayingVolume() { - try + if (AudioPlayingVolumeOK == null) + {//第一调用检查是否支持 + try + {//后续容错可能是偶发性 + using (var enumerator = new MMDeviceEnumerator()) + { + if (enumerator.HasDefaultAudioEndpoint(DataFlow.Render, Role.Console)) + { + var device = enumerator.GetDefaultAudioEndpoint(DataFlow.Render, Role.Console); + AudioPlayingVolumeOK = true; + return device.AudioMeterInformation.MasterPeakValue; + } + else + { + AudioPlayingVolumeOK = false; + return -1; + } + } + } + catch + { + AudioPlayingVolumeOK = false; + return -1; + } + } + else if (AudioPlayingVolumeOK == false) { + return -1; + } + try + {//后续容错可能是偶发性 using (var enumerator = new MMDeviceEnumerator()) { if (enumerator.HasDefaultAudioEndpoint(DataFlow.Render, Role.Console))