0

MediaRecorder 允许设置持续时间和文件大小,但我还没有弄清楚如何获取回调事件进行处理。通过密切关注此处的文档,我已经开始录制并运行:

http://developer.android.com/guide/topics/media/camera.html

我已经实现了MediaRecorder.OnInfoListener,我猜这将是监听这两个事件的那个。

public class CameraActivity extends Activity implements MediaRecorder.OnInfoListener {
    /* code here */
}

我添加了我想要的持续时间mMediaRecorder.setMaxDuration(5000);

最后,我为 OnInfo 侦听器设置了所需的回调方法,但它似乎永远不会触发。

@Override
public void onInfo(MediaRecorder arg0, int arg1, int arg2) {
    Log.i("CALLBACK", "Response Code: " + arg1);
}
4

2 回答 2

0

可能的问题是您没有将 OnInfoListener 分配给 MediaRecorder 对象。

在初始化 MediaRecorder 对象调用“setOnInfoListener(X)”并为变量 X 设置监听器之后。

例子:

    public class CameraActivity extends Activity implements MediaRecorder.OnInfoListener {
        /* some other code */

        @Override
        public void onInfo(MediaRecorder mediaRecorder, int i, int i1) {
            // Maximum Duration Reached
            if (i == MediaRecorder.MEDIA_RECORDER_INFO_MAX_DURATION_REACHED) {
                //DO SOMETHING
            }
        }

        @Override
        public void onCreate(Bundle savedInstanceState) {
            /* some other code */

            // Initialize MediaRecorder and assign listener
            MediaRecorder mediaRecorder = new MediaRecorder();
            // Required max length
            mediaRecorder.setMaxDuration(VAL_MAX_RECORD_LENGTH);
            // Set listener for MediaRecorder
            mediaRecorder.setOnInfoListener(this);
        }
    }   
                                               
于 2022-03-03T04:03:56.160 回答
0

recorder.setOnInfoListener(this);在实现 MediaRecorder.OnInfoListener 接口之后设置 MediaRecoder 监听器

要获取 maxDuration 和 maxFileSize 回调,请使用 onInfo 回调,如下所示

@Override
public void onInfo(MediaRecorder mr, int what, int extra) {
     if (what == MediaRecorder.MEDIA_RECORDER_INFO_MAX_DURATION_REACHED) {

         Log.e("Maximum Duration Reached","Maximum Duration Reached"); 
      } 
     if (what == MediaRecorder.MEDIA_RECORDER_INFO_MAX_FILESIZE_REACHED) {

         Log.e("Maximum File size Reached","Maximum File size Reached"); 
      } 
于 2016-07-28T10:15:57.810 回答