3
votes

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();
}
1
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 you file is in a project called SoftAlarm under the resources folder, you should state: GetManifestResourceStream("SoftAlarm.Resources.softAlarm.mp3") - Gerald Versluis
Then you probably should just do this: GetManifestResourceStream("softAlarm.mp3") as the OS will find it for you. But not entirely sure on this one. The link you're referring to also places the audio file in the shared library. That way you don't need any duplicate files as well :) - Gerald Versluis
so i tried like ... GetManifestResourceStream("MyProcet.softAlarm.mp" )and just GetManifestResourceStream("softAlarm.mp3"), error perssist - E.Rawrdríguez.Ophanim
I've created a sample project for you here: github.com/jfversluis/CrossSimpleAudioPlayerSample - Gerald Versluis
Done, thank you! And no problem at all :) - Gerald Versluis

1 Answers

8
votes

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