Playing Audio (mp3) File In Xamarin.Forms

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:

  1. using System;  
  2. namespace AudioPlayEx  
  3. {  
  4.     public interface IAudio  
  5.     {  
  6.         void PlayAudioFile(string fileName);  
  7.     }  
  8. }   

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:

  1. using System;  
  2. using Xamarin.Forms;  
  3. using AudioPlayEx.Droid;  
  4. using Android.Media;  
  5. using Android.Content.Res;  
  6. [assembly: Dependency(typeof(AudioService))]  
  7. namespace AudioPlayEx.Droid  
  8. {  
  9.     public class AudioService: IAudio  
  10.     {  
  11.         public AudioService()  
  12.         {}  
  13.         public void PlayAudioFile(string fileName)  
  14.         {  
  15.             var player = new MediaPlayer();  
  16.             var fd = global::Android.App.Application.Context.Assets.OpenFd(fileName);  
  17.             player.Prepared += (s, e) =>  
  18.             {  
  19.                 player.Start();  
  20.             };  
  21.             player.SetDataSource(fd.FileDescriptor, fd.StartOffset, fd.Length);  
  22.             player.Prepare();  
  23.         }  
  24.     }  
  25. }  
"Make sure that the audio files you are planning to play are copied in ‘Assets’ folder then only the above code will work." 

And the iOS project class will be like this:

  1. using System;  
  2. using Xamarin.Forms;  
  3. using AudioPlayEx;  
  4. using AudioPlayEx.iOS;  
  5. using System.IO;  
  6. using Foundation;  
  7. using AVFoundation;  
  8. [assembly: Dependency(typeof(AudioService))]  
  9. namespace AudioPlayEx.iOS  
  10. {  
  11.     public class AudioService: IAudio  
  12.     {  
  13.         public AudioService()  
  14.         {}  
  15.         public void PlayAudioFile(string fileName)  
  16.         {  
  17.             string sFilePath = NSBundle.MainBundle.PathForResource(Path.GetFileNameWithoutExtension(fileName), Path.GetExtension(fileName));  
  18.             var url = NSUrl.FromString(sFilePath);  
  19.             var _player = AVAudioPlayer.FromUrl(url);  
  20.             _player.FinishedPlaying += (object sender, AVStatusEventArgs e) =>  
  21.             {  
  22.                 _player = null;  
  23.             };  
  24.             _player.Play();  
  25.         }  
  26.     }  
  27. }  
"In case of iOS project you will need to copy the audio files to ‘Resources’ folder." 

And finally we will use the following code in our PCL/shared project in order to play the audio file,

  1. 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.

Read more articles on Xamarin:


Similar Articles