Reputation: 5858
I have a Windows Metro app written in c#.
Here is the code I am using to pick a file from a local music library:
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
}
It says that StorageFile
cannot be converted to the System.Uri
used to change the source of MediaElement. How do I make my file became a uri link? It seems that Uri("...")
only accepts String
of where the file location is.
Upvotes: 7
Views: 2263
Reputation: 18803
You have to use a file stream along with SetSource
. From 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);
Upvotes: 8