From 77635087b2a00e0d3864078ec9e8305ba75317c0 Mon Sep 17 00:00:00 2001 From: NicolasConstant Date: Thu, 19 Nov 2015 19:27:23 -0500 Subject: [PATCH] Make sure we iterate on all listed audio sessions. --- .../AudioManagement/CsCoreSoundAnalyser.cs | 34 ++++++++++++++----- SpotifySleepModeStopperGui/MainWindow.xaml.cs | 10 ++++-- 2 files changed, 33 insertions(+), 11 deletions(-) diff --git a/SpotifySleepModeStopper/Domain/AudioManagement/CsCoreSoundAnalyser.cs b/SpotifySleepModeStopper/Domain/AudioManagement/CsCoreSoundAnalyser.cs index 61a4021..ed1c4ae 100644 --- a/SpotifySleepModeStopper/Domain/AudioManagement/CsCoreSoundAnalyser.cs +++ b/SpotifySleepModeStopper/Domain/AudioManagement/CsCoreSoundAnalyser.cs @@ -7,6 +7,15 @@ namespace SpotifyTools.Domain.AudioManagement { public class CsCoreSoundAnalyser : ISoundAnalyser { + private readonly IMessageDisplayer _messageDisplayer; + + #region Ctor + public CsCoreSoundAnalyser(IMessageDisplayer messageDisplayer1) + { + _messageDisplayer = messageDisplayer1; + } + #endregion + public bool IsWindowsOutputingSound() { return IsOutputingSound(); @@ -25,18 +34,25 @@ private bool IsOutputingSound(string limitToProcess = null) { foreach (var session in sessionEnumerator) { - using (var audioMeterInformation = session.QueryInterface()) - using (var session2 = session.QueryInterface()) + try { - if (limitToProcess != null) + using (var audioMeterInformation = session.QueryInterface()) + using (var session2 = session.QueryInterface()) { - var processId = session2.ProcessID; - var name = Process.GetProcessById(processId).ProcessName; - if (name != limitToProcess) continue; - } + if (limitToProcess != null) + { + var processId = session2.ProcessID; + var name = Process.GetProcessById(processId).ProcessName; + if (name != limitToProcess) continue; + } - var peakValue = Math.Abs(audioMeterInformation.GetPeakValue()); - if (peakValue > 6E-9) return true; + var peakValue = Math.Abs(audioMeterInformation.GetPeakValue()); + if (peakValue > 6E-9) return true; + } + } + catch (Exception e) + { + _messageDisplayer.OutputMessage("Exception: " + e.Message + " at "+ e.StackTrace); } } } diff --git a/SpotifySleepModeStopperGui/MainWindow.xaml.cs b/SpotifySleepModeStopperGui/MainWindow.xaml.cs index 62d7986..739ba11 100644 --- a/SpotifySleepModeStopperGui/MainWindow.xaml.cs +++ b/SpotifySleepModeStopperGui/MainWindow.xaml.cs @@ -58,9 +58,15 @@ public MainWindow() menuItem.Click += exit_Click; _notifyIcon.ContextMenu = contextMenu; #endregion - + + #region Poor Man DI var iconChanger = new AppStateReporting(SetPlaying, SetNotPlaying); - _analyser = new SpotifySaveModeStopper(new DummyMessageDisplayer(), new PowerRequestContextHandler(), new CsCoreSoundAnalyser(), iconChanger); + var messageDisplayer = new DummyMessageDisplayer(); + var powerHandler = new PowerRequestContextHandler(); + var soundAnalyser = new CsCoreSoundAnalyser(messageDisplayer); + #endregion + + _analyser = new SpotifySaveModeStopper(messageDisplayer, powerHandler, soundAnalyser, iconChanger); _analyser.StartListening(); }