7

我有一个用 c# 编写的 Windows Metro 应用程序。

这是我用来从本地音乐库中选择文件的代码:

FileOpenPicker openPicker = new FileOpenPicker();
openPicker.ViewMode = PickerViewMode.Thumbnail;
openPicker.SuggestedStartLocation = PickerLocationId.MusicLibrary;
openPicker.FileTypeFilter.Add(".mp3");
openPicker.FileTypeFilter.Add(".wav");

StorageFile file = await openPicker.PickSingleFileAsync();
if (file != null)
{
   myMediaElement.Source = file; //error here
}
else
{
    //error here
}

它说StorageFile不能转换为System.Uri用于更改 MediaElement 的来源。如何使我的文件成为 uri 链接?似乎Uri("...")只接受String文件位置的位置。

4

1 回答 1

8

您必须使用文件流以及SetSource. 从如何使用 MediaElement 播放本地媒体文件

var file = await openPicker.PickSingleFileAsync();
var stream = await file.OpenAsync(Windows.Storage.FileAccessMode.Read);
myMediaElement.SetSource(stream, file.ContentType);
于 2013-05-13T20:34:45.590 回答