-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNotificationSoundService.cs
More file actions
48 lines (40 loc) · 1.2 KB
/
NotificationSoundService.cs
File metadata and controls
48 lines (40 loc) · 1.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
using System.Windows.Media;
namespace ToastDesk;
public sealed class NotificationSoundService : IDisposable
{
private readonly NotificationStore store;
private readonly AppSettings settings;
private readonly MediaPlayer player = new();
public NotificationSoundService(NotificationStore store, AppSettings settings)
{
this.store = store;
this.settings = settings;
store.NotificationAdded += OnNotificationAdded;
}
public void Dispose()
{
store.NotificationAdded -= OnNotificationAdded;
player.Close();
}
public bool PlayPreview()
{
return PlayConfiguredSound();
}
private void OnNotificationAdded(object? sender, AppNotification notification)
{
PlayConfiguredSound();
}
private bool PlayConfiguredSound()
{
var soundPath = NotificationSoundCatalog.ResolveSoundPath(settings);
if (string.IsNullOrWhiteSpace(soundPath))
{
return false;
}
player.Stop();
player.Open(new Uri(soundPath, UriKind.Absolute));
player.Volume = Math.Clamp(settings.NotificationSoundVolume / 100.0, 0, 1);
player.Play();
return true;
}
}