Reputation: 11
I want to make a small project. Something like a soundboard with jokes from work. But i can´t play sounds in UWP. I found this:
private async System.Threading.Tasks.Task Button_ClickAsync(object sender, RoutedEventArgs e)
{
MediaElement PlayMusic = new MediaElement();
PlayMusic.AudioCategory = Windows.UI.Xaml.Media.AudioCategory.Media;
StorageFolder Folder = Windows.ApplicationModel.Package.Current.InstalledLocation;
Folder = await Folder.GetFolderAsync("folder");
StorageFile sf = await Folder.GetFileAsync("song.mp3");
PlayMusic.SetSource(await sf.OpenAsync(FileAccessMode.Read), sf.ContentType);
PlayMusic.Play();
}`
but i dont know where I have to put the sound. I have tried the Assets folder but it didn´t work. Im a newbie and sorry for my bad english.
Upvotes: 1
Views: 414
Reputation: 1882
You can select your file to play using FileOpenPicker or you can place your file in assets folder and access using uri. Added both sample code snippet below
//Play from file
private async void PlayFromFile(object sender, RoutedEventArgs e)
{
//Play from file
FileOpenPicker openPicker = new FileOpenPicker();
openPicker.ViewMode = PickerViewMode.Thumbnail;
openPicker.FileTypeFilter.Add(".m4p");
openPicker.FileTypeFilter.Add(".mp3");
var sf = await openPicker.PickSingleFileAsync();
MediaPlayer mediaPlayer = new MediaPlayer();
mediaPlayer.Source = MediaSource.CreateFromStorageFile(sf);
mediaPlayer.Play();
}
//Play from assets folder
private void PlayFromAssetsFolder(object sender,RoutedEventArgs e)
{
//Play from assets folder
MediaPlayer mediaPlayer = new MediaPlayer();
mediaPlayer.Source = MediaSource.CreateFromUri(new Uri("ms-appx:///Assets/audio.mp3"));
mediaPlayer.Play();
}
Upvotes: 1