MediaPlayer Preparing Failed

2020-07-26 07:31发布

问题:

All I have been doing for the past 8 hours is trying to come up with a simple recorder/ playback app. I want to be able to record the audio in 3GP and then have it automatically loaded into the MediaPlayer so that I can play it back. I am 80% sure that it is recording and saving to the location specified in the code, but when I try and load it into my media player I always get this error. I have tried loading the toto_africa.mp3 file too to make sure it's not the recorded file's problem, and it still gives me this error. Any help with this error would be much appreciated because I have searched everywhere for the answer. I guess a more specific question would be: How do I properly get my MediaPlayer and MediaRecorder communicating and functioning correctly?


LogCat

03-14 00:32:18.708: E/appPath:(4443): /data/data/com.jordan.tester/files
03-14 00:32:18.708: E/(1) Length of de file mon'?(4443): 0
03-14 00:32:24.408: E/Are ya dead maan?(4443): Kiss the Lucky egg
03-14 00:32:24.688: E/IS da file alive?(4443): true
03-14 00:32:24.688: E/IS da file a file?(4443): true
03-14 00:32:24.688: E/(2) Length of de file mon'?(4443): 4515
03-14 00:32:28.362: V/MediaPlayer(4443): constructor
03-14 00:32:28.362: V/MediaPlayer(4443): setListener
03-14 00:32:28.368: V/MediaPlayer(4443):setDataSource(/data/data/com.jordan.tester/files/temp.3gp)
03-14 00:32:28.368: V/MediaPlayer(4443): prepare
03-14 00:32:28.368: V/MediaPlayer(4443): message received msg=100, ext1=1, ext2=-2147483648
03-14 00:32:28.368: E/MediaPlayer(4443): error (1, -2147483648)
03-14 00:32:28.368: V/MediaPlayer(4443): signal application thread
03-14 00:32:28.368: V/MediaPlayer(4443): prepare complete - status=1
03-14 00:32:28.378: D/MediaPlayer(4443): create failed:
03-14 00:32:28.378: D/MediaPlayer(4443): java.io.IOException: Prepare failed.: status=0x1
03-14 00:32:28.378: D/MediaPlayer(4443):    at android.media.MediaPlayer.prepare(Native Method)
03-14 00:32:28.378: D/MediaPlayer(4443):    at android.media.MediaPlayer.create(MediaPlayer.java:638)
03-14 00:32:28.378: D/MediaPlayer(4443):    at android.media.MediaPlayer.create(MediaPlayer.java:615)
03-14 00:32:28.378: D/MediaPlayer(4443):    at com.jordan.tester.Main.startPlaying(Main.java:120)
03-14 00:32:28.378: D/MediaPlayer(4443):    at com.jordan.tester.Main.onClick(Main.java:89)
03-14 00:32:28.378: D/MediaPlayer(4443):    at android.view.View.performClick(View.java:2419)
03-14 00:32:28.378: D/MediaPlayer(4443):    at android.view.View$PerformClick.run(View.java:8865)
03-14 00:32:28.378: D/MediaPlayer(4443):    at android.os.Handler.handleCallback(Handler.java:587)
03-14 00:32:28.378: D/MediaPlayer(4443):    at android.os.Handler.dispatchMessage(Handler.java:92)
03-14 00:32:28.378: D/MediaPlayer(4443):    at android.os.Looper.loop(Looper.java:143)
03-14 00:32:28.378: D/MediaPlayer(4443):    at android.app.ActivityThread.main(ActivityThread.java:5061)
03-14 00:32:28.378: D/MediaPlayer(4443):    at java.lang.reflect.Method.invokeNative(Native Method)
03-14 00:32:28.378: D/MediaPlayer(4443):    at java.lang.reflect.Method.invoke(Method.java:521)
03-14 00:32:28.378: D/MediaPlayer(4443):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
03-14 00:32:28.378: D/MediaPlayer(4443):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
03-14 00:32:28.378: D/MediaPlayer(4443):    at dalvik.system.NativeStart.main(Native Method)
03-14 00:32:28.378: E/TESTER(4443): java.lang.NullPointerException

My Code

package com.jordan.tester;

import java.io.File;
import java.io.IOException;

