How to play a .mp3 (or other) file in a UWP application?

I am trying this:

PlayMusic = new MediaElement(); PlayMusic.AudioCategory = Windows.UI.Xaml.Media.AudioCategory.Media; PlayMusic.Source = new Uri(@"C:\Users\UserName\Desktop\C:\Users\user\Desktop\Kill The Alarm - Begin Again.mp3"); PlayMusic.Play(); 

Error messages no longer appear on the display (try the catch clean command).

Sorry for the short description ... I can read and understand English very well, but it's hard for me to speak and write.

+8
c # windows-store-apps windows-store uwp audio music
source share
4 answers

Each Windows Store application has three folders. Local folder, Roaming folder and Temp folder. Each access is the same. Local is for storing assets in the local folder of the application.

Here is the answer:

 StorageFolder Folder = Windows.ApplicationModel.Package.Current.InstalledLocation; Folder = await Folder.GetFolderAsync("MyFolder"); StorageFile sf = await Folder.GetFileAsync("MyFile.mp3"); PlayMusic.SetSource(await sf.OpenAsync(FileAccessMode.Read), sf.ContentType); PlayMusic.Play(); 

MfG.

+11
source share

You cannot just read any file on your file system like this with Windows storage applications.

If you just want to test it:

  • Add file to your project in Visual Studio
  • Change your Build Action files to Content.
  • Change "Copy to output directory" to "Always copy."

What you probably want to do is explained in the section Reading local files using from from this article . It may also be helpful.

+4
source share

Put mySong.mp3 in the Assets folder. Then, in Visual Studio, right-click the Assets folder and select Add Existing Item. Add mySong.mp3 from the Assets folder. In XAML add a player:

  <MediaElement x:Name="myPlayer" AutoPlay="True" /> 

In C #, mySong.mp3 will play when you set the source:

  Uri newuri = new Uri("ms-appx:///Assets/mySong.mp3"); myPlayer.Source = newuri; 
+2
source share

Register MediaFailed -Event from MediaElement and check if it is raised. The ExceptionRoutedEventArgs passed to the method should contain information about why the file could not be played.

0
source share

All Articles