Skip to content

Commit

Permalink
refactor sound playing
Browse files Browse the repository at this point in the history
  • Loading branch information
mike-ward committed Nov 26, 2022
1 parent f977393 commit a37567e
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 9 deletions.
24 changes: 24 additions & 0 deletions src/tweetz.core/Services/SoundService.cs
Original file line number Diff line number Diff line change
@@ -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");
}
}
}
14 changes: 5 additions & 9 deletions src/tweetz.core/Views/TimelineView.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System;
using System.IO;
using System.Media;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand Down

0 comments on commit a37567e

Please sign in to comment.