Options | Software or Hardware | Will capturing lights on | Continue to send mute packets | Able to preview local camera |
Use the Mute Parameter | Software | Yes | Yes | No |
Use stopLocalVideo() & stopLocalAudio() | Hardware | No | No | No |
Use the Publish Parameter | Software | Yes | No | Yes |
await trtc.startLocalVideo();// mute the camera// After turning off the camera, the camera preview screen will become black,// and you can display the business side's own UI mask at this time.await trtc.updateLocalVideo({ mute: true });// unmute the cameraawait trtc.updateLocalVideo({ mute: false });
await trtc.startLocalAudio();// mute the microphoneawait trtc.updateLocalAudio({ mute: true });// unmute the microphoneawait trtc.updateLocalAudio({ mute: false });
// Turn off the cameraawait trtc.stopLocalVideo();// Turn on the camera// 'local-video' is the element id in the DOM used to play the local camera video container.await trtc.startLocalVideo({ view: 'local-video' });
// Turn off the microphoneawait trtc.stopLocalAudio();// Turn on the microphoneawait trtc.startLocalAudio();
await trtc.startLocalVideo();// unpublish the stream of camera.await trtc.updateLocalVideo({ publish: false });// publish the stream of camera.await trtc.updateLocalVideo({ publish: true });
await trtc.startLocalAudio(); // unpublish the stream of microphone await trtc.updateLocalAudio({ publish: false }); // publish the stream of microphone await trtc.updateLocalAudio({ publish: true });
// Open the camera, the default is the first camera in the camera list.await trtc.startLocalVideo();const cameraList = await TRTC.getCameraList();// Switch to the second cameraif (cameraList[1]) {await trtc.updateLocalVideo({ option: { cameraId: cameraList[1].deviceId }});}// On mobile device.// switch to front camera.await trtc.updateLocalVideo({ option: { useFrontCamera: true }});// switch to rear camera.await trtc.updateLocalVideo({ option: { useFrontCamera: false }});
// Open the microphone, the default is the first microphone in the microphone list.await trtc.startLocalAudio();const microphoneList = await TRTC.getMicrophoneList();// Switch to the second microphoneif (microphoneList[1]) {await trtc.updateLocalAudio({ option: { microphoneId: microphoneList[1].deviceId }});}
// The default speaker is the first speaker in the speaker list.const speakerList = await TRTC.getSpeakerList();if (speakerList[1]) {await TRTC.setCurrentSpeaker(speakerList[1].deviceId);}
trtc.on(TRTC.EVENT.DEVICE_CHANGED, (event) => {// Device insertionif (event.action === 'add') {// Camera insertionif (event.type === 'camera') {}} else if (event.action === 'remove') {// Device unplugged} else if (event.action === 'active') {// Device startup}console.log(`${event.type}(${event.device.label}) ${event.action}`);});
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) {// The camera in used is unplugged.}} else if (event.type === 'microphone') {const currentMicId = trtc.getAudioTrack()?.getSettings()?.deviceId;if (event.device.deviceId === currentMicId) {// The microphone in used is unplugged.}}}});
trtc.on(TRTC.EVENT.DEVICE_CHANGED, (event) => {if (event.action === 'add') {if (event.type === 'camera') {// After inserting the camera, switch to the new cameratrtc.updateLocalVideo({ option: { cameraId: event.device.deviceId }});}}});
trtc.on(TRTC.EVENT.VIDEO_PLAY_STATE_CHANGED, event => {// Local camera collection exception, at this time the SDK will try to automatically recover the collection, you can guide the user to check whether the camera is normal.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 => {// Local microphone collection exception, at this time the SDK will try to automatically recover the collection, you can guide the user to check whether the microphone is normal.if (event.userId === '' && (event.reason === 'mute' || event.reason === 'ended')) {}});
trtc.on(TRTC.EVENT.ERROR, error => {if (error.code === TRTC.ERROR_CODE.DEVICE_ERROR) {// Camera recapture failedif (error.extraCode === 5308) {// After guiding the user to check the device, call updateLocalVideo and pass in cameraId to recapture.trtc.updateLocalVideo({ option: { cameraId: '' }});}// Microphone recapture failedif (error.extraCode === 5309) {// After guiding the user to check the device, call updateLocalAudio and pass in microphoneId to recapture.trtc.updateLocalAudio({ option: { microphoneId: '' }});}}})
const trtc = TRTC.create();await trtc.startLocalAudio();let isAudioMuted = false;await trtc.updateLocalAudio({ mute: true });isAudioMuted = true;// create a new trtc instance for detecting the volume of microphone.const trtcA = TRTC.create();trtcA.enableAudioVolumeEvaluation();trtcA.on(TRTC.EVENT.AUDIO_VOLUME, event => {event.result.forEach(item => {// 1. userId === '' is local volume.// 2. It is generally believed that when the volume is greater than 10, the user is talking, and you can also customize this threshold.if (item.userId === '' && item.volume > 10 && isAudioMuted) {// The user is speaking after microphone muted.}})})await trtcA.startLocalAudio();
Was this page helpful?