2013-05-13 91 views
7

我有用c#编写的Windows Metro应用程序。存储文件到系统uri?

这里是我使用从本地音乐库中选择一个文件中的代码:

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不能转换为用于改变MediaElement的源System.Uri。我如何使我的文件成为一个URI链接?看起来Uri("...")只接受文件位置所在的String

回答

8

您必须使用文件流以及SetSource。从How to play a local media file using a MediaElement

var file = await openPicker.PickSingleFileAsync(); 
var stream = await file.OpenAsync(Windows.Storage.FileAccessMode.Read); 
myMediaElement.SetSource(stream, file.ContentType); 
+0

确实很有帮助! – 2013-05-13 20:46:07