Android player raising exception prepare failed:status 0x1

12,205

Solution 1

prepare failed:status 0x1

is occuring because of either file path is in error or incorrect directory or Url or Uri found.

Try following in your code.

 mediaplayer.setDataSource(MainActivity.this, Uri.parse("android.resource://com.example.lvm/raw/"+name)); 
 //do not add any extension to name, eg. `R.raw.your_raw_file` 

Instead of

mp.setDataSource("android.resource://com.example.lvm/raw/"+name);

Solution 2

First, check the name of your file. It cannot contains capital letter and special characters. Remove that file, change name and copy it to environment again. Then use in Eclipse Project => Clean function (R.java should be regenerated). You can also try to use mp.setDataSource("android.resource://com.example.lvm/raw/"+name); instead of mediaplayer.setDataSrouce(MainActivity.this, Uri.parse("android.resource://com.example.lvm/raw/"+name)); Remember that do not add any extension to name, eg. R.raw.your_raw_file

Solution 3

It seems creating 31 prepared MediaPlayer is the maximum. When you are trying to create the 32th and invoked prepare will fail with this error.

If this is the case, how to fix this:

  1. Invoke release on useless MediaPlayer;
  2. Limit how many MediaPlayer you can create. For example if you are using a queue, try ArrayBlockingQueue<T>.
  3. Catch the IOException e, check "Prepare failed.: status=0x1".equals(e.getMessage()), if so retry.
Share:
12,205
dsharew
Author by

dsharew

Coding for Food

Updated on June 13, 2022

Comments

  • dsharew
    dsharew almost 2 years

    I am trying to play audio file from res/raw folder.
    but getting the error
    prepare failed: status = 0x1

    My code:

    package com.example.lvm;
    
    import java.io.IOException;
    
    import android.media.MediaPlayer;
    import android.media.MediaPlayer.OnCompletionListener;
    import android.os.Bundle;
    import android.app.Activity;
    import android.view.Menu;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.Toast;
    
    public class MainActivity extends Activity {
    Button introAudio;
            @Override
            protected void onCreate(Bundle savedInstanceState) {
                    super.onCreate(savedInstanceState);
                    setContentView(R.layout.activity_main);
                    introAudio=(Button)findViewById(R.id.introAudio);
                    introAudio.setOnClickListener(new OnClickListener() {
    
                            @Override
                            public void onClick(View arg0) {
                                    MediaPlayer mp = new MediaPlayer();
                                    String name = "greeting";
                                     mp.setOnCompletionListener(new OnCompletionListener() {
    
                                            @Override
                                            public void onCompletion(MediaPlayer mp) {
                                                mp.release();
                                            }
    
                                        });
    
                                    try {
                                         mp.setDataSource("android.resource://com.example.lvm/raw/"+name);
                                             mp.prepare();
                                             mp.start();
                                    } catch (Exception e) {
                                             Toast.makeText(MainActivity.this, e.getMessage(), Toast.LENGTH_LONG).show();
                                    }
                            }
                    });
            }
    
            @Override
            public boolean onCreateOptionsMenu(Menu menu) {
                    // Inflate the menu; this adds items to the action bar if it is present.
                    getMenuInflater().inflate(R.menu.activity_main, menu);
                    return true;
            }
    
    }
    
    • Alex Sorokoletov
      Alex Sorokoletov over 9 years
      I have same problem. However in my case I download the file and store it. File exists right before I call prepare, all other streams are closed. If I call prepare some time later it doesn't throw an exception. Don't know what's the problem but didn't find solution yet. Filename is just guid
    • Alex Sorokoletov
      Alex Sorokoletov over 9 years
      As usual, as soon as I shared my comment I found a solution. First of all, I checked logs in DDMS to see any additional information about error. I found that error right before exception Failed to open file 'audio/2f32a5de.mp4'. (No such file or directory). After that I found that in one specific case I do not create full path correctly :)
  • dsharew
    dsharew over 10 years
    I did. now there is no any exception raised but I cant hear the play.
  • Ritesh Gune
    Ritesh Gune over 10 years
    make sure the file is not corrupt, plz try with another file. I don't see any problem now.
  • Mygod
    Mygod over 9 years
    Well I meant 32nd. :P
  • ani0904071
    ani0904071 almost 8 years
    thanks for ur suggestion, I had to modify my code , but ur suggestion worked for me :)