3

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;
        }

}
TN888
  • 7,659
  • 9
  • 48
  • 84
dsharew
  • 10,377
  • 6
  • 49
  • 75
  • 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 Sep 22 '14 at 01:56
  • 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 :) – Alex Sorokoletov Sep 22 '14 at 02:01

3 Answers3

4
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);
HarshitMadhav
  • 4,769
  • 6
  • 36
  • 45
Ritesh Gune
  • 16,629
  • 6
  • 44
  • 72
1

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

TN888
  • 7,659
  • 9
  • 48
  • 84
1

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.
Mygod
  • 2,077
  • 1
  • 19
  • 43