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

Nik Reiman picture Nik Reiman · Sep 21, 2010 · Viewed 67.7k times · Source

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.

Answer

MadMurdok picture MadMurdok · Jun 6, 2013

This is my solution:

MediaPlayer mediaPlayer = new MediaPlayer();
FileInputStream fis = null;
try {
    File directory = new File("android.resource://com.example.myapp/raw/");
    fis = new FileInputStream(directory);
    mediaPlayer.setDataSource(fis.getFD());
    mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
    mediaPlayer.prepare();
}   finally {
    if (fis != null) {
        try {
            fis.close();
        } catch (IOException ignore) {
        }
    }

}