实时音视频 SPARKRTC-SDK使用

时间:2024-07-04 11:53:25

SDK使用

  1. 创建引擎。

    AppId获取方法请参见创建应用

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    HRTCEngineConfig config = new HRTCEngineConfig();
    config.setAppId(appId); // AppId需在控制台中创建应用后获取
    config.setCountryCode(countryCode); // 可以根据Grs国家码对照表传值,建议传"CN"
    config.setContext(getApplicationContext()); // 上下文,请传入Application Context
    config.setDomain(webSocketFalvor); // 该字段已废弃,不需要再传值
    config.setMuteAudioRoute(isMuteAudioRoute);
    config.setLogEnable(true);
    config.setLogSize(logSize);
    config.setLogLevel(logLevel);
    config.setLogPath(logPath); // logPath为目录,非文件
    mHwRtcEngine = HRTCEngine.create(config, mHwHandler); // mHwHandler继承自IHRTCEngineEventHandler,用于监听各种回调事件
    
    设置本地窗口。
    1
    2
        SurfaceView surface = mHwRtcEngine.createRenderer(getApplicationContext()); // 不可使用new SurfaceView(context)创建
        mHwRtcEngine.setupLocalView(surface, HRTCEnums.HRTCVideoDisplayMode.HRTC_VIDEO_DISPLAY_MODE_HIDDEN);
    
    加入房间。
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    HRTCJoinParam joinParam = new HRTCJoinParam();
    joinParam.setUserId(mUserId); // userId用于标识同一房间的不同用户
    joinParam.setUserName(mUserName); // 用户昵称,如无特殊需求,保持和userId一致即可
    joinParam.setRole( HRTCJoinParam.HRTCRoleType.HRTC_ROLE_TYPE_JOINER);
    joinParam.setRoomId(roomid);
    joinParam.setScenario(1);
    joinParam.setOptionalInfo(optionInfo);
    joinParam.setAuthorization(signature);  
    joinParam.setCtime(ctime);
    joinParam.setAutoSubscribeVideo(false);
    joinParam.setAutoSubscribeAudio(true);
    mHwRtcEngine.joinRoom(joinParam );
    

    joinParam:入会参数,包含用户ID、用户名、房间号、认证信息、ctime、是否自动订阅音频和视频、SFU类型、场景和用户角色,具体请参见HRTCJoinParam

  2. 监听远端用户加入房间,并设置远端窗口。

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    @Override
    public void onRemoteUserOnline(String roomId, final String userId, String nickname) {
        if (userId.equals(mUserId)||mRole == HRTC_ROLE_TYPE_JOINER.ordinal()) {
            return;
        }
        Log.e(TAG, "onRemoteUserOnline userId: " + userId);
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(LiveActivity.this, userId + "加入了房间", Toast.LENGTH_SHORT).show();
                SurfaceView surface = mHwRtcEngine.createRenderer(getApplicationContext());
                mHwRtcEngine.startRemoteStreamView(userId,surface,  HRTCEnums.HR TCS treamType.HRTC_STREAM_TYPE_HD);
                mHwRtcEngine.updateRemoteRenderMode(userId,HRTCEnums.HRTCVideoDisplayMode.HRTC_VIDEO_DISPLAY_MODE_Fit);
                // 将userId对应surface添加到布局中
            }  
        });
    }
    

  3. 监听远端用户离开房间,并删除远端窗口。

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    @Override
    public void onRemoteUserOffline(String roomId, final String userId, int reason) {
        Log.i(TAG, "HwRtcDemo onRemoteUserOffline roomId:" + roomId + ", userId:" + userId + ", reason:" + reason);
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Log.e(TAG, "HwRtcDemo run removeRemoteUser! ");
                Toast.makeText(LiveActivity.this, userId + "离开了房间", Toast.LENGTH_SHORT).show();
    
                mHwRtcEngine.stopRemoteStreamView(userId);
                // 将userId对应的surface从布局中移除
            }
        });
    }
    

  4. 离开房间。

    1
    2
    mHwRtcEngine.leaveRoom();
    finish();
    

  5. 销毁引擎

    1
    HRTCEngine.destroy();
    

support.huaweicloud.com/csdk-rtc/rtc_05_0008.html