From 74d2ff9e897743aec2feebd225f00ea197b46c00 Mon Sep 17 00:00:00 2001 From: ZouJin Date: Tue, 15 Aug 2023 16:09:48 +1000 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=20CoreAudioAPI?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- VPet-Simulator.Windows/MainWindow.cs | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/VPet-Simulator.Windows/MainWindow.cs b/VPet-Simulator.Windows/MainWindow.cs index 7f7303d..ec7675c 100644 --- a/VPet-Simulator.Windows/MainWindow.cs +++ b/VPet-Simulator.Windows/MainWindow.cs @@ -477,7 +477,7 @@ namespace VPet_Simulator.Windows public float AudioPlayingVolume() { if (AudioPlayingVolumeOK == null) - { + {//第一调用检查是否支持 try { float vol = 0; @@ -499,13 +499,20 @@ namespace VPet_Simulator.Windows { return -1; } - using (var enumerator = new MMDeviceEnumerator()) - { - using (var meter = AudioMeterInformation.FromDevice(enumerator.GetDefaultAudioEndpoint(DataFlow.Render, Role.Multimedia))) + try + {//后续容错可能是偶发性 + using (var enumerator = new MMDeviceEnumerator()) { - return meter.GetPeakValue(); + using (var meter = AudioMeterInformation.FromDevice(enumerator.GetDefaultAudioEndpoint(DataFlow.Render, Role.Multimedia))) + { + return meter.GetPeakValue(); + } } } + catch + { + return -1; + } } /// /// 音乐检测器