文档

RTC 相关 API

更新时间:

本文主要介绍了 Android SDK 中 RTC 相关的 API。

创建房间

 public void createRoom(Context context, 
                        final final IdrsSdk.SdkInitCallBack sdkInitCallBack)

请求参数

参数

类型

说明

是否必须

context

Context

上下文

sdkInitCallBack

SdkInitCallBack

创建的房间的回调接口

返回值

代码示例

  IdrsSdk.getInstance().createRoom(this, new IdrsSdk.SdkInitCallBack() {
            @Override
            public void success() {
                //成功
                
            }

            @Override
            public void fail(String message) {
                //失败

            }
        });

加入房间

public void joinRoom(Context context, String roomId, String token, 
                     final IdrsSdk.SdkInitCallBack sdkInitCallBack)

请求参数

参数

类型

说明

是否必须

context

Context

上下文

roomId

String

房间号

token

String

房间密码

sdkInitCallBack

SdkInitCallBack

创建的房间的回调接口

返回值

代码示例

IdrsSdk.getInstance().joinRoom(this, roomId, roomToken, sdkInitCallBack);

获取房间信息

public RoomInfo getRoomInfo()

请求参数

返回值

类型

说明

RoomInfo

返回房间的实体类

代码示例

IdrsSdk.getInstance().getRoomInfo();

设置房间信息

public void setRoomInfo(RoomInfo roomInfo)

请求参数

参数

类型

说明

是否必须

roomInfo

RoomInfo

房间实体类

返回值

代码示例

IdrsSdk.getInstance().setRoomInfo(roomInfo);

获取ARTVC实例

public AlipayRtcEngine getRtcEngineInstance()

请求参数

返回值

类型

说明

AlipayRtcEngine

ARTVC 实例

代码示例

  mRtcEngine = IdrsSdk.getInstance().getRtcEngineInstance();

设置 RTC 监听和处理

public void setRtcListenerAndHandler(AlipayRtcEngineEventListener listener,
                                     Handler handler)

请求参数

参数

类型

说明

是否必须

listener

AlipayRtcEngineEventListener

RTC 引擎事件监听器

handler

Handler

处理消息

返回值

