How can I play mp3 files stored in my Windows Phone 8.1 solution?

让人想犯罪 __ 提交于 2019-12-11 09:08:32

问题


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?


回答1:


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




回答2:


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();



回答3:


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 "\".



来源:https://stackoverflow.com/questions/24957852/how-can-i-play-mp3-files-stored-in-my-windows-phone-8-1-solution

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!