import android.media.MediaPlayer;
import android.media.MediaRecorder;
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class Main extends Activity implements OnClickListener {

    private Button beginRecord, beginPlay, stopRecord, stopPlay;
    private MediaPlayer player;
    private MediaRecorder recorder;
    private String TESTER_TAG = "TESTER";

    private File file;
    private String tempPath = "R.raw.toto_africa";
    private String fn = "temp.3gp";
    private File appPath;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        beginRecord = (Button) findViewById(R.id.beginRecord);
        beginPlay = (Button) findViewById(R.id.beginPlay);
        stopRecord = (Button) findViewById(R.id.stopRecord);
        stopPlay = (Button) findViewById(R.id.stopPlay);

        beginRecord.setOnClickListener(this);
        beginPlay.setOnClickListener(this);
        stopRecord.setOnClickListener(this);
        stopPlay.setOnClickListener(this);

        file = new File(this.getFilesDir(), fn);
        file.delete();

        appPath = this.getFilesDir();
        Log.e("appPath:", appPath.toString());
        file = new File(this.getFilesDir(), fn);
        tempPath = file.getAbsolutePath();
        Log.e("tempPath:", tempPath);

        Log.e("(1) Length of de file mon'?", String.valueOf(file.length()));
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {

        case R.id.beginRecord:
            try {
                startRecording();

            } catch (Exception e) {
                Log.e(TESTER_TAG, e.toString());
            }
            break;
        // ////////////////////////////////////////////////////////////
        case R.id.beginPlay:
            try {
                startPlaying();
            } catch (Exception e) {
                Log.e(TESTER_TAG, e.toString());
            }
            break;
        // ///////////////////////////////////////////////////////////
        case R.id.stopRecord:
            try {
                stopRecording();
            } catch (Exception e) {
                Log.e(TESTER_TAG, e.toString());
            }
            break;
        // /////////////////////////////////////////////////////////////
        case R.id.stopPlay:
            try {
                stopPlaying();
            } catch (Exception e) {
                Log.e(TESTER_TAG, e.toString());
            }
            break;
        }
    }

    private void startPlaying() {
        ditchMediaPlayer();

        try {
            player = MediaPlayer.create(getBaseContext(), Uri.parse(tempPath));
            player.start();
        } catch (IllegalStateException e) {
            Log.e(TESTER_TAG, e.toString());
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (SecurityException e) {
            e.printStackTrace();
        }
    }

    private void stopPlaying() {
        // /
    }

    private void stopRecording() {
        if (recorder != null) {
            Log.e("Are ya dead maan?", "Kiss the Lucky egg");
            recorder.stop();
            recorder.release();
            Log.e("IS da file alive?", String.valueOf(file.exists()));
            Log.e("IS da file a file?", String.valueOf(file.isFile()));
            Log.e("(2) Length of de file mon'?", String.valueOf(file.length()));
        }
    }

    private void startRecording() {
        ditchMediaRecorder();
        // File outFile = new File(OUTPUT_FILE);
        recorder = new MediaRecorder();
        recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
        recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
        recorder.setOutputFile(tempPath);

        try {
            recorder.prepare();
        } catch (IllegalStateException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        recorder.start();
    }

    private void ditchMediaRecorder() {
        if (recorder != null) {
            recorder.release();
        }
    }

    private void ditchMediaPlayer() {
        if (player != null) {

            try {
                player.release();
            } catch (Exception e) {
                Log.e(TESTER_TAG, e.toString());
            }
        }
    }
}

回答1:

From your logcat logs, I find that the setDataSource part of your Create is failing. From your logs,

03-14 00:32:28.368: V/MediaPlayer(4443):setDataSource(/data/data/com.jordan.tester/files/temp.3gp)
03-14 00:32:28.368: V/MediaPlayer(4443): prepare
03-14 00:32:28.368: V/MediaPlayer(4443): message received msg=100, ext1=1, ext2=-2147483648 
03-14 00:32:28.368: E/MediaPlayer(4443): error (1, -2147483648)

As part of the prepare, the player engine implementation waits for finishSetDataSource. If the underlying player engine couldn't set a DataSource, it will return an UNKNOWN_ERROR which corresponds to 0x80000000 or -2147483648.

I believe the problem is stemming from the string being passed to the setDataSource. From the logs reproduced above, the string is /data/data/com.jordan.tester/files/temp.3gp, which I feel doesn't match any of the known implementation methods for file or streaming sources.

To solve your problem, I would recommend you to either append a file:// to your input string i.e. the string being passed to create could be file:///data/data/com.jordan.tester/files/temp.3gp or open the file in Java itself and pass a file descriptor to create.