代码示例

  mRtcEngine.setRtcListenerAndHandler(alipayRtcEngineEventListener, eventHandler);
    private final AlipayRtcEngineEventListener alipayRtcEngineEventListener = new AlipayRtcEngineEventListener() {
        @Override
        public void onError(int i, String s, Bundle bundle) {
            Log.d(TAG, "onError() called with: i = [" + i + "], s = [" + s + "], bundle = [" + bundle + "]");
        }

        @Override
        public void onCommonEvent(int i, String s, Bundle bundle) {
            Log.d(TAG, "onCommonEvent() called with: i = [" + i + "], s = [" + s + "], bundle = [" + bundle + "]");
        }

        @Override
        public void onRoomInfo(final RoomInfo roomInfo) {
          
        }

        @Override
        public void onEnterRoom(int i) {
            Log.d(TAG, "onEnterRoom() called with: i = [" + i + "]");
        }

        @Override
        public void onLeaveRoom(int i) {
            Log.d(TAG, "onLeaveRoom() called with: i = [" + i + "]");
        }

        @Override
        public void onInviteReply(ReplyOfInviteInfo replyOfInviteInfo) {
            Log.d(TAG, "onInviteReply() called with: replyOfInviteInfo = [" + replyOfInviteInfo + "]");
        }

        @Override
        public void onRecordInfo(String s) {
            Log.d(TAG, "onRecordInfo() called with: s = [" + s + "]");
        }

        @Override
        public void onCameraPreviewInfo(final ARTVCView artvcView) {
          
        }

        @Override
        public void onCameraPreviewFirstFrame() {
            Log.d(TAG, "onCameraPreviewFirstFrame() called");
        }

        @Override
        public void onCameraPreviewStop() {
            Log.d(TAG, "onCameraPreviewStop() called");
        }

        @Override
        public void onPublishEvent(PublishConfig publishConfig, PublishEventCode publishEventCode, String s, FeedInfo feedInfo) {
            Log.d(TAG, "onPublishEvent() called with: publishConfig = [" + publishConfig + "], publishEventCode = [" + publishEventCode + "], s = [" + s + "], feedInfo = [" + feedInfo + "]");
        }

        @Override
        public void onUnpublishEvent(UnpublishConfig unpublishConfig, PublishEventCode publishEventCode, String s) {
            Log.d(TAG, "onUnpublishEvent() called with: unpublishConfig = [" + unpublishConfig + "], publishEventCode = [" + publishEventCode + "], s = [" + s + "]");
        }

        @Override
        public void onSubscribeEvent(FeedInfo feedInfo, SubscribeEventCode subscribeEventCode, String s, ARTVCView artvcView) {
            Log.d(TAG, "onSubscribeEvent() called with: feedInfo = [" + feedInfo + "], subscribeEventCode = [" + subscribeEventCode + "], s = [" + s + "], artvcView = [" + artvcView + "]");
        }

        @Override
        public void onUnsubscribeEvent(FeedInfo feedInfo, SubscribeEventCode subscribeEventCode, String s) {
            Log.d(TAG, "onUnsubscribeEvent() called with: feedInfo = [" + feedInfo + "], subscribeEventCode = [" + subscribeEventCode + "], s = [" + s + "]");
        }

        @Override
        public void onParticipantsEnter(final List<ParticipantInfo> infos) {
          
        }

        @Override
        public void onParticipantsLeave(List<ParticipantLeaveInfo> list) {
            Log.d(TAG, "onParticipantsLeave() called with: list = [" + list + "]");
        }

        @Override
        public void onPublishNotify(FeedInfo feedInfo) {
            Log.d(TAG, "onPublishNotify() called with: feedInfo = [" + feedInfo + "]");
        }

        @Override
        public void onUnpublishNotify(FeedInfo feedInfo) {
            Log.d(TAG, "onUnpublishNotify() called with: feedInfo = [" + feedInfo + "]");
        }

        @Override
        public void onSubscribeNotify(FeedInfo feedInfo, String s) {
            Log.d(TAG, "onSubscribeNotify() called with: feedInfo = [" + feedInfo + "], s = [" + s + "]");
        }

        @Override
        public void onUnsubscribeNotify(FeedInfo feedInfo, String s) {
            Log.d(TAG, "onUnsubscribeNotify() called with: feedInfo = [" + feedInfo + "], s = [" + s + "]");
        }

        @Override
        public void onRemoteViewFirstFrame(final FeedInfo feedInfo, final ARTVCView artvcView) {
           
        }

        @Override
        public void onRemoteViewStop(FeedInfo feedInfo, ARTVCView artvcView) {
            Log.d(TAG, "onRemoteViewStop() called with: feedInfo = [" + feedInfo + "], artvcView = [" + artvcView + "]");
        }

        @Override
        public void onCurrentNetworkType(int i) {
            Log.d(TAG, "onCurrentNetworkType() called with: i = [" + i + "]");
        }

        @Override
        public void onCurrentAudioPlayoutMode(int i) {
            Log.d(TAG, "onCurrentAudioPlayoutMode() called with: i = [" + i + "]");
        }

        @Override
        public void onBandwidthImportanceChangeNotify(boolean b, double v, FeedInfo feedInfo) {
            Log.d(TAG, "onBandwidthImportanceChangeNotify() called with: b = [" + b + "], v = [" + v + "], feedInfo = [" + feedInfo + "]");
        }

        @Override
        public void onSnapShotComplete(Bitmap bitmap, FeedInfo feedInfo) {
            Log.d(TAG, "onSnapShotComplete() called with: bitmap = [" + bitmap + "], feedInfo = [" + feedInfo + "]");
        }

        @Override
        public void onStatisticDebugInfo(StatisticInfoForDebug statisticInfoForDebug, FeedInfo feedInfo) {
            Log.d(TAG, "onStatisticDebugInfo() called with: statisticInfoForDebug = [" + statisticInfoForDebug + "], feedInfo = [" + feedInfo + "]");
        }

        @Override
        public void onRealTimeStatisticInfo(RealTimeStatisticReport realTimeStatisticReport, FeedInfo feedInfo) {
            Log.d(TAG, "onRealTimeStatisticInfo() called with: realTimeStatisticReport = [" + realTimeStatisticReport + "], feedInfo = [" + feedInfo + "]");
        }
    };

设置 RTC 音频监听器

public void setAlipayRtcEngineAudioListener(
    AlipayRtcEngineAudioListener rtcEngineAudioListener, 
    @Nullable Handler handler)

请求参数

参数

类型

说明

是否必须

rtcEngineAudioListener

AlipayRtcEngineAudioListener

RTC 音频监听器

handler

Handler

处理消息

返回值

