MediaRecorder实现setOnInfoListener -max持续时间

时间:2011-08-24 23:36:58

标签: android mediarecorder

我正在使用mediarecorder通过MIC捕获音频。我已将最长持续时间设置为20秒。录制会自动停止,并且不会在setOnInfoListener内的断点处停止。

**UPDATE: Changed my code according to suggestion but still doesnt stop at the breakpoint inside the listener.**

mRecorder.reset();
mRecorder.setOnInfoListener(new OnInfoListener() {
    @Override
    public void onInfo(MediaRecorder mr, int what, int extra) {                     
        if (what == MediaRecorder.MEDIA_RECORDER_INFO_MAX_DURATION_REACHED) {
            mRecorder.stop();

        }          
    }
});
mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mRecorder.setAudioSamplingRate(8000);
mRecorder.setOutputFormat(MediaRecorder.OutputFormat.RAW_AMR);
mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
mRecorder.setOutputFile(fileName);
mRecorder.setMaxDuration(20000);
try {
    mRecorder.prepare();
} catch(IOException exception) {
    mRecorder.reset();
    mRecorder.release();
    mRecorder = null;
    return;
}
mRecorder.start();

有人可以告诉我为什么代码没有在侦听器中点击我的onInfo方法而是默默地停止录制。

由于

4 个答案:

答案 0 :(得分:4)

设置输出格式时,请尝试使用THREE_GPP而不是RAW_AMR。

mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);

根据setOutputFormat()的{​​{3}}:

It is recommended to always use 3GP format when using the
H.263 video encoder and AMR audio encoder. Using an MPEG-4
container format may confuse some desktop players.

答案 1 :(得分:3)

在致电setOnInfoListener()之前,请尝试将来电转至prepare()

在我自己的视频捕获代码中,我在创建setOnInfoListener()对象后立即调用MediaRecorder。在您的代码示例中,一个好地方可能就在reset()之后和setAudioSource()之前。

否则,OnInfoListener类的正文看起来是正确的。

我已经从我的应用添加了MediaRecorder设置代码,该代码可以正常工作。

try {
    mCamera.unlock();

    mMediaRecorder = new MediaRecorder();
    mMediaRecorder.setOnErrorListener( new VideoRecorderErrorListener() );
    mMediaRecorder.setOnInfoListener( new VideoRecorderInfoListener() );

    // As per Android API docs, the ordering of the following initialization
    // calls is important.
    mMediaRecorder.setCamera(mCamera);
    mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
    mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);

    mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
    mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
    mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);

    mMediaRecorder.setOutputFile( mOutputFilePath );

    mMediaRecorder.setMaxFileSize(VIDEO_MAX_FILE_SIZE);
    mMediaRecorder.setAudioChannels(AUDIO_CHANNELS);
    mMediaRecorder.setAudioSamplingRate(AUDIO_SAMPLING_RATE);
    mMediaRecorder.setAudioEncodingBitRate(AUDIO_ENCODING_BIT_RATE);
    mMediaRecorder.setMaxDuration(VIDEO_MAX_DURATION);
    mMediaRecorder.setVideoFrameRate(mPictureFPS);
    mMediaRecorder.setVideoEncodingBitRate(VIDEO_ENCODING_BIT_RATE);
    mMediaRecorder.setVideoSize(mPreviewWidth, mPreviewHeight);

    mMediaRecorder.setPreviewDisplay(mHolder.getSurface());

    mMediaRecorder.prepare();

    mMediaRecorder.start();
} catch (IllegalStateException e) {

答案 2 :(得分:0)

一旦达到最大持续时间,它就会调用(自定义方法)stopRecording():在这里我们可以处理所有停止录制并释放播放器相机和预览。

 myRecorder = new MediaRecorder();
 myRecorder.setOnInfoListener(new MediaRecorder.OnInfoListener() {
            @Override
            public void onInfo(MediaRecorder mr, int what, int extra) {
                if (what == MediaRecorder.MEDIA_RECORDER_INFO_MAX_DURATION_REACHED) {
                    stopRecording();
                }
            }
        });

答案 3 :(得分:-1)

u将MediaRecorder mr对象传递到您的方法中,但是您没有使用它。尝试mr.stop();