MediaRecorder 实现 setOnInfoListener -max 持续时间
我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
设置输出格式时,请尝试使用 THREE_GPP 而不是 RAW_AMR。
根据 文档
setOutputFormat()
:When you set the output format, try using THREE_GPP instead of RAW_AMR.
According to the documentation for
setOutputFormat()
:尝试将调用移至
setOnInfoListener()
,然后再调用prepare()
。在我自己的视频捕获代码中,我在创建
MediaRecorder
对象后立即调用setOnInfoListener()
。在您的代码示例中,一个好的位置可能就在reset()
之后和setAudioSource()
之前。否则,
OnInfoListener
类的主体看起来是正确的。我已经从我的应用程序中添加了 MediaRecorder 设置代码,该代码可以正常工作。
Try moving your call to
setOnInfoListener()
before the call toprepare()
.In my own video capture code, I invoke
setOnInfoListener()
right after creating theMediaRecorder
object. In your code example, a good place might be right afterreset()
and beforesetAudioSource()
.Otherwise, the body of your
OnInfoListener
class looks correct.I've added the MediaRecorder setup code from my app, which does work correctly.
一旦达到最大持续时间,它将调用(自定义方法)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.
您将 MediaRecorder mr 对象传递给您的方法,但您没有使用它。尝试 mr.stop();
u passed the MediaRecorder mr object into your method but you didn't use it. Try mr.stop();