Xamarin.Forms playing audio

1

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() {
            DependencyService.Get<IAudio>().PlayAudioFile("beep.mp3");

        }
    }

// Interfáz

 public interface IAudio
    {
        void PlayAudioFile(string fileName);
    }

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

folder

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();
  }
}

Does anyone know anything about it?

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

1 answer

2

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
source