0

I'm having problems with a simple app, trying to use SimpleAudioPlayer Plugin in Xamarin, with Visual Studio 2019 and creating an Android app.

Atream always returns null. File meulanchinho.mp3 is placed in Friday.Android project, inside Assets folder and with Build Action set to "AndroidAsset"

namespace Friday

{

[XamlCompilation(XamlCompilationOptions.Compile)]

public partial class PranchaLaranja : ContentPage

{

    public PranchaLaranja ()

    {
        bool xr = false;
        InitializeComponent ();

        var player = Plugin.SimpleAudioPlayer.CrossSimpleAudioPlayer.Current;
        player.Load(GetStreamFromFile("meulanchinho.mp3"));
        player.Play();
        xr = player.IsPlaying;
    }
    public Stream GetStreamFromFile(string filename)
    {
        var assembly = typeof(App).GetTypeInfo().Assembly;
        var xAppName = typeof(App).GetTypeInfo().Name;
        Stream stream = assembly.GetManifestResourceStream(xAppName + filename);
        return stream;
    }
}
}

As stream returns null, player.play returns an error:

Unhandled Exception:

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

Saveen
  • 4,120
  • 14
  • 38
  • 41
  • GetStreamFromFile() should only be needed if the audio is in the shared project. For an AndroidAsset you should be able to just specify the file name – Jason Jun 08 '19 at 03:20

1 Answers1

0

After some digging and testing I made it. The mp3 file "should not be" on Android project Assets folders. It must be on solution. With build option "Emblended Resource".

Hope it helps anyone else.

Thanks for your time and if someone can just explain "why" is this (because many web search, it always advised me to put the file in the assets folder).

  • Why? You have a method `GetStreamFromFile` that is specifically returning a stream from an embedded assembly resource. You could just use `Assets.Open` to obtain a stream from an Android Asset. – SushiHangover Jun 08 '19 at 04:47