How to play a file from a library through MediaElement?

I can only play files from the application store, but I have to play the file from a library or other source. I'm trying to:

var storageFile = await StorageFile.GetFileFromApplicationUriAsync(new Uri("C:\\Users\\admin\\Music\\line.mp3")); var stream = await storageFile.OpenAsync(FileAccessMode.Read); mediaElement.SetSource(stream, storageFile.ContentType); mediaElement.Play(); 

It gives an exception: "An exception of type" System.ArgumentException "occurred in Polar.exe but was not processed in the user code. Additional information: the value does not fall into the expected range."

I tried mediaElement.Source (), but the element does not play sound. No exception, nothing.

I think this is a stupid problem, but I cannot find a solution. What am I doing wrong?

+2
c # windows-store-apps
source share
1 answer

If you want to play audio files from a library, you need to use KnownFolders . GetFileFromApplicationUriAsync will not work.

 var storageFile = await KnownFolders.MusicLibrary.GetFileAsync("line.mp3"); var stream = await storageFile.OpenAsync(FileAccessMode.Read); mediaElement.SetSource(stream, storageFile.ContentType); mediaElement.Play(); 

If you want to list the entire media file or all folders, use this.

 var AllStorageFiles = await KnownFolders.MusicLibrary.GetFilesAsync(); var AllStorageFolders = await KnownFolders.MusicLibrary.GetFoldersAsync(); 
+5
source share

All Articles