Reputation: 42175
I have a number of mp3 files stored in my solution at the location
/Resources/mp3Files/
In Windows Phone 8 I was able to play these with the following:
var name = track.Item1;
var uri = new Uri("/Resources/mp3Files/sound.mp3", UriKind.Relative);
var song = Song.FromUri(name, uri);
FrameworkDispatcher.Update();
MediaPlayer.Play(song);
However, in Windows Phone 8.1 this doesn't work.
What do I need to do to play mp3 files I have stored in my solution?
Upvotes: 1
Views: 4778
Reputation: 42175
This is what worked:
// get folder app is installed to
var installFolder = Windows.ApplicationModel.Package.Current.InstalledLocation;
// get folders mp3 files are installed to.
var resourcesFolder = await installFolder.GetFolderAsync("Resources");
var mp3FilesFolder = await resourcesFolder.GetFolderAsync("mp3Files");
// open the mp3 file async
var audioFile = await mp3FilesFolder.GetFileAsync("sound.mp3");
var stream = await audioFile.OpenAsync(Windows.Storage.FileAccessMode.Read);
// play dat funky music
MediaElement mediaplayer = new MediaElement();
mediaplayer.SetSource(stream, audioFile.ContentType);
mediaplayer.Play();
Upvotes: 2
Reputation: 720
Here is how one used to open a file in WP8.0 embedded in solution with 'Build Action' 'Content':
Dim fs As IO.FileStream = IO.File.OpenRead("MyFolder/MyFile.txt")
And here is how it is done in WP8.1 Win-RT:
Dim fs As System.IO.Stream = Await Windows.ApplicationModel.Package.Current.InstalledLocation.OpenStreamForReadAsync("MyFolder\MyFile.txt")
Notice how "/" has changed to "\".
Upvotes: 0
Reputation: 13984
You need to use MediaElement in Windows 8.1: http://blogs.msdn.com/b/johnkenn/archive/2013/12/31/supporting-background-audio-in-your-windows-8-1-app.aspx
Playing Audio from a file stored in the Music folder
var audioFile = await KnownFolders.MusicLibrary.GetFileAsync("/Resources/mp3Files/sound.mp3");
var stream = await audioFile.OpenAsync(Windows.Storage.FileAccessMode.Read);
mediaplayer.SetSource(stream, audioFile.ContentType);
Take a look for a whole example: http://msdn.microsoft.com/library/windows/apps/xaml/jj841209.aspx
Upvotes: 2