代码示例

   AlipayRtcEngineAudioListener audioListener = new AlipayRtcEngineAudioListener() {
                    @Override
                    public void onWebRtcAudioCapturered(AlipayRtcAudioInfo alipayRtcAudioInfo) {
                        super.onWebRtcAudioCapturered(alipayRtcAudioInfo);
                        if (alipayRtcAudioInfo != null) {
                            ByteArrayInputStream fis = new ByteArrayInputStream(alipayRtcAudioInfo.data);
                            ByteArrayOutputStream fos = new ByteArrayOutputStream(alipayRtcAudioInfo.data.length);
                            try {
                                new SSRC(fis, fos, 48000, 16000, 2, 2, 1, Integer.MAX_VALUE, 0, 0,
                                    true);
                                mAudioSpliter.splitAudioData(fos.toByteArray());
                            } catch (Throwable t) {
                                t.printStackTrace();
                            }
                        }
                    }
                };
                audioListener.setEnableAudioCaptured(true);
                mRtcEngine.setAlipayRtcEngineAudioListener(audioListener, new Handler());

设置 RTC 事件监听器

public void setRtcEventExtendListener(AlipayRtcEngineEventExtendListener listener)

请求参数

参数

类型

说明

是否必须

listener

AlipayRtcEngineEventExtendListener

RTC 事件监听器

返回值

代码示例

                mRtcEngine.setRtcEventExtendListener(new AlipayRtcEngineEventExtendListener() {
                    @Override
                    public void onVideoSizeChangedTo(int height, int width, ARTVCView view, FeedInfo feedInfo) {
                        super.onVideoSizeChangedTo(height, width, view, feedInfo);
                    }

                    @Override
                    public void onCustomRawResp(long requestId, int oldOpcmd, JSONObject jsonObject) {
                        super.onCustomRawResp(requestId, oldOpcmd, jsonObject);
                        if (jsonObject == null) {
                            return;
                        }

                        Log.i("CallActCustom", "cmd result: " + jsonObject.toJSONString());
                        int opcmdInner = jsonObject.getIntValue("opcmd");
                        String recordId = jsonObject.getString("recordId");
                        mRecordId = jsonObject.getString("recordId");
                        if (opcmdInner == 1032) {

                            recordIdList.add(mRecordId);
                            mSessionId = jsonObject.getString("sessionId");
                            recordAtLong = System.currentTimeMillis();
                            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
                            recordAt = df.format(recordAtLong);
                            Log.d(TAG, "recordAt: " + recordAt);
                        } else if (opcmdInner == 1034) {
                            long recordDuration = System.currentTimeMillis() - recordAtLong;
                            UploadReportRequest reportRequest = new UploadReportRequest();
                            reportRequest.setAppId(appId);
                            reportRequest.setRoomId(mRoomInfo.roomId);
                            reportRequest.setType("REMOTE");
                            reportRequest.setRecordAt(recordAt);
                            reportRequest.setDuration(recordDuration);
                            String metaFileName = "remotemeta";
                            GetAppBean.DataBean data = IdrsSdk.getInstance().getAppData();
                            String metaFilePath = ConfigurationFile.getInstance().createMetaFilePath(CreateRoomActivity.this, metaFileName);
                            VideoConfigurator.getInstance().saveToFile(metaFilePath);
                            String resultFilename = "remoteresult";
                            String resultFilePaht = ConfigurationFile.getInstance().createMetaFilePath(CreateRoomActivity.this, resultFilename);
                            VideoConfigurator.getInstance().saveResultToFile(resultFilePaht);

                            IdrsProcessResultBean idrsResultBean = new IdrsProcessResultBean();
                            idrsResultBean.setAk(AK);
                            idrsResultBean.setSk(SK);
                            idrsResultBean.setAppId(appId);
                            idrsResultBean.setFeeId(data == null ? "" : data.getFeeId());
                            idrsResultBean.setType(IdrsSdk.TYPE_REMOTE);
                            idrsResultBean.setRoomId(mRoomInfo.roomId);
                            idrsResultBean.setDuration(recordDuration);
                            idrsResultBean.setRecordAt(recordAtLong);
                            idrsResultBean.setRecordId(recordId);
                            List<FileBean> fileBeanList = new ArrayList<>();
                            idrsResultBean.setFiles(fileBeanList);
                            FileBean metaBean = new FileBean();
                            metaBean.setFileName(metaFileName);
                            metaBean.setUrlType(IdrsProcessResultBean.FILE_TYPE_META);
                            metaBean.setFilePath(metaFilePath);
                            fileBeanList.add(metaBean);

                            FileBean resultBean = new FileBean();
                            resultBean.setFileName(resultFilename);
                            resultBean.setUrlType(IdrsProcessResultBean.FILE_TYPE_RESULT);
                            resultBean.setFilePath(resultFilePaht);
                            fileBeanList.add(resultBean);

                            IdrsSdk.uploadFiles(idrsResultBean, new UploadCallback() {

                                @Override
                                public void success(String json) {
                                    runOnUiThread(new Runnable() {
                                        @Override
                                        public void run() {
                                            Toast.makeText(CreateRoomActivity.this, "上报成功", Toast.LENGTH_LONG).show();
                                        }
                                    });
                                }

                                @Override
                                public void fail(String msg) {
                                    runOnUiThread(new Runnable() {
                                        @Override
                                        public void run() {
                                            Toast.makeText(CreateRoomActivity.this, "上报失败", Toast.LENGTH_LONG).show();
                                        }
                                    });
                                }
                            });
                            Log.i("CallActCustom", "stop record result: " + jsonObject.toJSONString());
                        }
                    }
                });

