rtc
SDK使用
更新时间:2021/01/07 GMT+08:00
- 创建引擎。
1 2 3 4 5 6
try { //serverAddr:域名,appId:应用ID,getApplicationContext():上下文,this:IHRTCEngineEventHandler 的实现。 mHwRtcEngine = HRTCEngine.create(getApplicationContext(), serverAddr, appId, this); } catch (Exception e) { e.printStackTrace(); }
- 设置本地窗口。
1 2
SurfaceView surface = HRtcEngine.createRenderer(getApplicationContext()); mHwRtcEngine.setupLocalView(surface, HRTCEnums.HRTCVideoDisplayMode.HRTC_VIDEO_DISPLAY_MODE_HIDDEN);
- 加入房间,调测阶段直接使用appId加入,正式商用阶段建议使用签名鉴权。
1 2 3 4 5
HRTCUserInfo userInfo = new HRTCUserInfo(); userInfo.setUserId(mUserId); userInfo.setUserName(mUserName); userInfo.setRole( HRTCUserInfo.HRTCRoleType.HRTC_ROLE_TYPE_JOINER); mHwRtcEngine.joinRoom(userInfo, mRoomId, HRTCEnums.HRTCMediaType.HRTC_MEDIA_TYPE_VIDEO);
- userInfo:用户信息,包含用户ID、用户名和用户角色,具体请参见HRTCUserInfo。
- mRoomId:加入的房间。
- HRTCEnums.HRTCMediaType.HRTC_MEDIA_TYPE_VIDEO:加入房间的通话类型,音频+视频模式。
- 监听远端用户加入房间,并设置远端窗口。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
@Override public void onUserJoined(String roomId, final String userId, String nickname) { if (userId.equals(mUserId)||mRole == ROLE_TYPE_PUBLISER.ordinal()) {//ROLE_TYPE_PUBLISER为publisher 角色 return; } Log.e(TAG, "onUserJoined userId: " + userId); runOnUiThread(new Runnable() { @Override public void run() { Toast.makeText(LiveActivity.this, userId + "加入了房间", Toast.LENGTH_SHORT).show(); SurfaceView surface = HRTCEngine.createRenderer(getApplicationContext()); mHwRtcEngine.startRemoteStreamView(userId,surface, HRTCEnums.HRTCStreamType.HRTC_STREAM_TYPE_HD); mHwRtcEngine.setRemoteViewDisplayMode(userId,HRTCEnums.HRTCVideoDisplayMode.HRTC_VIDEO_DISPLAY_MODE_HIDDEN); mVideoGridContainer.addUserVideoSurface(userId, surface);//mVideoGridContainer 为视图容器 } }); }
- 监听远端用户离开房间,并删除远端窗口。
1 2 3 4 5 6 7 8 9 10 11 12 13 14
@Override public void onUserOffline(String roomId, final String userId, int reason) { Log.i(TAG, "HwRtcDemo onUserOffline 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); mVideoGridContainer.removeUserVideo(userId, false); } }); }
- 离开房间。
1 2
mHwRtcEngine.leaveRoom(); finish();
父主题: Android SDK
