前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Android平台GB28181设备接入端如何实现本地录像?

Android平台GB28181设备接入端如何实现本地录像?

原创
作者头像
音视频牛哥
发布2022-10-04 01:23:30
3840
发布2022-10-04 01:23:30
举报

实现Android平台GB28181设备接入的时候,有个功能点不可避免,那就是本地录像,实际上,在实现GB28181设备接入模块之前,我们前些年做RTMP推送和轻量级RTSP服务的时候,早已经实现了本地录像功能。

本地录像功能,我们实现的主要控制接口如下:

音视频录制开关,为了更细粒度的控制录像,如只需要录纯音频还是纯视频,或者音视频,可以通过下述两个接口实现:

代码语言:javascript
复制
/**
   * 音频录制开关, 目的是为了更细粒度的去控制录像, 一般不需要调用这个接口, 这个接口使用场景比如同时推送音视频,但只想录制视频,可以调用这个接口关闭音频录制
   *
   * @param is_recoder: 0: do not recorder; 1: recorder; sdk默认是1
   *
   * @return {0} if successful
   */
  public native int SmartPublisherSetRecorderAudio(long handle, int is_recoder);

  /**
   * 视频录制开关, 目的是为了更细粒度的去控制录像, 一般不需要调用这个接口, 这个接口使用场景比如同时推送音视频,但只想录制音频,可以调用这个接口关闭视频录制
   *
   * @param is_recoder: 0: do not recorder; 1: recorder; sdk默认是1
   *
   * @return {0} if successful
   */
  public native int SmartPublisherSetRecorderVideo(long handle, int is_recoder);

创建并设置录像目录:

代码语言:javascript
复制
/**
     * Create file directory(创建录像存放目录)
     * 
     * @param path,  E.g: /sdcard/daniulive/rec
     * 
     * <pre> The interface is only used for recording the stream data to local side. </pre> 
     * 
     * @return {0} if successful
     */
    public native int SmartPublisherCreateFileDirectory(String path);
    
    /**
     * Set recorder directory(设置录像存放目录)
     * 
     * @param path: the directory of recorder file.
     * 
     * <pre> NOTE: make sure the path should be existed, or else the setting failed. </pre>
     * 
     * @return {0} if successful
     */
    public native int SmartPublisherSetRecorderDirectory(long handle, String path);

设置单个文件录制的大小:

代码语言:javascript
复制
    /**
     * Set the size of every recorded file(设置单个录像文件大小,如超过最大文件大小,自动切换到下个文件录制)
     * 
     * @param size: (MB), (5M~500M), if not in this range, set default size with 200MB.
     * 
     * @return {0} if successful
     */
    public native int SmartPublisherSetRecorderFileMaxSize(long handle, int size);

启动录像、暂停录像、停止录像,说到这里,好多开发者可能对暂停/恢复录像,比较感兴趣,实际上,我们在具体使用场景下,并不是所有的数据,都想录制下来,比如智慧教室,下课时间,我们无需录制,这时候,只要调用PauseRecorder来暂停录像,等上课后,恢复录像即可。

代码语言:javascript
复制
/**
    * Start recorder(开始录像)
    *
    * @return {0} if successful
    */
    public native int SmartPublisherStartRecorder(long handle);

    /**
     * Pause recorder(暂停/恢复录像)
     *
     * is_pause: 1表示暂停, 0表示恢复录像, 输入其他值将调用失败
     *
     * @return {0} if successful
     */
    public native int SmartPublisherPauseRecorder(long handle, int is_pause);

    /**
    * Stop recorder(停止录像)
    *
    * @return {0} if successful
    */
    public native int SmartPublisherStopRecorder(long handle);

具体调用如下:

代码语言:javascript
复制
class ButtonStartRecorderListener implements View.OnClickListener {
        public void onClick(View v) {
            if (isRecording) {
                stopRecorder();

                if (!isPushingRtmp && !isRTSPPublisherRunning && !isGB28181StreamRunning) {
                    ConfigControlEnable(true);
                }

                btnStartRecorder.setText("实时录像");
                isRecording = false;

                btnPauseRecorder.setText("暂停录像");
                btnPauseRecorder.setEnabled(false);
                isPauseRecording = true;

                return;
            }

            Log.i(TAG, "onClick start recorder..");

            if (libPublisher == null)
                return;

            if (!isPushingRtmp && !isRTSPPublisherRunning&& !isGB28181StreamRunning) {
                InitAndSetConfig();
            }

            ConfigRecorderParam();

            int startRet = libPublisher.SmartPublisherStartRecorder(publisherHandle);
            if (startRet != 0) {
                isRecording = false;

                Log.e(TAG, "Failed to start recorder.");
                return;
            }

            if (!isPushingRtmp && !isRTSPPublisherRunning && !isGB28181StreamRunning) {
                CheckInitAudioRecorder();
                ConfigControlEnable(false);
            }

            startLayerPostThread();

            btnStartRecorder.setText("停止录像");
            isRecording = true;

            btnPauseRecorder.setEnabled(true);
            isPauseRecording = true;
        }
    }

实际上,本地录像和GB28181上去的数据,是同一路编码数据,当然,如果做的更智能一些,也可以分两个实例来实现,一路编码用于GB28181平台接入,一路高分辨率帧率,用于本地录制,具体根据使用场景定制即可。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
云直播
云直播(Cloud Streaming Services,CSS)为您提供极速、稳定、专业的云端直播处理服务,根据业务的不同直播场景需求,云直播提供了标准直播、快直播、云导播台三种服务,分别针对大规模实时观看、超低延时直播、便捷云端导播的场景,配合腾讯云视立方·直播 SDK,为您提供一站式的音视频直播解决方案。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档