1

I am attempting to build a UnoPlatform app which has the capability to play some embedded mp3 sound effect files.

Using MediaPlayerElement, it is straight-forward to play media from HTTP sources, however it seems there must be some platform specific hoops to jump through to get embedded resource files to play.

The following event handler works for UWP, but not IOS, Android, or WebAssembly:

private void SpinButton_OnClick(object sender, RoutedEventArgs e)
{
   player1.Source = MediaSource.CreateFromUri(new Uri("ms-appx:///Assets/Sound.mp3"));
}

I have attempted to follow the info here https://learn.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/mediaelement for how to embed the media on IOS, but it does not seem to work for me.

Is there a trick to this? is there a sample out there with this working?

Martin Zikmund
  • 38,440
  • 7
  • 70
  • 91

1 Answers1

2

The current implementation of the MediaPlayerElement is still a bit limited, but playing back mp3 on Android and iOS should be supported.

The trick is to put the .mp3 files in the right location as they are not yet (as compared to image files) automatically copied over to the correct platform-specific location during build. In case of Android, you need to put the audio files in the Assets folder, for example:

Android Assets folder

And the Build Action needs to be set to AndroidAsset.

In case of iOS, the files go to the Resources folder and have the Build Action set to Bundle Resource.

You can now reference the files as if they were in fact in the "Assets" folder, using the ms-appx: URI scheme:

<Grid>
    <MediaPlayerElement AutoPlay="True" Source="ms-appx:///Assets/test.mp3" />
</Grid>

This is just a quick example, but hopefully you can use it as a starting point. Whenever in doubt, it is quite useful to browse the source code of the respective features on GitHub, for example here is the media file loading implementation for Android.

For WebAssembly, MediaPlayerElement is not supported yet, but if you want to see it supported soon, please vote up on this issue.

Martin Zikmund
  • 38,440
  • 7
  • 70
  • 91