MediaPlayer准备失败

时间:2013-03-14 05:55:06

标签: android audio media-player record

过去8小时我一直在做的就是尝试提供一个简单的录音/播放应用程序。我希望能够在3GP中录制音频,然后将其自动加载到MediaPlayer中,以便我可以播放它。我80%确定它正在录制并保存到代码中指定的位置,但是当我尝试将其加载到我的媒体播放器中时,我总是会收到此错误。我也尝试加载toto_africa.mp3文件,以确保它不是录制文件的问题,它仍然给我这个错误。任何有关此错误的帮助都将非常感激,因为我已经到处寻找答案。我想更具体的问题是:如何正确地让MediaPlayerMediaRecorder正确沟通并正常运作?


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

我的代码

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 个答案:

答案 0 :(得分:4)

从您的logcat日志中,我发现setDataSource的{​​{1}}部分失败了。从您的日志中

Create

作为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) 的一部分,播放器引擎实现等待prepare。如果基础播放器引擎无法设置finishSetDataSource,则会返回与DataSourceUNKNOWN_ERROR对应的0x80000000

我认为问题源于传递给-2147483648的字符串。从上面复制的日志中,字符串是setDataSource,我觉得它与文件或流媒体源的任何已知实现方法都不匹配。

要解决您的问题,我建议您在输入字符串中添加/data/data/com.jordan.tester/files/temp.3gp,即传递给create的字符串可以是file://,也可以用Java本身打开文件并传递文件描述符到file:///data/data/com.jordan.tester/files/temp.3gp