Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 443
Vasile Doe Avatar asked Feb 06 '23 23:02

Vasile Doe


1 Answers

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();
like image 182
SWilko Avatar answered Feb 09 '23 13:02

SWilko