14

我遇到了一个非常奇怪的行为:有时我的 mediarecorder 因错误“停止失败”而崩溃,有时它工作正常。是我的错还是系统的错误?我不明白出了什么问题。

private void stopRecording(){
        ticker.cancel();
        ticker.purge();

        recorder.stop();

        startBtn.setText("Start");
        recordInProcess = false;

        markList = locWriteTask.getMarkArray();

    mCamera.lock();
        recorder.release();
    }

 private void startRecording(){

         startBtn.setText("Stop");

         recordInProcess = true;

             recorder = new MediaRecorder();

         mCamera.unlock();
         recorder.setCamera(mCamera);

         recorder.setPreviewDisplay(mSurfaceHolder.getSurface());
         recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
         recorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
         recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
         recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
         recorder.setVideoEncoder(MediaRecorder.VideoEncoder.MPEG_4_SP);
         recorder.setMaxDuration((int) 10000000); 
         recorder.setVideoSize(320, 240); 
         recorder.setVideoFrameRate(15); 
         recorder.setOutputFile(FULL_PATH_TO_LOCAL_FILE + counter + MP4);

         try{
             recorder.prepare();
         } catch (Exception e){
             finish();
         }

         lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, ll);

         ticker = new Timer();
         locWriteTask = new WriteTimeLocationTimerTask(ll);
         ticker.schedule(locWriteTask, 0, DELAY);   

         recorder.start();
    }
4

4 回答 4

10

您可能会在 MediaRecorder.stop() 方法中捕获 RuntimeException。

例子:

MediaRecorder mRecorder = new MediaRecorder();
File mFile = new File("The output file's absolutePath");

... //config the mRecorder
mRecorder.setOutputFile(mFile.getAbsolutePath());

... //prepare() ...
mRecorder.start();

try {
    mRecorder.stop();
} catch(RuntimeException e) {
    mFile.delete();  //you must delete the outputfile when the recorder stop failed.
} finally {
    mRecorder.release();
    mRecorder = null;
}
于 2012-11-07T06:51:28.487 回答
1

如果记录器未处于记录状态,则停止可能会失败。

http://developer.android.com/reference/android/media/MediaRecorder.html

于 2012-07-20T22:32:42.127 回答
0

在 SurfaceCreated(SurfaceHolder 支架)中添加以下内容:

CamcorderProfile camcorderProfile = CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH);  //get your own profile   
 Camera.Parameters parameters = mCamera.getParameters();  
 parameters.setPreviewSize(camcorderProfile.videoFrameWidth,camcorderProfile.videoFrameHeight);   
 mCamera.setParameters(parameters);  
于 2014-07-23T11:20:32.320 回答
-1

遇到同样的错误:有时我的 MediaRecorder 因错误“停止失败”而崩溃,有时它工作正常。添加这个解决了我的问题:

@Override
public void onStop() {
    super.onStop();
    if (mRecorder != null) {
        mRecorder.release();
        mRecorder = null;
    }
}
于 2017-07-12T06:34:15.200 回答