安卓录音机停止失败

14

我遇到了一个非常奇怪的问题:有时我的媒体录制器会崩溃并显示“停止失败”的错误,有时它会正常工作。这是我的错还是系统的bug?我无法确定出错原因。

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个回答

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

它在Android 6.0.1上总是失败,但在7.*上运行良好。我使用SURFACE作为输入。 - user924

1

16
我如何检查 MediaRecorder 是否处于录制状态? - Kalpesh
@Kalpesh 很遗憾,MediaRecorder API没有提供查询其状态的功能。 - Bhargav

0
在你的SurfaceCreated(SurfaceHolder holder)中添加以下内容:
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);  

这个是用来修复什么问题的? - StarShine
@StarShine 这与问题无关,回答毫无用处。 - user924

-1

遇到了类似的错误:有时我的MediaRecorder会崩溃,并显示“无法停止”错误,有时却能正常工作。 添加以下内容解决了我的问题:

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

添加到什么地方? - StarShine
将其添加到包含MediaRecorder其余代码的类或活动中。 - CEO tech4lifeapps

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接