Derik Whittaker



Playing a embedded media file (mp3) in WinRT (not via Xaml)

When building WinRT applications it is going to be pretty common to want to play some sort of embedded audio file.  It may also be common to want to play this file but via a ViewModel (or simply not in a view).  In this post we are going to take a quick look at how to play an MP3 file by using the MediaElement control, but we will NOT be doing this via XAML, we will be doing via straight up code.  The reason I am not using XAML here is because i don’t want to limit the places or times I can play audio, also I want to trigger the playing of the file via my ViewModel.

Before we get coding lets assume we have an audio file embedded in our application under the /Assests/SoundFiles folder and the file name is ding.mp3


private MediaElement _mediaElement;
public async Task Play( string fileName)
    var packageLocation = Windows.ApplicationModel.Package.Current.InstalledLocation;
    var assetsFolder = await packageLocation.GetFolderAsync("assets");
    var soundsFolder = await assetsFolder.GetFolderAsync("soundFiles");
    var myAudio = await soundsFolder.GetFileAsync(fileName); // would be ding.mp3
    _mediaElement = new MediaElement();
    var stream = await myAudio.OpenAsync(FileAccessMode.Read);
    _mediaElement.SetSource(stream, myAudio.ContentType);


In the code above you can see that we are doing the following things.

  1. Getting the StorageFile reference to Ding.mp3
  2. Creating a new instance of the MediaElement (this is class level because I found the audio would not play if it were a local instance)
  3. Open the StorageFile and get the Stream reference of the file
  4. Set the Stream reference as the source for the MediaElement
  5. Call Play

Now in theory you should be able to set the ‘source’ property to the file path, but i was not able to get this to work.  This may have been my error or could be an issue with the MediaElement. 

As you can see this is not too complicated and requires almost no code

Till next time,

Posted 07-14-2012 12:39 PM by Derik Whittaker
Filed under: , ,



Will Duff wrote re: Playing a embedded media file (mp3) in WinRT (not via Xaml)
on 07-19-2012 11:48 AM

You can get the audio file directly by doing:

var myAudio = Windows.Storage.StorageFile.GetFileFromPathAsync("ms-appx:///Assets/SoundFiles/ding.mp3");

Note that there are three slashes after ms-appx. The first two slashes are part of the protocol (e.g. ms-appx://) and the third slash makes it relative to the root of your appx installation directory. More information is at

Daniel wrote re: Playing a embedded media file (mp3) in WinRT (not via Xaml)
on 08-07-2012 3:37 PM

I tried playing an mp3 file from remote url using Media Element and in background mode, it starts auto play at time and also does not work as expected. Any code sample.

About The CodeBetter.Com Blog Network
CodeBetter.Com FAQ

Our Mission

Advertisers should contact Brendan

Google Reader or Homepage Latest Items
Add to My Yahoo!
Subscribe with Bloglines
Subscribe in NewsGator Online
Subscribe with myFeedster
Add to My AOL
Furl Latest Items
Subscribe in Rojo

Member Projects
DimeCasts.Net - Derik Whittaker

Friends of
Red-Gate Tools For SQL and .NET


SmartInspect .NET Logging
NGEDIT: ViEmu and Codekana
NHibernate Profiler
Balsamiq Mockups
JetBrains - ReSharper
Web Sequence Diagrams
Ducksboard<-- NEW Friend!


Site Copyright © 2007 CodeBetter.Com
Content Copyright Individual Bloggers


Community Server (Commercial Edition)