2013-05-13 90 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