Java.IO.FileNotFoundException: Xamarin.Forms

First of all, create an interface in the PCL.

    public  interface IReadStream
    {
        Stream ReadStreamFromAsset();
    }

Then achieve the interface in the android platform.


[assembly: Dependency(typeof(ReadStreamService))]
namespace MyPopUpPageDemo.Droid
{
    class ReadStreamService : IReadStream
    {
        public Stream ReadStreamFromAsset()
        {
          
            AssetManager assets = Android.App.Application.Context.Assets;

            return assets.Open("app_sound.mp3");
        }
    }
}

Please notice mp3 file Build Action to AndroidAsset

enter image description here

Then use dependenceService in the PCL.

           ISimpleAudioPlayer player;
        Stream stream;
        public MainPage()
        {
           
            InitializeComponent();

            player = Plugin.SimpleAudioPlayer.CrossSimpleAudioPlayer.CreateSimpleAudioPlayer();
            stream = DependencyService.Get<IReadStream>().ReadStreamFromAsset();
            player.Load(stream);

        }

When button is clicked it, play the audio

 private void Button_Clicked(object sender, EventArgs e)
        {
            player.Play();
          

        }

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top