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.
A Uniform Resource Identifier (URI) is a unique sequence of characters that identifies a logical or physical resource used by web technologies.
Uri(String, UriKind) Initializes a new instance of the Uri class with the specified URI. This constructor allows you to specify if the URI string is a relative URI, absolute URI, or is indeterminate. public: Uri(System::String ^ uriString, UriKind uriKind); C# Copy.
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With