37

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.

Nik Reiman
  • 39,067
  • 29
  • 104
  • 160
  • Have you tried that code if you store your file in the SD card and pass its own path? – Pedriyoo Jan 31 '11 at 14:15
  • its rather too late, but i guess the constructor of mediaplayer has an overload which takes resourceId – Samuel Apr 19 '11 at 07:23
  • In my case, I had Gzip Compression on the web address where the url info is. Compressed media files may not be decoded on android. – İlker Elçora Jun 23 '21 at 08:39

15 Answers15

32

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) {
        }
    }

}
Md Imran Choudhury
  • 9,343
  • 4
  • 62
  • 60
MadMurdok
  • 570
  • 6
  • 8
  • 3
    Just a reminder for local files. If MediaPlayer.setDataSource(String path) has prepare error, then this work around does not work either. Since in the background logic of MediaPlayer.setDataSource(String path), it is also using setDataSource(FileDescriptor fd). – Hexise Jul 12 '17 at 04:41
20

I do not know if this is your issue, but I just found a solution to the problem the described by Tuszy above. I could read the file I was creating from external storage but not from Cache.

The solution is that the read write permissions when writing the file are different.

Please see this excellent explanation in this blog I found.

http://blog.weston-fl.com/android-mediaplayer-prepare-throws-status0x1-error1-2147483648/

enter image description here

enter image description here

Tura
  • 1,227
  • 11
  • 22
Ryan Heitner
  • 13,119
  • 6
  • 77
  • 119
  • I am not working at the same company which had this problem anymore, so I can't say for sure whether the solution would fix this problem. However, I'm going to mark this as the correct answer, as it's the best resource I've seen on this rather obscure bug so far. – Nik Reiman Aug 16 '12 at 15:48
  • I don't think that blog exists anymore. Please port the answer here. – Jason Robinson Aug 03 '15 at 20:22
  • I managed to dig the blog out of an archiving site. Once you are one the web it is forever. – Ryan Heitner Aug 04 '15 at 06:58
8

I know that I am late here but hopefully this helps somebody else. I worked around this issue by setting up a setOnCompletionListener callback that explicitly releases the MediaPlayer object after the media is done playing.

I can't take credit for this solution, as it was originally posted by Ronny here: How do you detect when a sound file has finished?

But since this is the first answer when I search for Android+Failed Status 0x1 here is my code that solved this problem for me:

 public void playSound(String soundPath){
    MediaPlayer m = new MediaPlayer();

    m.setOnCompletionListener(new OnCompletionListener() {

        @Override
        public void onCompletion(MediaPlayer mp) {
            mp.release();
        }

    });

    try {

        AssetFileDescriptor descriptor = mContext.getAssets().openFd(soundPath);
        m.setDataSource(descriptor.getFileDescriptor(), descriptor.getStartOffset(),
                        descriptor.getLength());            

        descriptor.close();

        m.prepare();
        m.setVolume(100f, 100f);
        m.setLooping(false);
        m.start();

    } catch (Exception e) {
        //Your catch code here.
    }
 }   
Community
  • 1
  • 1
Anthony Roe
  • 143
  • 1
  • 5
7

If anyone else gets stuck on this when reading from a remote URL,

my urls were all nonsecure, (i.e. http instead of https)

since I had no access to the server urls (3rd party)

I had to add the following in the android manifest.

<application
    ...
    android:usesCleartextTraffic="true"
    ...
</application>
Vasili Fedotov
  • 1,091
  • 14
  • 31
4

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.

Paul Lammertsma
  • 37,593
  • 16
  • 136
  • 187
3

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());
    }
ani0904071
  • 362
  • 2
  • 11
2

I had a similar problem. I had a zip file which contained sound files which were compressed. In order to pass them on to MediaPlayer they needed to be uncompressed. When I placed them inside application's cache directory (context.getCacheDir()), MediaPlayer returned the same error which you described.

But when I placed them on external storage (Environment.getExternalStorageDirectory()), everything started working.

srgtuszy
  • 1,548
  • 1
  • 18
  • 16
  • I also placed them on external storage (Environment.getExternalStorageDirectory()), everything started working. – Ram G. Feb 09 '15 at 18:40
2

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"

SAWJUSTO
  • 369
  • 1
  • 5
  • 19
2

I found Best and clear solution for this error : java.io.IOException: Prepare failed.: status=0x1

Note

if you want read from your sd card and got this error , your are not set
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/> in your Manifast .

Note 2

if your are want play from url and got that error maybe your are set setDataSource to like this :https://www.android.com just remove S from http like this http://www.android.com

Erfan
  • 3,059
  • 3
  • 22
  • 49
2

I know this may be too late. I managed to use video from resources as this:

MediaPlayer mp = ...;
Uri uri = Uri.parse("android.resource://"+getPackageName()+"/" + R.raw.my_movie);
mp.setDataSourceUri(this, uri);
Md Imran Choudhury
  • 9,343
  • 4
  • 62
  • 60
Aleks G
  • 56,435
  • 29
  • 168
  • 265
1

If you are dealing with remote URL, use below code to get encoded url compatible with MediaPlayer.

public String getEncodedURL(String urlStr) throws Exception 
{
            URL url = new URL(urlStr);
            URI uri = new URI(url.getProtocol(), url.getUserInfo(), url.getHost(), url.getPort(), url.getPath(), url.getQuery(), url.getRef());
            urlStr = uri.toASCIIString();
            return urlStr;
}
Akshay Khale
  • 8,151
  • 8
  • 50
  • 58
Rakesh Patil
  • 370
  • 4
  • 12
1

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.

Ben
  • 51
  • 5
1

first thanks for your code, the open raw ressource helped me with my problem.

I'm sorry I don't have a definite answer to your problem but based on the api media example in the SDK and my media player in my project I would suggest checking that the file opens correctly.

otherwise maybe you need to set the display before preparing as such:

mMediaPlayer = new MediaPlayer();
            mMediaPlayer.setDataSource(fileDescriptor.getFileDescriptor(), fileDescriptor.getStartOffset(), fileDescriptor.getDeclaredLength());
            mMediaPlayer.setDisplay(holder);
            mMediaPlayer.prepare();

those are the only ideas I have

hope it helps

Jason

Jason Rogers
  • 19,194
  • 27
  • 79
  • 112
  • hi, I am facing same issue. My code like this--- MediaPlayer mMediaPlayer=new MediaPlayer(); String PATH_TO_FILE=this.getFilesDir()+"/Organic_chemistry/new_audio.mp3"; mMediaPlayer.setDataSource(PATH_TO_FILE); mMediaPlayer.prepare(); – Datta Kunde Jun 15 '12 at 12:23
0

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)

TouchBoarder
  • 6,422
  • 2
  • 52
  • 60
0

playing mp3 files instead of wmva fixed it. Apparently that isn't an issue on API 19

sam winston
  • 162
  • 1
  • 13