From a37567e8c2604495ea4ee6d0d4089a68082c5ef9 Mon Sep 17 00:00:00 2001 From: mike-ward Date: Sat, 26 Nov 2022 10:44:12 -0600 Subject: [PATCH] refactor sound playing --- src/tweetz.core/Services/SoundService.cs | 24 ++++++++++++++++++++++ src/tweetz.core/Views/TimelineView.xaml.cs | 14 +++++-------- 2 files changed, 29 insertions(+), 9 deletions(-) create mode 100644 src/tweetz.core/Services/SoundService.cs diff --git a/src/tweetz.core/Services/SoundService.cs b/src/tweetz.core/Services/SoundService.cs new file mode 100644 index 0000000..59dd405 --- /dev/null +++ b/src/tweetz.core/Services/SoundService.cs @@ -0,0 +1,24 @@ +using System; +using System.Media; +using System.Windows; + +namespace tweetz.core.Services +{ + public static class SoundService + { + public static void PlayDefaultNotifySound() + { + using var notifySound = Application.GetResourceStream(new Uri("pack://application:,,,/notify.wav"))!.Stream; + using var player = new SoundPlayer(notifySound); + player.PlaySync(); + TraceService.Message("Played default sound"); + } + + public static void PlayFromSoundSource(string filename) + { + using var player2 = new SoundPlayer(filename); + player2.PlaySync(); + TraceService.Message("Played from sound source"); + } + } +} \ No newline at end of file diff --git a/src/tweetz.core/Views/TimelineView.xaml.cs b/src/tweetz.core/Views/TimelineView.xaml.cs index dd76ea1..ef93b91 100644 --- a/src/tweetz.core/Views/TimelineView.xaml.cs +++ b/src/tweetz.core/Views/TimelineView.xaml.cs @@ -1,6 +1,4 @@ using System; -using System.IO; -using System.Media; using System.Windows; using System.Windows.Controls; using System.Windows.Media; @@ -58,8 +56,6 @@ private void UpdateAppIcon() ((MainWindow)Application.Current.MainWindow!).UpdateAppIcon(isMinimized); } - private static readonly Stream notifySound = Application.GetResourceStream(new Uri("pack://application:,,,/notify.wav"))!.Stream; - private void PlayNotifySound() { try @@ -68,12 +64,12 @@ private void PlayNotifySound() { if (string.IsNullOrWhiteSpace(Settings.NotifySoundSource)) { - new SoundPlayer(notifySound).Play(); - return; + SoundService.PlayDefaultNotifySound(); + } + else + { + SoundService.PlayFromSoundSource(Settings.NotifySoundSource); } - - var player = new SoundPlayer(Settings.NotifySoundSource); - player.Play(); } } catch (Exception ex)