开启录制

public void startRTCRecord(String waterMarkId)

请求参数

参数

类型

说明

是否必须

waterMarkId

String

水印 ID

返回值

代码示例

IdrsSdk.getInstance().startRTCRecord(waterMarkId);

开启录制扩展

public Long startRTCRecord(String waterMarkId, String tagFilter, 
                           Map<String, String> config, String userTag,
                           IdrsSdk.RecordType recordType, RecordParam recordParamObj)

请求参数

参数

类型

说明

是否必须

waterMarkId

String

水印 ID,没有时传 null

tagFilter

String

混流传 null,单流传 tag

config

Map<String, String>

自定义配置,可传 null

userTag

String

用户类型

recordType

IdrsSdk.RecordType

录制类型,可传 null

recordParamObj

RecordParam

自定义水印实体列

返回值

类型

说明

Long

返回的成功码

代码示例

    RecordParam recordParam = new RecordParam();
                    recordParam.setHeight(720);
                    recordParam.setWidth(1280);
                    JSONArray overlaps = new JSONArray();
                    JSONObject overlap_1 = new JSONObject();
                    overlap_1.put("enable", true);
                    overlap_1.put("fontSize", 60);
                    overlap_1.put("id", 777);
                    overlap_1.put("tag", videoTag);
                    overlap_1.put("text", customWatermark);
                    overlap_1.put("type", 2);
                    overlap_1.put("xPosition", 30);
                    overlap_1.put("yPosition", 30);
                    JSONObject overlap_2 = new JSONObject();
                    overlap_2.put("enable", true);
                    overlap_2.put("fontSize", 60);
                    overlap_2.put("id", 888);
                    overlap_2.put("tag", videoTag);
                    overlap_2.put("text", customWatermark);
                    overlap_2.put("type", 2);
                    overlap_2.put("xPosition", 30);
                    overlap_2.put("yPosition", 30);
                    overlaps.add(overlap_1);
                    overlaps.add(overlap_2);
                    recordParam.setOverlaps(overlaps, null);

                    if(rtc_single){//单流
                        IdrsSdk.getInstance().startRTCRecord(null, tagFilter, null, "idrs_test", IdrsSdk.RecordType.RECORD_SINGLE, recordParam);
                    }
                    if (rtc_hybrid){//混流
                        IdrsSdk.getInstance().startRTCRecord(null, null, null, "idrs_test", IdrsSdk.RecordType.RECORD_MIX, recordParam);
                    }

变更录制配置

public Long modifyRTCRecordConfig(String recordId, 
                                  TagPositions tagPositions, Overlaps overlaps)

请求参数

参数

类型

说明

是否必须

recordId

String

录制 ID

tagPositions

TagPositions

布局配置

overlaps

Overlaps

水印配置

返回值

类型

说明

Long

返回的成功码

代码示例

IdrsSdk.getInstance().modifyRTCRecordConfig(recordId,tagPositions,overlaps);

结束录制

 public void stopRTCRecord(String recordId)

请求参数

参数

类型

说明

是否必须

recordId

String

录制 ID,在开启 RTC 事件回调接口中返回

返回值

代码示例

IdrsSdk.getInstance().stopRTCRecord(recordId);

退出房间

public void release()

请求参数

返回值

代码示例

IdrsSdk.getInstance().release()