Skip to content

Commit

Permalink
fix(Banner::Sound): Catch execption that could be thrown.
Browse files Browse the repository at this point in the history
Fixes SOUNDSWITCH-1C
  • Loading branch information
Belphemur committed May 11, 2021
1 parent e468fe0 commit c7a3059
Showing 1 changed file with 17 additions and 8 deletions.
25 changes: 17 additions & 8 deletions SoundSwitch/Framework/Banner/BannerForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
using System.Windows.Forms;
using NAudio.CoreAudioApi;
using NAudio.Wave;
using Serilog;
using SoundSwitch.Framework.Audio;
using SoundSwitch.Model;
using Timer = System.Windows.Forms.Timer;
Expand Down Expand Up @@ -113,14 +114,22 @@ internal void SetData(BannerData data)
private void PrepareSound(BannerData data)
{
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);
Task.Factory.StartNew(async () =>
{
try
{
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);
}
catch (Exception e)
{
Log.Warning(e, "Issue while playing {sound}", data.SoundFile.FilePath);
}
}, cancellationTokenSource.Token);
}

Expand Down

0 comments on commit c7a3059

Please sign in to comment.