我无法播放使用MediaRecorder录制的文件

时间:2013-07-31 16:12:45

标签: java android eclipse android-mediaplayer android-mediarecorder

我正在使用MediaRecoder录制声音,但在录制完成后无法播放。我尝试使用Google Play音乐,ES媒体播放器,甚至将其上传到电脑,并尝试用Winamp打开它。没什么可玩的!

//AUDIO RECORDER
    recorder = new MediaRecorder();
    recorder.reset();       
    recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
    recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
    recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
    if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))
    {
        externalStoragePath = Environment.getExternalStorageDirectory().getAbsolutePath();
        externalOutputPath = externalStoragePath + File.separator + "/Android/data/com.whizzappseasyvoicenotepad/test.mp3";
        recorder.setOutputFile(externalOutputPath);
    }
    else
    {
        storagePath = Environment.getDataDirectory().getAbsolutePath();
        recorder.setOutputFile(storagePath + "/Android/data/com.whizzappseasyvoicenotepad/test.mp3");
    }

甚至尝试用按钮点击在应用程序中打开它:

public void testPlay (View v) throws IllegalArgumentException, SecurityException, IllegalStateException, IOException{
    mediaPlayer = new MediaPlayer();
    mediaPlayer.setDataSource(externalOutputPath);
    mediaPlayer.prepare();
    mediaPlayer.start();
}

但是这会导致应用程序崩溃。但这不是主要问题,主要问题是我无法播放该文件。编码或其他什么问题?

我也尝试将它从.mp3更改为.3gp,但它无法正常工作。然后我也尝试删除.mp3和.3gp,只留下'test'作为名称,然后它甚至不把它识别为音频文件。

哦,如果有人想要在应用程序崩溃时使用logcat:

07-31 16:51:43.953: E/AndroidRuntime(26918): java.lang.IllegalStateException: Could not execute method of the activity
07-31 16:51:43.953: E/AndroidRuntime(26918): Caused by: java.lang.reflect.InvocationTargetException
07-31 16:51:43.953: E/AndroidRuntime(26918): Caused by: java.io.IOException: setDataSourceFD failed.: status=0x80000000

但同样,应用程序崩溃目前不是问题。首先,我想解决无法播放音频文件的问题。但是如果你知道为什么它会崩溃我也会很感激!

1 个答案:

答案 0 :(得分:1)

我仍然不确定是什么问题,但我认为我停止录制的方式有问题(整个尝试,抓住事情)。我只是重写了整个代码,并将MediaRecorder放在两种不同的方法中。 startRecording()和stopRecording()方法。现在它完美无缺!

的startRecording()

public void startRecording (){
     recorder = new MediaRecorder();
     recorder.reset();      
     recorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
     recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
     recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
     if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))
     {
         externalStoragePath = Environment.getExternalStorageDirectory().getAbsolutePath();
         externalOutputPath = externalStoragePath + File.separator + "/Android/data/com.whizzappseasyvoicenotepad/test.mp3";
         recorder.setOutputFile(externalOutputPath);
     }
     else
     {
        storagePath = Environment.getDataDirectory().getAbsolutePath();
        recorder.setOutputFile(storagePath + "/Android/data/com.whizzappseasyvoicenotepad/test.mp3");
     }
     recorder.setOnErrorListener(errorListener);
     recorder.setOnInfoListener(infoListener);

     try {
        recorder.prepare();
        recorder.start();
    } catch (IllegalStateException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

stopRecording()

public void stopRecording() {
    if (null != recorder) {
        recorder.stop();
        recorder.reset();
        recorder.release();

        recorder = null;
    }
}