This tutorial mainly introduces how to
Demo
Turn on/off the Microphone and Camera
There are 3 options to implement this feature. We recommend the first option. Differences between three options after turning off device|
Software | Yes | Yes | No |
Hardware | No | No | No |
Software | Yes | No | Yes |
For remote users, the three plans behave the same:
Use the Mute Parameter
You can use the mute parameter of trtc.updateLocalVideo() and trtc.updateLocalAudio() to control the on/off state of camera and microphone after turning on the camera and microphone. In this option, the camera and microphone are muted at the software level. In fact, the device is still in active state, and the physical capturing light of the device is still on.
Physically, since it takes some time for the microphone to start, the user's voice may be missed during this period. Therefore, we recommend that you use this option. The advantage of this option is that it will be faster to turn the camera or microphone back on:
await trtc.startLocalVideo();
await trtc.updateLocalVideo({ mute: true });
await trtc.updateLocalVideo({ mute: false });
await trtc.startLocalAudio();
await trtc.updateLocalAudio({ mute: true });
await trtc.updateLocalAudio({ mute: false });
Note:
In addition, a very low bit rate(about 1kbps) muted packet will be sent after muting microphone or camera.
In this option, the camera and microphone are closed physically, and the physical capturing light of the device will be turned off.
await trtc.stopLocalVideo();
await trtc.startLocalVideo({ view: 'local-video' });
await trtc.stopLocalAudio();
await trtc.startLocalAudio();
Use the Publish Parameter
In this option, you control the publish state of audio and video stream at the software level. In fact, the device is still in active state, and the physical capturing light of the device is still on.
await trtc.startLocalVideo();
await trtc.updateLocalVideo({ publish: false });
await trtc.updateLocalVideo({ publish: true });
await trtc.startLocalAudio();
await trtc.updateLocalAudio({ publish: false });
await trtc.updateLocalAudio({ publish: true });
Switch the Microphone, Camera and Speaker
Switch Camera
await trtc.startLocalVideo();
const cameraList = await TRTC.getCameraList();
if (cameraList[1]) {
await trtc.updateLocalVideo({ option: { cameraId: cameraList[1].deviceId }});
}
await trtc.updateLocalVideo({ option: { useFrontCamera: true }});
await trtc.updateLocalVideo({ option: { useFrontCamera: false }});
Switch Microphone
await trtc.startLocalAudio();
const microphoneList = await TRTC.getMicrophoneList();
if (microphoneList[1]) {
await trtc.updateLocalAudio({ option: { microphoneId: microphoneList[1].deviceId }});
}
Switch Speaker
const speakerList = await TRTC.getSpeakerList();
if (speakerList[1]) {
await TRTC.setCurrentSpeaker(speakerList[1].deviceId);
}
Detect the State of Media Device
DEVICE_CHANGED Event
You can listen for DEVICE_CHANGED event to detect the behavior of device insertion, device unplugged and device startup. trtc.on(TRTC.EVENT.DEVICE_CHANGED, (event) => {
if (event.action === 'add') {
if (event.type === 'camera') {}
} else if (event.action === 'remove') {
} else if (event.action === 'active') {
}
console.log(`${event.type}(${event.device.label}) ${event.action}`);
});
Check If the Device in Used is Unplugged
trtc.on(TRTC.EVENT.DEVICE_CHANGED, (event) => {
if (event.action === 'remove') {
if (event.type === 'camera') {
const currentCameraId = trtc.getVideoTrack()?.getSettings()?.deviceId;
if (event.device.deviceId === currentCameraId) {
}
} else if (event.type === 'microphone') {
const currentMicId = trtc.getAudioTrack()?.getSettings()?.deviceId;
if (event.device.deviceId === currentMicId) {
}
}
}
});
Switch to New Device When User Inserts a New device.
trtc.on(TRTC.EVENT.DEVICE_CHANGED, (event) => {
if (event.action === 'add') {
if (event.type === 'camera') {
trtc.updateLocalVideo({ option: { cameraId: event.device.deviceId }});
}
}
});
Recapture Automatically
The SDK will detect the insertion and unplugged behavior of the camera and microphone, and automatically recapture media device at the appropriate time. The specific logic is as follows:
1. If the current streaming camera/microphone is unplugged, the SDK will try to recapture the available media device.
2. If the camera/microphone is unplugged and there is no available media device, the SDK will detect the device insertion behavior. If a new available media device is inserted, the SDK will try to recapture the new inserted media device.
Listen for device capture exception events:
trtc.on(TRTC.EVENT.VIDEO_PLAY_STATE_CHANGED, event => {
if (event.userId === '' && event.streamType === TRTC.TYPE.STREAM_TYPE_MAIN && (event.reason === 'mute' || event.reason === 'ended')) {}
});
trtc.on(TRTC.EVENT.AUDIO_PLAY_STATE_CHANGED, event => {
if (event.userId === '' && (event.reason === 'mute' || event.reason === 'ended')) {}
});
Listen for SDK recapture failed events:
trtc.on(TRTC.EVENT.ERROR, error => {
if (error.code === TRTC.ERROR_CODE.DEVICE_ERROR) {
if (error.extraCode === 5308) {
trtc.updateLocalVideo({ option: { cameraId: '' }});
}
if (error.extraCode === 5309) {
trtc.updateLocalAudio({ option: { microphoneId: '' }});
}
}
})
Detect Whether the User is Speaking after Muting Microphone
const trtc = TRTC.create();
await trtc.startLocalAudio();
let isAudioMuted = false;
await trtc.updateLocalAudio({ mute: true });
isAudioMuted = true;
const trtcA = TRTC.create();
trtcA.enableAudioVolumeEvaluation();
trtcA.on(TRTC.EVENT.AUDIO_VOLUME, event => {
event.result.forEach(item => {
if (item.userId === '' && item.volume > 10 && isAudioMuted) {
}
})
})
await trtcA.startLocalAudio();
Detect the Microphone and Camera on/off State of Remote User
This is often used to confirm how to display the microphone on/off stat of a remote user.
Suppose there are two users A and B.
1. After A enters the room successfully, if there is another anchor B in the room, A will receive REMOTE_USER_ENTER event. 2. At this point, A can assume that B has not turned on the microphone or camera by default.