直播场景
TRTC场景和角色
一般业务场景中,并不是所有用户都需要发布流,部分用户只需要订阅流。
TRTC 中有两种场景:
rtc 场景:默认场景。 rtc 场景中所有用户均为主播,该场景中不存在观众角色。
主播 可以推流。一个房间最多支持50个主播同时推流。
观众无法推流。观众数量没有限制。
主播端
await trtc.enterRoom({scene: TRTC.TYPE.SCENE_LIVE,role: TRTC.TYPE.ROLE_ANCHOR,sdkAppId,userId,userSig,roomId});
观众端
作为观众进入房间
将角色参数设置为TRTC.TYPE.ROLE_AUDIENCE。
await trtc.enterRoom({scene: TRTC.TYPE.SCENE_LIVE,role: TRTC.TYPE.ROLE_AUDIENCE,sdkAppId,userId,userSig,roomId});
播放远程音频
默认情况下,SDK会自动播放远程音频,您无需调用任何API即可播放远程音频。
如果您不希望 SDK 自动播放音频,可以尝试以下方法:
trtc.on(TRTC.EVENT.REMOTE_AUDIO_AVAILABLE, event => {// Call this api when you need to play remote audio.trtc.muteRemoteAudio(event.userId, false);// Stop remote audiotrtc.muteRemoteAudio(event.userId, true);})// SettingautoReceiveAudio = falseto turn off automatic audio playback.await trtc.enterRoom({ ..., autoReceiveAudio: false });
播放远程视频
1. 进入房间前监听 TRTC.EVENT.REMOTE_VIDEO_AVAILABLE 事件,以接收所有远程用户视频发布事件。
2. 收到该事件后,调用 trtc.startRemoteVideo() 播放远端视频流。
trtc.on(TRTC.EVENT.REMOTE_VIDEO_AVAILABLE, ({ userId, streamType }) => {// To play the video image, you need to place an HTMLElement in the DOM,// which can be a div tag, assuming its id is `${userId}_${streamType}`const view = `${userId}_${streamType}`;trtc.startRemoteVideo({ userId, streamType, view });});
如何切换角色
// Switch to the anchorawait trtc.switchRole(TRTC.TYPE.ROLE_ANCHOR);// Turn on mic and camera, then the other anchor in the room will able to receive your audio and video.await trtc.startLocalAudio();await trtc.startLocalVideo();// Switch back to the audience when the calling is endedawait trtc.switchRole(TRTC.TYPE.ROLE_AUDIENCE);
