2012-04-13 106 views
8

我遇到了一个非常奇怪的行为:有时我的媒体记录器崩溃,出现错误“停止失败”,有时它工作正常。是我的错,还是系统的错误? 我不能得到什么是错的。Android媒体录制器停止失败

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

回答

8

您可以赶上在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; 
} 
2

加入您的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); 
+0

这是应该解决的问题? – StarShine 2018-01-24 23:22:50

0

经历了同样的错误:有时候我MediaRecorder崩溃,并显示错误 “停止失败”,有时工作得很好。将这个解决我的问题:

@Override 
public void onStop() { 
    super.onStop(); 
    if (mRecorder != null) { 
     mRecorder.release(); 
     mRecorder = null; 
    } 
} 
+0

加上这个到底是什么? – StarShine 2018-01-24 23:23:05