Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I try 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();

No more error messages appear on the display (try catch runs clean through).

Sorry for the short description... I can read and understand English very well but it is difficult for me to talk and write.

like image 599
Paule Paul Avatar asked Sep 15 '15 18:09

Paule Paul


3 Answers

Every Windows Store App has three folders. A Local folder, a Roaming folder and a Temp folder. Each is accessed the same way. Local is meant to store assets in a local, application-specific folder.

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.

like image 83
Paule Paul Avatar answered Oct 06 '22 00:10

Paule Paul


You cannot just read any file on your file system like this with windows store applications.

If you just want to test it:

  1. Add the file to your project in Visual Studio
  2. Change your file’s "Build Action" to "Content".
  3. Change "Copy to Output Directory" to "Copy Always".

What you probably want to do is explained in the section, Read Local files w/o a Picker from this article. This might also be helpful.

like image 27
O.O Avatar answered Oct 06 '22 01:10

O.O


Put mySong.mp3 in your Assets folder. Then in Visual Studio, right click on your Assets folder and select "add existing item". Add mySong.mp3 FROM your 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;
like image 20
pollaris Avatar answered Oct 06 '22 00:10

pollaris