Skip to content

Commit

Permalink
Better closing, change sensibility to detect music stopping playing m…
Browse files Browse the repository at this point in the history
…ore reliably
  • Loading branch information
NicolasConstant committed Nov 13, 2015
1 parent 8a523fe commit b146bb0
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ private bool IsOutputingSound(string limitToProcess = null)
}

var peakValue = Math.Abs(audioMeterInformation.GetPeakValue());
if (peakValue >= 5E-10) return true;
if (peakValue > 6E-9) return true;
}
}
}
Expand Down
8 changes: 7 additions & 1 deletion SpotifySleepModeStopper/Domain/SpotifySaveModeStopper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ public class SpotifySaveModeStopper
private CancellationTokenSource _cancellationTokenSource;
private Task _analyst;

#if DEBUG
TimeSpan _checkInterval = TimeSpan.FromSeconds(5);
#else
TimeSpan _checkInterval = TimeSpan.FromSeconds(20);
#endif

public SpotifySaveModeStopper(IMessageDisplayer messageDisplayer, IPreventSleepScreen preventSleepScreen, ISoundAnalyser soundAnalyser, IAppStatusReporting appState1)
{
_messageDisplayer = messageDisplayer;
Expand All @@ -37,7 +43,7 @@ public void StartListening()
_cancellationTokenSource?.Cancel();
_cancellationTokenSource = new CancellationTokenSource();
_analyst = Repeat.Interval(
TimeSpan.FromSeconds(30),
_checkInterval,
AnalyseSpotifyStatus, _cancellationTokenSource.Token);
}

Expand Down
39 changes: 16 additions & 23 deletions SpotifySleepModeStopperGui/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,20 +1,9 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Forms;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;
using SpotifyTools.Domain;
using SpotifyTools.Domain.AppStatesManagement;
Expand All @@ -39,30 +28,29 @@ public MainWindow()
{
InitializeComponent();

#region Events Subscription
Closing += OnClosing;
#endregion

#region Init Tray Icon
WindowState = WindowState.Minimized;
Visibility = Visibility.Hidden;
ShowInTaskbar = false;

_notifyIcon = new NotifyIcon();
using (
var stream =
Assembly.GetExecutingAssembly().GetManifestResourceStream("SpotifySleepModeStopperGui.music.ico"))
using (var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("SpotifySleepModeStopperGui.music.ico"))
{
_notPlayingIcon = new Icon(stream);
}
using (
var stream =
Assembly.GetExecutingAssembly()
.GetManifestResourceStream("SpotifySleepModeStopperGui.music_playing.ico"))
using ( var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("SpotifySleepModeStopperGui.music_playing.ico"))
{
_playingIcon = new Icon(stream);
}
SetNotPlaying();
_notifyIcon.Visible = true;

var contextMenu = new System.Windows.Forms.ContextMenu();
var menuItem = new System.Windows.Forms.MenuItem();
var contextMenu = new ContextMenu();
var menuItem = new MenuItem();

contextMenu.MenuItems.AddRange(new[] { menuItem });
menuItem.Index = 0;
Expand All @@ -76,14 +64,19 @@ public MainWindow()
_analyser.StartListening();
}

private void exit_Click(object sender, EventArgs e)
private void OnClosing(object sender, CancelEventArgs e)
{
_analyser.StopListening();

_notifyIcon.Visible = false;
_notifyIcon.Dispose();

Environment.Exit(0);
//Environment.Exit(0);
}

private void exit_Click(object sender, EventArgs e)
{
Application.Current.Shutdown();
}

private void SetNotPlaying()
Expand Down

0 comments on commit b146bb0

Please sign in to comment.