Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Storage file to System uri?

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.

like image 392
Hendra Anggrian Avatar asked May 13 '13 20:05

Hendra Anggrian


People also ask

What is a system URI?

A Uniform Resource Identifier (URI) is a unique sequence of characters that identifies a logical or physical resource used by web technologies.

What is URI string in C#?

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.


1 Answers

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);
like image 190
chue x Avatar answered Sep 27 '22 16:09

chue x