From 4e235560dba18a2093822e8f7ca5b7efdcfbb6ad Mon Sep 17 00:00:00 2001 From: Antoine Aflalo Date: Sun, 23 May 2021 10:04:29 -0400 Subject: [PATCH] fix(Notification::Custom): Fix issue where the custom sound wasn't played properly. Fixes #662 --- SoundSwitch/Framework/Banner/BannerForm.cs | 10 +++-- .../Notification/NotificationCustom.cs | 38 +++++++++++++------ 2 files changed, 34 insertions(+), 14 deletions(-) diff --git a/SoundSwitch/Framework/Banner/BannerForm.cs b/SoundSwitch/Framework/Banner/BannerForm.cs index 512f113f17..aae8a4f35a 100644 --- a/SoundSwitch/Framework/Banner/BannerForm.cs +++ b/SoundSwitch/Framework/Banner/BannerForm.cs @@ -113,24 +113,28 @@ internal void SetData(BannerData data) /// private void PrepareSound(BannerData data) { - var cancellationTokenSource = CancellationTokenSource.CreateLinkedTokenSource(_cancellationTokenSource.Token); Task.Factory.StartNew(async () => { try { + using var cancellationTokenSource = CancellationTokenSource.CreateLinkedTokenSource(_cancellationTokenSource.Token); using var player = data.CurrentDevice == null ? new WasapiOut() : new WasapiOut(data.CurrentDevice, AudioClientShareMode.Shared, true, 200); await using var waveStream = new CachedSoundWaveStream(data.SoundFile); player.Init(waveStream); - player.PlaybackStopped += (_, _) => cancellationTokenSource.Cancel(); + player.PlaybackStopped += (_, _) => cancellationTokenSource.CancelAfter(TimeSpan.FromMilliseconds(750)); player.Play(); await Task.Delay(-1, cancellationTokenSource.Token); } + catch (TaskCanceledException) + { + //Ignored + } catch (Exception e) { Log.Warning(e, "Issue while playing {sound}", data.SoundFile.FilePath); } - }, cancellationTokenSource.Token); + }, _cancellationTokenSource.Token); } /// diff --git a/SoundSwitch/Framework/NotificationManager/Notification/NotificationCustom.cs b/SoundSwitch/Framework/NotificationManager/Notification/NotificationCustom.cs index 8721a0ff55..26af64e6c5 100644 --- a/SoundSwitch/Framework/NotificationManager/Notification/NotificationCustom.cs +++ b/SoundSwitch/Framework/NotificationManager/Notification/NotificationCustom.cs @@ -17,6 +17,7 @@ using System.Threading.Tasks; using NAudio.CoreAudioApi; using NAudio.Wave; +using Serilog; using SoundSwitch.Framework.Audio; using SoundSwitch.Framework.NotificationManager.Notification.Configuration; using SoundSwitch.Localization; @@ -25,8 +26,9 @@ namespace SoundSwitch.Framework.NotificationManager.Notification { public class NotificationCustom : INotification { + private CancellationTokenSource _cancellationTokenSource; public NotificationTypeEnum TypeEnum => NotificationTypeEnum.CustomNotification; - public string Label => SettingsStrings.notificationOptionCustomized; + public string Label => SettingsStrings.notificationOptionCustomized; public INotificationConfiguration Configuration { get; set; } @@ -35,17 +37,32 @@ public void NotifyDefaultChanged(MMDevice audioDevice) { if (audioDevice.DataFlow != DataFlow.Render) return; - Task.Factory.StartNew(() => + + _cancellationTokenSource?.Cancel(); + _cancellationTokenSource?.Dispose(); + _cancellationTokenSource = new CancellationTokenSource(); + Task.Factory.StartNew(async () => { - using var output = new WasapiOut(audioDevice, AudioClientShareMode.Shared, true, 10); - using var waveStream = new CachedSoundWaveStream(Configuration.CustomSound); - output.Init(waveStream); - output.Play(); - while (output.PlaybackState == PlaybackState.Playing) + try { - Thread.Sleep(500); + using var cts = CancellationTokenSource.CreateLinkedTokenSource(_cancellationTokenSource.Token); + using var player = new WasapiOut(audioDevice, AudioClientShareMode.Shared, true, 200); + await using var waveStream = new CachedSoundWaveStream(Configuration.CustomSound); + player.Init(waveStream); + + player.PlaybackStopped += (_, _) => cts.CancelAfter(TimeSpan.FromMilliseconds(750)); + player.Play(); + await Task.Delay(-1, cts.Token); } - }); + catch (TaskCanceledException) + { + //Ignored + } + catch (Exception e) + { + Log.Warning(e, "Issue while playing {sound}", Configuration.CustomSound.FilePath); + } + }, _cancellationTokenSource.Token); } public void OnSoundChanged(CachedSound newSound) @@ -69,7 +86,7 @@ public void NotifyProfileChanged(Profile.Profile profile, uint? processId) { if (profile.Playback == null) return; - + using var enumerator = new MMDeviceEnumerator(); try { @@ -84,7 +101,6 @@ public void NotifyProfileChanged(Profile.Profile profile, uint? processId) public void NotifyMuteChanged(string microphoneName, bool newMuteState) { - } } } \ No newline at end of file