ライブブロードキャスト
TRTC シーンとロール
一般的なビジネスシーンでは、すべてのユーザーがストリームをリリースする必要はなく、一部のユーザーはストリームを購読するだけでよいです。
TRTC には2種類のシーンがあります。
rtc シーン:デフォルトシーン。rtc シーンのすべてのユーザーはホストであり、このシーンには視聴者のロールは存在しません。
ホスト はプッシュできます。1 ルームは最大 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);})// Setting autoReceiveAudio = false to 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);