Skip to content

Commit

Permalink
fix(Notification::Custom): Fix issue where the custom sound wasn't pl…
Browse files Browse the repository at this point in the history
…ayed properly.

Fixes #662
  • Loading branch information
Belphemur committed May 23, 2021
1 parent e1a2599 commit 4e23556
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 14 deletions.
10 changes: 7 additions & 3 deletions SoundSwitch/Framework/Banner/BannerForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,24 +113,28 @@ internal void SetData(BannerData data)
/// <param name="data"></param>
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);
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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; }

Expand All @@ -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)
Expand All @@ -69,7 +86,7 @@ public void NotifyProfileChanged(Profile.Profile profile, uint? processId)
{
if (profile.Playback == null)
return;

using var enumerator = new MMDeviceEnumerator();
try
{
Expand All @@ -84,7 +101,6 @@ public void NotifyProfileChanged(Profile.Profile profile, uint? processId)

public void NotifyMuteChanged(string microphoneName, bool newMuteState)
{

}
}
}

0 comments on commit 4e23556

Please sign in to comment.