MediaPlayer Prepare failed. : stauts=0x1

IkeNwaoguIkeNwaogu USMember
edited December 2016 in Xamarin.Android

Hey I am following the guide for using audio here on the xamarin website. Every time I run my code I get this error : Java.IO.IOException: Prepare failed.: status=0x1. My audio file is in the assets folder and is an mp3. Does anyone know why I am getting this error?. My code is below:

public class MainActivity : Activity
    {
        protected MediaPlayer player = new MediaPlayer();
        public void StartPlayer(string filePath)
        {
            if (player == null)
            {
                player = new MediaPlayer();
            }
            else
            {
                player.Reset();
                player.SetDataSource(filePath);

                player.Prepare();
                player.Start();
            }
        }
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);

            // Set our view from the "main" layout resource
             SetContentView (Resource.Layout.Main);
            StartPlayer(@"c:\users\sr116\documents\visual studio 2015\Projects\WorkingWithAudio\WorkingWithAudio\Assets\Instrumental.mp3");
        }


    }
}

Posts

  • JoshuaGrassJoshuaGrass USMember

    I'm having the exact same problem. sometimes it works, some times it doesn't.

  • Hi,
    I wasted a lot of time, trying to find a solution to this issue on my side too. So I post here, just in case it is helping some people.
    My situation : I wanted to load an audio file from my assets (so not registered in my resources). I am using a similar code as Ike Nwaogu, except that I am using an AssetFileDescriptor to open my file (in my activity class code, so I have access to "Assets") :

    string path = "Audio/myfile.ogg";
    Android.Content.Res.AssetFileDescriptor afd = Assets.OpenFd(path);
    MediaPlayer soundPlayer = new MediaPlayer();
    
    if (afd != null)
    {
        soundPlayer.Reset();
        soundPlayer.SetDataSource(afd.FileDescriptor);
        soundPlayer.Prepare();
        soundPlayer.Enabled = true;
        afd.Close();
    }
    

    I was failing on the Prepare(). I tried to add the access to external storage permissions (but it did make sense since it was loaded from my assets directly, I tried just in case).

    Just by chance, by seeing other people samples on the forums, I added the afd.StartOffset, afd.DeclaredLength to the parameters:

    soundPlayer.SetDataSource(afd.FileDescriptor, afd.StartOffset, afd.DeclaredLength);

    and it worked ... I don't know if it just luck and if is going to fail again later or if there is a bug in API ...

Sign In or Register to comment.