如何播放存储在Windows Phone 8.1解决方案中的mp3文件?

时间:2014-07-25 14:16:12

标签: windows-runtime windows-phone-8.1

我在我的解决方案中存储了许多mp3文件

/Resources/mp3Files/

在Windows Phone 8中,我可以使用以下内容播放这些内容:

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

但是,在Windows Phone 8.1中,这不起作用。

播放存储在我的解决方案中的mp3文件需要做些什么?

3 个答案:

答案 0 :(得分:2)

您需要在Windows 8.1中使用MediaElement:http://blogs.msdn.com/b/johnkenn/archive/2013/12/31/supporting-background-audio-in-your-windows-8-1-app.aspx

从存储在“音乐”文件夹中的文件播放音频

var audioFile = await KnownFolders.MusicLibrary.GetFileAsync("/Resources/mp3Files/sound.mp3");

var stream = await audioFile.OpenAsync(Windows.Storage.FileAccessMode.Read);

mediaplayer.SetSource(stream, audioFile.ContentType);

看一下整个例子:http://msdn.microsoft.com/library/windows/apps/xaml/jj841209.aspx

答案 1 :(得分:2)

这是有用的:

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

答案 2 :(得分:0)

以下是使用'Build Action''Content'在解决方案中嵌入的WP8.0中打开文件的方法:

Dim fs As IO.FileStream = IO.File.OpenRead("MyFolder/MyFile.txt")

以下是WP8.1 Win-RT中的完成方式:

Dim fs As System.IO.Stream = Await Windows.ApplicationModel.Package.Current.InstalledLocation.OpenStreamForReadAsync("MyFolder\MyFile.txt")

注意“/”如何变为“\”。