Recently, I had requirement to play mp3 files in one of my Xamarin Forms applications, and when I searched over the internet I learned that currently Xamarin Forms doesn’t support playing audio files out of the box. But as every Xamarin Forms developer knows, we can achieve any native functionality using dependency service so I thought why not give this a try? So here it goes.
We need to create an interface which will be implemented in platform specific project, I named it IAudio.cs and the code for the same is as follows:
- using System;
- namespace AudioPlayEx
- {
- public interface IAudio
- {
- void PlayAudioFile(string fileName);
- }
- }
Now we need to write the class which will impliment this interface in platform specfic code . I named the class ‘AudioService’ in both platforms
The Android project class will be like this:
- using System;
- using Xamarin.Forms;
- using AudioPlayEx.Droid;
- using Android.Media;
- using Android.Content.Res;
- [assembly: Dependency(typeof(AudioService))]
- namespace AudioPlayEx.Droid
- {
- public class AudioService: IAudio
- {
- public AudioService()
- {}
- public void PlayAudioFile(string fileName)
- {
- var player = new MediaPlayer();
- var fd = global::Android.App.Application.Context.Assets.OpenFd(fileName);
- player.Prepared += (s, e) =>
- {
- player.Start();
- };
- player.SetDataSource(fd.FileDescriptor, fd.StartOffset, fd.Length);
- player.Prepare();
- }
- }
- }
And the iOS project class will be like this:
- using System;
- using Xamarin.Forms;
- using AudioPlayEx;
- using AudioPlayEx.iOS;
- using System.IO;
- using Foundation;
- using AVFoundation;
- [assembly: Dependency(typeof(AudioService))]
- namespace AudioPlayEx.iOS
- {
- public class AudioService: IAudio
- {
- public AudioService()
- {}
- public void PlayAudioFile(string fileName)
- {
- string sFilePath = NSBundle.MainBundle.PathForResource(Path.GetFileNameWithoutExtension(fileName), Path.GetExtension(fileName));
- var url = NSUrl.FromString(sFilePath);
- var _player = AVAudioPlayer.FromUrl(url);
- _player.FinishedPlaying += (object sender, AVStatusEventArgs e) =>
- {
- _player = null;
- };
- _player.Play();
- }
- }
- }
And finally we will use the following code in our PCL/shared project in order to play the audio file,
- DependencyService.Get<IAudio>().PlayAudioFile("MySong.mp3");
You can call this code with the click of a button event or any other event in your PCL/Shared project. In the above example I have played an mp3 file but you can use the same code to play any audio file supported by the platform for example .aif (only in iOS), mp4, wav etc.

ets tsePosted Jun 3, 2022, 6:42 AM
How can I play a file from System.Environment.SpecialFolder.LocalApplicationData?
keertana dapariPosted Apr 28, 2017, 1:58 AM
Can you tell me how to record audio and play that particular audio in xamarin forms
Asfend YarPosted Mar 27, 2016, 5:27 AM
good work
Gopi ChandPosted Mar 27, 2016, 5:07 AM
Interesting :)
Kashif SohailPosted Mar 26, 2016, 11:36 PM
Wao
Vignesh ManiPosted Mar 26, 2016, 9:28 AM
good
Ammar ShaukatPosted Mar 25, 2016, 4:44 PM
Good
Sibeesh VenuPosted Mar 25, 2016, 5:41 AM
Nice Share
Debasis SahaPosted Mar 25, 2016, 12:17 AM
Good
Saillesh PawarPosted Mar 25, 2016, 12:06 AM
Nice share sir
Kumaresh RajalingamPosted Mar 24, 2016, 8:47 PM
good one
Ammar ShaukatPosted Mar 24, 2016, 4:48 PM
Good