2012-04-13 31 views
8

Tôi đã đối mặt với một hành vi rất lạ: đôi khi mediarecorder của tôi gặp sự cố "Lỗi dừng" và đôi khi hoạt động tốt. Có lỗi của tôi hay đó là lỗi của hệ thống? Tôi không thể hiểu sai.Dừng mediarecorder Android không thành công

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

Trả lời

8

Bạn có thể bắt một RuntimeException tại phương pháp MediaRecorder.stop().

Ví dụ:

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

add sau trong SurfaceCreated (giữ SurfaceHolder) của bạn:

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

điều này có nghĩa vụ gì để khắc phục? – StarShine

0

kinh nghiệm các lỗi tương tự: Đôi khi MediaRecorder của tôi bị rơi với một lỗi "Stop thất bại" và đôi khi nó làm việc tốt. Việc này đã giải quyết được sự cố của tôi:

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

thêm điều này vào chính xác? – StarShine

Các vấn đề liên quan