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

4

10

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.

Odont answered 15/9, 2015 at 18:9 Comment(3)
try wrapping the long file name around single quotes or rename the file to have no spaces and see if it worksCallipash
no change by this means. :/Odont
C:\Users\UserName\Desktop\C:\Users\paulk\Desktop\Kill The Alarm - Begin Again.mp3 is not a valid path, you're repeating the substring to your Desktop folder.Enrich
O
13

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.

Odont answered 16/9, 2015 at 18:11 Comment(0)
V
4

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.

Vankirk answered 15/9, 2015 at 22:34 Comment(1)
Yeaaaaah! Thank You Yo Much! I did it! (i post the solution as Answer!)Odont
A
3

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;
Apsis answered 15/5, 2017 at 11:6 Comment(0)
D
0

Register the MediaFailed-Event of the MediaElement and check if it gets raised. The ExceptionRoutedEventArgs passed to the method, should contain information about, why the file cannot be played.

Dniester answered 15/9, 2015 at 18:19 Comment(2)
Try including the audio file in your solution and then change the URIDniester
PlayMusic.Source = new Uri(@"ms-appx://Sleep/Media/KillTheAlarm.mp3"); No changes :/Odont

© 2022 - 2024 — McMap. All rights reserved.