Storage file to System uri?
Asked Answered
E

1

7

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.

Ennis answered 13/5, 2013 at 20:0 Comment(0)
W
8

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);
Woolgrower answered 13/5, 2013 at 20:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.