开发者

MediaRecorder implementing setOnInfoListener -max duration

I 'm using mediarecorder to capture audio through MIC. I have set the max duration to 20 seconds. The recording stops automatically and does not stop at my break point inside 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();

Can someone please tell me why does the code not hit my onInfo method inside the listener rather silently开发者_C百科 stops recording.

Thanks


When you set the output format, try using THREE_GPP instead of RAW_AMR.

mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);

According to the documentation for setOutputFormat():

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.


Try moving your call to setOnInfoListener() before the call to prepare().

In my own video capture code, I invoke setOnInfoListener() right after creating the MediaRecorder object. In your code example, a good place might be right after reset() and before setAudioSource().

Otherwise, the body of your OnInfoListener class looks correct.

I've added the MediaRecorder setup code from my app, which does work correctly.

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) {


as soon as it will reach the maximum duration, it will call the (Customized Method) stopRecording(): Where we can handle all the stop recording and release player camera and preview.

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


u passed the MediaRecorder mr object into your method but you didn't use it. Try mr.stop();

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