Vasile Doe
Vasile Doe

Reputation: 1754

Play sound in Universal Windows Platform

I'd like to play .mp3 or wav sounds in my uwp app, I need to play it only when app is open and without any media element on the ui. Is there a possibility to make some threads to play separate songs at the same time. Any relevant info is appreciated.

Upvotes: 5

Views: 6126

Answers (2)

Muhammad Hashim Shafiq
Muhammad Hashim Shafiq

Reputation: 653

May be this code snippet will help you out.

MediaElement PlayMusic = new MediaElement();
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();

Upvotes: 3

SWilko
SWilko

Reputation: 3612

Adapting the above answer slightly using MediaElement. Presumes you have a media file at the root of your application in MyFolder/MySound.wav

 var element = new MediaElement();
 var folder = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFolderAsync("MyFolder");
 var file = await folder.GetFileAsync("MySound.wav");
 var stream = await file.OpenAsync(Windows.Storage.FileAccessMode.Read);
 element.SetSource(stream, "");
 element.Play();

Upvotes: 8

Related Questions