Implementing the Android SoundHandler using the MediaPlayer framework

To implement the same functionality for the sound handler interface in Android, we will be using the the MediaPlayer framework.

Let's create a new folder in the Android project called Sound, and create a new file called SoundHandler.cs:

public class SoundHandler : ISoundHandler 
{ 
        private MediaPlayer _mediaPlayer; 
 
        public bool IsPlaying { get; set; } 
} 

The same as the iOS version, let's add the Load and PlayPause functions:

public void Load() { try { _mediaPlayer = new MediaPlayer(); _mediaPlayer.SetAudioStreamType(Stream.Music); AssetFileDescriptor descriptor = Android.App.Application.Context.Assets.OpenFd("Moby - The Only Thing.mp3"); _mediaPlayer.SetDataSource(descriptor.FileDescriptor, ...

Get Xamarin Blueprints now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.