Are you looking for the fastest and simplest way to play waves and mp3 in your UWP without adding a Medialement to your XAML Tree? This tutorial shows you how use the MediaPlayer API to fire-and-forget audio clips.

Playing Wav and Mp3 in a Universal Windows Application.

I’ve written a lot about playing audio in XAML apps. I’ve used SharpDX, Native DirectX and more recently Audiograph. All this was to avoid having to add a MediaElement to the XAML tree. With XNA we could use the SoundEffect to fire-and-forget audioclips and Windows 8 never had a simple alternative to that.

UWP has come a long way since Windows 8 and here’s how you can use the MediaPlayer API to play wave and mp3 files.

Download the demo project here: https://github.com/madeinouweland/windows-10-uwp-wav-mp3-mediaplayer

mediaplayer

UWP MediaPlayer

The MediaPlayer is part of UWP since v10.0.10240.0 and can be used like this:

private void OnButtonWavClick(object sender, RoutedEventArgs e)
{
Play("ms-appx:///matrix.wav");
}

private void Play(string fileName)
{
var mediaPlayer = new MediaPlayer();
mediaPlayer.Source = MediaSource.CreateFromUri(new Uri($"{fileName}", UriKind.RelativeOrAbsolute));
mediaPlayer.Play();
}

Download the demo project with audio examples here: https://github.com/madeinouweland/windows-10-uwp-wav-mp3-mediaplayer

Written by Loek van den Ouweland on 2017-09-20.
Questions regarding this artice? You can send them to the address below.