当setMaxFileSize()和setMaxDuration()组合使用时,MediaRecorder服务器死亡

时间:2019-05-20 17:14:29

标签: java android android-mediarecorder

当我设置MediaRecorder实例并且结合使用setMaxFileSize和setMaxDuration时,收到消息“ W / IMediaDeathNotifier:媒体服务器已死”,并且记录停止。我要完成的工作是重复录制,直到达到最大持续时间。

当我单独使用这些方法时,它们可以很好地工作。我可以设置持续时间或文件大小,并让onInfoListener进行回调。

mMediaRecorder = new MediaRecorder();

    mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
    mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.SURFACE);

    // Create a media file name
    @SuppressLint("SimpleDateFormat") final String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    final File outputFile = getRecordingFile(1,timeStamp);
    mMediaRecorder.setOutputFile(outputFile);
    Log.d(TAG, "OutputFile: " + outputFile);

    mMediaRecorder.setOrientationHint(0);
    mMediaRecorder.setProfile(CamcorderProfile.get(CameraMetadata.LENS_FACING_BACK, CamcorderProfile.QUALITY_1080P));

    totalRecordDuration = 360 * 1000;
    eachFileSize = 50 * 1000000;

    mMediaRecorder.setMaxFileSize(eachFileSize);
    mMediaRecorder.setMaxDuration(totalRecordDuration);


    //Listen to file size and other relevant info to continue recording
    mMediaRecorder.setOnInfoListener(new MediaRecorder.OnInfoListener() {
        private String TAG = "MediaRecorder info listener";
        int i = 2;

        String startTimeStamp = timeStamp;
        File currentOutputFile = outputFile;

        @Override
        public void onInfo(MediaRecorder mr, int what, int extra) {
            switch (what){
                case MediaRecorder.MEDIA_RECORDER_INFO_MAX_FILESIZE_APPROACHING:

                    try {
                        mr.setNextOutputFile(getRecordingFile(i, timeStamp));
                    } catch (IOException e) {
                        e.printStackTrace();
                    }

                    i++;
                    Log.e(TAG, "Max file size approaching");
                    break;

                case MediaRecorder.MEDIA_RECORDER_INFO_MAX_FILESIZE_REACHED:
                    Log.e(TAG, "Max file size reached");
                    //stopRecording();
                    break;

                case MediaRecorder.MEDIA_RECORDER_INFO_MAX_DURATION_REACHED:
                    //stopRecording();

                    Log.e(TAG,"Max duration reached");
                    break;

                case MediaRecorder.MEDIA_RECORDER_INFO_NEXT_OUTPUT_FILE_STARTED:
                    Log.e(TAG,"OutputFile:");
                    break;
            }
        }
    });

    mMediaRecorder.prepare();

0 个答案:

没有答案