diff --git a/SoundSwitch/Framework/Banner/BannerData.cs b/SoundSwitch/Framework/Banner/BannerData.cs index 154154c9f4..9e10490a21 100644 --- a/SoundSwitch/Framework/Banner/BannerData.cs +++ b/SoundSwitch/Framework/Banner/BannerData.cs @@ -12,13 +12,9 @@ * GNU General Public License for more details. ********************************************************************/ -using System; -using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; using System.Drawing; -using System.IO; -using System.Linq; -using System.Text; -using System.Threading.Tasks; +using NAudio.CoreAudioApi; using SoundSwitch.Framework.Audio; namespace SoundSwitch.Framework.Banner @@ -46,7 +42,14 @@ public class BannerData /// /// Gets/sets the path for a wav sound to be playedc during the notification, this is optional. /// + [AllowNull] public CachedSound SoundFile { get; internal set; } + + /// + /// On what device to play the + /// + [AllowNull] + public MMDevice CurrentDevice { get; internal set; } /// /// Set the priority of the notification diff --git a/SoundSwitch/Framework/Banner/BannerForm.Designer.cs b/SoundSwitch/Framework/Banner/BannerForm.Designer.cs index 88cea84a16..9b284b9d59 100644 --- a/SoundSwitch/Framework/Banner/BannerForm.Designer.cs +++ b/SoundSwitch/Framework/Banner/BannerForm.Designer.cs @@ -7,19 +7,6 @@ partial class BannerForm /// private System.ComponentModel.IContainer components = null; - /// - /// Clean up any resources being used. - /// - /// true if managed resources should be disposed; otherwise, false. - protected override void Dispose(bool disposing) - { - if (disposing && (components != null)) - { - components.Dispose(); - } - base.Dispose(disposing); - } - #region Windows Form Designer generated code /// diff --git a/SoundSwitch/Framework/Banner/BannerForm.cs b/SoundSwitch/Framework/Banner/BannerForm.cs index 757977d9f6..6ac60ea4a6 100644 --- a/SoundSwitch/Framework/Banner/BannerForm.cs +++ b/SoundSwitch/Framework/Banner/BannerForm.cs @@ -17,6 +17,7 @@ using System.Threading; using System.Threading.Tasks; using System.Windows.Forms; +using NAudio.CoreAudioApi; using NAudio.Wave; using SoundSwitch.Framework.Audio; using SoundSwitch.Model; @@ -32,9 +33,7 @@ public partial class BannerForm : Form private Timer _timerHide; private bool _hiding; private BannerData _currentData; - - - private WasapiOut _player; + private CancellationTokenSource _cancellationTokenSource = new(); /// /// Constructor for the class @@ -113,18 +112,16 @@ internal void SetData(BannerData data) /// private void PrepareSound(BannerData data) { - _player = new WasapiOut(); - var task = new Task(() => - { - using var waveStream = new CachedSoundWaveStream(data.SoundFile); - _player.Init(waveStream); - _player.Play(); - while (_player.PlaybackState == PlaybackState.Playing) - { - Thread.Sleep(500); - } - }); - task.Start(); + var cancellationTokenSource = CancellationTokenSource.CreateLinkedTokenSource(_cancellationTokenSource.Token); + Task.Factory.StartNew(async () => { + 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.Play(); + await Task.Delay(-1, cancellationTokenSource.Token); + }, cancellationTokenSource.Token); } /// @@ -132,14 +129,27 @@ private void PrepareSound(BannerData data) /// private void DestroySound() { - if (_player != null) + _cancellationTokenSource.Cancel(); + _cancellationTokenSource.Dispose(); + _cancellationTokenSource = new(); + } + + /// + /// Clean up any resources being used. + /// + /// true if managed resources should be disposed; otherwise, false. + protected override void Dispose(bool disposing) + { + if (disposing) { - _player.Stop(); - _player.Dispose(); - _player = null; + _timerHide?.Dispose(); + _cancellationTokenSource?.Dispose(); + components?.Dispose(); } + + base.Dispose(disposing); } - + /// /// Event handler for the "hiding" timer. @@ -176,4 +186,4 @@ private async void FadeOut() } } } -} +} \ No newline at end of file diff --git a/SoundSwitch/Framework/NotificationManager/Notification/NotificationBanner.cs b/SoundSwitch/Framework/NotificationManager/Notification/NotificationBanner.cs index 3584d0eaeb..7316bda7cd 100644 --- a/SoundSwitch/Framework/NotificationManager/Notification/NotificationBanner.cs +++ b/SoundSwitch/Framework/NotificationManager/Notification/NotificationBanner.cs @@ -89,6 +89,7 @@ public void NotifyDefaultChanged(MMDevice audioDevice) if (Configuration.CustomSound != null && File.Exists(Configuration.CustomSound.FilePath)) { toastData.SoundFile = Configuration.CustomSound; + toastData.CurrentDevice = audioDevice; } toastData.Title = audioDevice.DataFlow switch