stefan.s stefan.s - 1 month ago 21
C# Question

How to play an internal sound with BackgroundMediaPlayer from my Universal Windows App

I'm creating a timer in C#. When the timer finishes the user will get a notification.

If my app is gets suspended, I schedule a notification with the

ToastNotificationManager
and in the toast XML set the sound to an internal sound, e.g.:

<audio src='ms-winsoundevent:Notification.Looping.Alarm10' loop='true'/>


However if my app runs in the foreground I don't want to schedule a notification, but just play the sound with the BackgroundMediaPlayer.

Of course I want to use the same alarm sound, as in the notification. But I haven't found a way to address the internal sounds there.

I've found this:

BackgroundMediaPlayer.Current.SetUriSource(new Uri("ms-appx:///Assets/Sounds/alarm01.wav"));
BackgroundMediaPlayer.Current.Play();


But this would require me to copy all system sounds to my app and I don't want to do that, since it would increase the size.

Answer

I've created a demo to reproduce the problem and found that you can use the internal sound directly by using

BackgroundMediaPlayer.Current.SetUriSource(new Uri("ms-winsoundevent:Notification.Looping.Alarm10"));
BackgroundMediaPlayer.Current.Play();

It worked in my UWP Project. I hope it will work in your Project too.

Comments