Skip to content

Commit

Permalink
Make sure we iterate on all listed audio sessions.
Browse files Browse the repository at this point in the history
  • Loading branch information
NicolasConstant committed Nov 20, 2015
1 parent b146bb0 commit 7763508
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -25,18 +34,25 @@ private bool IsOutputingSound(string limitToProcess = null)
{
foreach (var session in sessionEnumerator)
{
using (var audioMeterInformation = session.QueryInterface<AudioMeterInformation>())
using (var session2 = session.QueryInterface<AudioSessionControl2>())
try
{
if (limitToProcess != null)
using (var audioMeterInformation = session.QueryInterface<AudioMeterInformation>())
using (var session2 = session.QueryInterface<AudioSessionControl2>())
{
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);
}
}
}
Expand Down
10 changes: 8 additions & 2 deletions SpotifySleepModeStopperGui/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down

0 comments on commit 7763508

Please sign in to comment.