Android MediaPlayer throwing “Prepare failed.: status=0x1” on 2.1, works on 2.2


Answers

This is my solution:

MediaPlayer mediaPlayer = new MediaPlayer();
FileInputStream fis = null;
try {
    fis = new FileInputStream(mp3File);
    mediaPlayer.setDataSource(fis.getFD());
    mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
    mediaPlayer.prepare();
}   finally {
    if (fis != null) {
        try {
            fis.close();
        } catch (IOException ignore) {
        }
    }

}
Question

I've been really banging my head against the table trying to get the MediaPlayer class to try to play h.264-encoded videos on Android 2.1. My code is rather simple:

  AssetFileDescriptor fileDescriptor = getResources().openRawResourceFd(R.raw.my_movie);
  introMoviePlayer = new MediaPlayer();
  introMoviePlayer.setDataSource(fileDescriptor.getFileDescriptor(), fileDescriptor.getStartOffset(), fileDescriptor.getDeclaredLength());
  introMoviePlayer.prepare();

This always throws an exception at prepare(), with the text java.io.IOException: Prepare failed.: status=0x1. I got a bit more info by using MediaPlayer.create() with a URI, which also throws at prepare(), which is actually called by MediaPlayer.create(), with the message Command PLAYER_PREPARE completed with an error or info PVMFErrResourceConfiguration.

The same code works perfectly in Froyo (2.2). The videos themselves play fine in the video player app. Does anyone have perhaps a helpful hint that might help to solve this problem?

Edit: Still no solution -- very frustrating problem indeed. However, I have found that by creating a VideoView and setting the URI for the raw video works. This is very puzzling, as sending the exact same URI through a MediaPlayer class will throw.




In my case it was just a filename that it didn't like:
/storage/emulated/0/appname/2018-03-12T11:52:08Z.wav
Removed the hyphens and colons, and the exception went away.




To playback a video or audio file located in the raw folder use:

mMediaPlayer = MediaPlayer.create(this, R.raw.a_video_or_audio_file);
mMediaPlayer.start(); 

no need to call prepare() when using MediaPlayer.create(Context,R.raw.some_resource_file)




On my opinion i will ask you to check back your location of your media files<< SONG PATH or MOVIE PATH >>. I faced with this exception, i have overcome by correct "SONG PATH"




initilize the fileName, mediaPlayer instance:

private MediaPlayer mp;
private final static String fileName = "ring";

for playing/starting audio file from res/raw directory:

        try 
        {   
            mp = new MediaPlayer();
            mp.setAudioStreamType(AudioManager.STREAM_RING); //set streaming according to ur needs
            mp.setDataSource(Context, Uri.parse("android.resource://yourAppPackageName/raw/"+fileName));
            mp.setLooping(true);
            mp.prepare();
            mp.start();

        } catch (Exception e) 
        {
            System.out.println("Unable to TunePlay: startRingtone(): "+e.toString());
        }

finally stopping the audioFile:

    try
    {
        if (!(mp == null) && mp.isPlaying())
        {
            mp.stop();
            mp.release(); //its a very good practice
        }       
    }
    catch (Exception e)
    {
        System.out.println("Unable to  TunePlay: stopRingtone():"+e.toString());
    }



If you are using multiple instances of MediaPlayer, make sure that you've executed release() on a resource before attempting to use it in a different instance.






Related