This tutorial mainly introduces how to implement a basic audio and video call.
Install
npm install trtc-sdk-v5 --save
yarn add trtc-sdk-v5
Download manually:
2. Copy trtc.js to your project.
3. Import trtc.js by script
<script src="trtc.js"></script>
API Overview
trtc instance, provides the core capability for real-time audio and video calls. API Lifecycle
Usage
Create a TRTC Instance
import TRTC from 'trtc-sdk-v5';
const trtc = TRTC.create();
Enter the Room
Call the trtc.enterRoom() method to enter the room. Usually called in the click callback of the Start Call button.
Key parameters:
scene: Set to 'rtc'. Another kind of scene is live, which can be referred to Live Streaming. sdkAppId: The sdkAppId of the audio and video application you created in TRTC Console. userId: User ID specified by you.
userSig: User signature, refer to UserSig. roomId: Room ID specified by you, usually a unique room ID.
For more detailed parameter descriptions, refer to the interface document trtc.enterRoom(). try {
await trtc.enterRoom({ roomId: 8888, scene:'rtc', sdkAppId, userId, userSig });
console.log('Entered the room successfully');
} catch (error) {
console.error('Failed to enter the room ' + error);
}
Turn on/off Microphone
await trtc.startLocalAudio();
await trtc.stopLocalAudio();
Turn on/off Camera
const view = 'local-video';
await trtc.startLocalVideo({ view });
await trtc.stopLocalVideo();
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 });
});
Exit the Room
await trtc.exitRoom();
trtc.destroy();
Handling being kicked out
In addition to actively exiting the room, users may also be kicked out of the room for the following reasons.
1. kick: Two users with the same userId enter the same room, and the user who entered the room first will be kicked out. It is not allowed, which may cause abnormal audio and video calls between the two parties, so you should avoid this happening.
2. banned: A user is kicked out of a TRTC room through the server's RemoveUser | RemoveUserByStrRoomId interface. The user will receive a kicked event, and the reason is banned. 3. room-disband: A TRTC room is dissolved through the server's DismissRoom | DismissRoomByStrRoomId interface. After the room is dissolved, all users in the room will receive a kicked event, and the reason is room-disband. At this time, the SDK will throw the KICKED_OUT event. trtc.on(TRTC.EVENT.KICKED_OUT, error => {
console.error(`kicked out, reason:${error.reason}, message:${error.message}`);
});
Contact Us
If you encounter any problems during the implementation process, please feel free to create an issue on GitHub issue, and we will deal with it as soon as possible.
Was this page helpful?