Audio, sound, or music can be essential part of mobile apps. Unfortunately, Xamarin.Forms doesn’t support this directly from their API. But of course, it can be implemented easily by using
DependencyService. This time I’ll show you how to implement this Audio Player in Xamarin.Forms with the help of
DependencyService. Also, I’ll show you how to integrate it using simple MVVM Pattern.
First thing first, create a new Xamarin.Forms Solution. Use Portable Class library and XAML for user interface. Let’s name this solution
AudioPlayer. Now that we’ve created a new solution, we’ll have three projects: Portable Library, iOS, and Android. We’ll implement one by one starting from our core Portable Library.
Audio Player Service Interface
Let’s head to our portable library project,
AudioPlayer. Create a new interface called
IAudioPlayerService. This Audio Service will have 4 core functions:
Play(string pathToAudio): Play an audio file which path is defined by
Play(): Same with previous function, but it’ll play the audio from previous session if paused.
Pause():This will pause current audio session. We’ll use
Play()to resume audio playing.
OnFinishedPlaying: This Action will be fired if audio player reached the end of file, which means audio playing is stopped.
Based on those functions, here is how our interface looks like:
Next step is to implement audio player service for each platforms based on this interface.
Audio Player Service iOS Implementation
For this step, let’s head to iOS project. Create a new class called
AudioPlayerService and implement
IAudioPlayerService interface. Also don’t forget to put
[assembly: Dependency(typeof(AudioPlayerService))] on top of namespace declaration.
In iOS environment, we’re going to use
AVAudioPlayer class to handle audio player. So we’re going to declare
_audioPlayer as a private variable. We won’t need anything to initialize this class, so we’re going to leave the constructor empty.
Next we’re going to implement
Play(string) function. It’ll look for the file under
Resources folder in iOS project at first, then an
AVAudioPlayer class will be created by using
pathToAudioFile parameter as path to audio file relative to
Resources folder. So for example if you have file under
Resources/file.mp3, you can just call this function like this:
But before we play the audio file, we need to check if
_audioPlayer is playing another file. All we need to do is remove
FinishedPlaying event and stop the audio playing. After that, create an
AVAudioPlayer object. Then add a
FinishedPlaying event listener and finally, we can just call
_audioPlayer.Play() to start playing the audio.
For the rest of the functions, we just need to call the equivalent of each native functions. The following snippet is what you need to implement those functions.
OnFinishedPlaying action is invoked under
Player_FinishedPlaying event listener because it indicates that the player has finished playing the audio file.
Audio Player Service Android Implementation
Android implementation is similar with iOS. The equivalent native class that can be used to handle audio playing is
MediaPlayer. So let’s declare a private variable called
_mediaPlayer. Also, we don’t need anything for initialization of this class, so just left the constructor empty.
If iOS look for the file under
Resources folder, then Android uses
Assets folder equivalently. So if your mp3 file is under
Assets/file.mp3 is equivalent with iOS under
Similar with iOS version, before we play the audio file we need to remove
Completion event listener and stop
_mediaPlayer if it’s currently playing. The difference with iOS is we can’t just create a new
MediaPlayer native class with selected file name. We’re going to need
AssetFileDescriptor to read the file. Then the
MediaPlayer need to be prepared by calling
This preparation is handled asynchronously. So we need to implement an event listener called
Prepared when preparation is completed. Then under
Prepared event listener, we can implement
Completion event listener to indicate that
_mediaPlayer finished playing the audio file and also call
Start() function to start audio playing process.
Like in iOS version, the rest of the functions have equivalent with native implementation. So what we need to do is to implement like following snippet.
Same with iOS,
OnFinishedPlaying action is invoked under
MediaPlayer_Completion that indicates the media player finished playing the entire audio file
Integrating Audio Player Service into Xamarin.Forms App
This final section is how to implement our audio player service inside Xamarin.Forms app. We’re just going to need implement a simple Page with one button to play or pause audio file. First you need to put audio file(s) under respective folder. Put it under
Droid/Assets/ for Android version and
iOS/Resources for iOS version. If you don’t have any audio file to test, you can download one of awesome music from incompetech. For this tutorial, I use their music called Galway.
Let’s create a new ViewModel called
AudioPlayerViewModel. Don’t forget to implement
INotifyPropertyChanged or any MVVM framework you prefer.
Add a new property called
CommandText. This property will be used as Button’s text. It’ll be written as Play if audio is stopped and Pause if audio is currently playing.
Add one parameter inside
AudioPlayerViewModel to pass audio player service we’ve created. Then add one variable
_isStopped to indicate wether audio player service is finished playing or not.
Last one is to add
PlayPauseCommand that will be fired if user touch the button. This is just a simple implementation. If audio is playing, it’ll paused. If not, it’ll resume playing.
Now let’s head to our user interface. You can create whatever you want, but for the sake of this tutorial I just need to add one button under
StackLayout. Bind respective button’s properties with the ViewModel’s properties.
AudioPlayerViewModel class as your user interface’s
BindingContext. To get native audio player service, use
DependencyService.Get() function and pass it into ViewModel’s parameter.
After all above code implemented, you can try running it on your iOS or Android devices. Press the button to play or pause the audio.
Even though Xamarin.Forms doesn’t provide anything out of the box, we can easily implement it by using Dependency Service and native implementation for each platforms. If you stuck finding C# code for each native implementation, you can just find Java code or Objective-C / Swift and rewrite it in C#. Anyway, thanks for reading and hopefully it’s useful for your next project.
You can download the final projects from GitHub.
* “Galway” Kevin MacLeod (incompetech.com) Licensed under Creative Commons: By Attribution 3.0 License.
Simple Custom Fonts Helper in Xamarin.Forms for Android
With the help of Custom Renderer, a unified API can be created