npm install trtc-sdk-v5 --save
yarn add trtc-sdk-v5
import TRTC from 'trtc-sdk-v5';
<script src="/your_path/trtc.js"></script>
const trtc = TRTC.create();
Parameter | Type | Description |
sdkAppId | number | |
userId | string | User ID specified by you. |
userSig | string | |
roomId | number | Room ID specified by you, usually a unique room ID. |
try {await trtc.enterRoom({ sdkAppId, userId, userSig, roomId: 8888 });console.log('enter room successfully');} catch (error) {console.error('failed to enter room ' + error);}
await trtc.startLocalAudio();
await trtc.stopLocalAudio();
// To preview the camera image, you need to place an HTMLElement in the DOM,// which can be a div tag, assuming its id is local-video.const view = 'local-video';await trtc.startLocalVideo({ view });
await trtc.stopLocalVideo();
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 = false
to turn off automatic audio playback.await trtc.enterRoom({ ..., autoReceiveAudio: false });
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 });});
await trtc.exitRoom();// After the exit is successful, you can call the trtc.destroy method to// destroy the instance and release related resources in a timely manner// if you do not need to use the trtc instance later.// The destroyed trtc instance cannot be used again and a new instance needs to be created.trtc.destroy();
trtc.on(TRTC.EVENT.KICKED_OUT, error => {console.error(`kicked out, reason:${error.reason}, message:${error.message}`);});
Was this page helpful?