This tutorial mainly introduces how to implement the Live Streaming by using scene live. TRTC Scene and Roles
In general business scenarios, not all users need to publish streams, and some users only need to subscribe streams.
There are two types of scene in TRTC:
Scene live: Users in this scene can be divided into two roles, the anchor and the audience. The difference between the two roles is: The anchor can publish the stream. A room supports up to 50 anchors to publish streams at the same time. The audience can not publish the stream. There is no limit on the number of viewers. Scene rtc: All users in rtc scene are anchors, and there are no audience in this scene. Anchor Side
The process of implementing audio and video calls on the anchor side is basically the same as the implementation process of the rtc scene. Refer to: Basic Audio/Video Call. The main difference is the parameters scene and role set when calling trtc.enterRoom(). Refer to the following sample code: await trtc.enterRoom({
scene: TRTC.TYPE.SCENE_LIVE,
role: TRTC.TYPE.ROLE_ANCHOR,
sdkAppId,
userId,
userSig,
roomId
});
Audience Side
Enter the room as an audience
Set the role parameter to TRTC.TYPE.ROLE_AUDIENCE.
await trtc.enterRoom({
scene: TRTC.TYPE.SCENE_LIVE,
role: TRTC.TYPE.ROLE_AUDIENCE,
sdkAppId,
userId,
userSig,
roomId
});
Play Remote Audio
By default, the SDK will automatically play remote audio, and you do not need to call any API to play remote audio.
Autoplay Policy Restriction
If you do not want the SDK to automatically play audio, you can try this:
trtc.on(TRTC.EVENT.REMOTE_AUDIO_AVAILABLE, event => {
trtc.muteRemoteAudio(event.userId, false);
trtc.muteRemoteAudio(event.userId, true);
})
await trtc.enterRoom({ ..., autoReceiveAudio: false });
Play Remote Video
trtc.on(TRTC.EVENT.REMOTE_VIDEO_AVAILABLE, ({ userId, streamType }) => {
const view = `${userId}_${streamType}`;
trtc.startRemoteVideo({ userId, streamType, view });
});
How to Switch Role
The audience role does not have the permission to publish stream, so if the audience wants to make a call with the anchor, they need to switch to the anchor role by using trtc.switchRole and then publish streams.
await trtc.switchRole(TRTC.TYPE.ROLE_ANCHOR);
await trtc.startLocalAudio();
await trtc.startLocalVideo();
await trtc.switchRole(TRTC.TYPE.ROLE_AUDIENCE);
Apakah halaman ini membantu?