Playing sound with CrossSimpleAudioPlayer - Xamarin Forms

Recently I've been trying to use CrossSimpleAudioPlayer in Xamarin Forms to play .mp3 file, but I'm receiving this message

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

I changed the property Build Action to Embedded Resource, but my error persist, is something i'm missing here?

This is my reference https://blog.xamarin.com/adding-sound-xamarin-forms-app/?utm_medium=social&utm_campaign=blog&utm_source=linkedin&utm_content=simpleaudioplayer-nuget and this is my code:

public void Play() {
  var assembly = typeof(App).GetTypeInfo().Assembly;
  Stream audioStream = assembly.GetManifestResourceStream("softAlarm." + "softAlarm.mp3");
  var audio = Plugin.SimpleAudioPlayer.CrossSimpleAudioPlayer.Current;
  audio.Load(audioStream);

  audio.Play();
}

Upvotes: 3

Views: 3554

Answers (1)

Gerald Versluis
Gerald Versluis

Reputation: 34148

Where is your softAlarm.mp3 file located? You need to put in the full path concatenated with dots, prefixed by your project name.

So, if your file is in a project called SoftAlarm under the Resources folder, you should state: GetManifestResourceStream("SoftAlarm.Resources.softAlarm.mp3");

The sound file should be in your shared project and have the build action Embedded Resource.

I have created a working sample project for you here: https://github.com/jfversluis/CrossSimpleAudioPlayerSample

Upvotes: 8

Related Questions