Xamarin.Forms playing audio


hi friends I'm trying to play an mp3 for the notifications of an App that I developed in Xamarin.Forms I was reading about it and very similar to SQLite I must handle this event with an interface, but I have trouble referencing it


Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object.

I think maybe it's the way I try to reproduce it,

I try to execute my method of playing from this class

public class AudioService
        //private IAudio awx;

        public void Play() {


// Interfáz

 public interface IAudio
        void PlayAudioFile(string fileName);

This is my handler class Android , I put my beep file in the Assets


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.SetDataSource(fd.FileDescriptor, fd.StartOffset, fd.Length);

Does anyone know anything about it?

asked by E.Rawrdríguez.Ophanim 27.02.2018 в 18:02

1 answer


You probably missed adding:

[assembly: Xamarin.Forms.Dependency(typeof(AudioService))]

before the definition of the namespace in the Android AudioService class.

answered by 28.02.2018 / 20:27