MediaRecorder 实现 setOnInfoListener -max 持续时间

发布于 2024-12-01 10:15:16 字数 1060 浏览 4 评论 0原文

我正在使用 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 方法而是默默地停止录制。

谢谢

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 stops recording.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

撧情箌佬 2024-12-08 10:15:16

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

mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);

根据 文档 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.

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.
最终幸福 2024-12-08 10:15:16

尝试将调用移至 setOnInfoListener(),然后再调用 prepare()

在我自己的视频捕获代码中,我在创建 MediaRecorder 对象后立即调用 setOnInfoListener()。在您的代码示例中,一个好的位置可能就在 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) {

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) {
勿挽旧人 2024-12-08 10:15:16

一旦达到最大持续时间,它将调用(自定义方法)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();
                }
            }
        });

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();
                }
            }
        });
很酷又爱笑 2024-12-08 10:15:16

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文