媒体设备
本教程主要介绍如何。
1. 打开/关闭麦克风和摄像头。
2. 切换麦克风、摄像头和扬声器。
3. 检测媒体设备的状态。
4. 检测用户将麦克风静音后是否正在说话。
5. 检测远程用户的麦克风和摄像头开/关状态。
打开/关闭麦克风和摄像头
选项 | 软件或硬件 | 将采集灯打开 | 继续发送静音包 | 能够预览本地摄像头 |
使用静音参数 | 软件 | Yes | Yes | No |
使用 stopLocalVideo() 和 stopLocalAudio() | 硬件 | No | No | No |
使用发布参数 | 硬件 | Yes | No | Yes |
对于远程用户,这三个计划的行为相同:
在关闭摄像头后,房间内其他用户会收到 REMOTE_VIDEO_UNAVAILABLE 事件。
在开启摄像头后,房间内其他用户会收到 REMOTE_VIDEO_AVAILABLE 事件。
在关闭麦克风后,房间内其他用户会收到 REMOTE_AUDIO_UNAVAILABLE 事件。
在开启麦克风后,房间内其他用户会收到 REMOTE_AUDIO_AVAILABLE 事件。
使用静音参数
打开摄像头和麦克风后,可以使用 trtc.updateLocalVideo() 和 trtc.updateLocalAudio() 的 mute 参数来控制摄像头和麦克风的开关状态。在此选项中,摄像头和麦克风在软件级别静音。事实上,设备仍然处于激活状态,设备的物理捕获灯仍然亮着。
物理上,由于麦克风启动需要一些时间,因此在此期间可能会错过用户的声音。因此,我们建议您使用此选项。此选项的优点是重新打开摄像头或麦克风会更快:
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 });
说明:
另外,麦克风或摄像头静音后,会发送一个非常低的比特率(约1kbps)静音数据包。
该选项下,摄像头和麦克风物理关闭,设备的物理采集灯将关闭。
// 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);}
检测媒体设备的状态
DEVICE_CHANGED事件
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 }});}}});
自动夺回
SDK 将检测摄像头和麦克风的插入和拔出行为,并在适当的时间自动重新捕获媒体设备。具体逻辑如下:
1. 如果当前流媒体摄像头/麦克风被拔出,SDK 将尝试重新捕获可用的媒体设备。
2. 如果摄像头/麦克风被拔出并且没有可用的媒体设备,SDK 将检测设备插入行为。如果插入新的可用媒体设备,SDK 将尝试重新捕获新插入的媒体设备。
3. 如果重新捕获失败,会抛出 TRTC.ERROR_CODE.DEVICE_ERROR 错误,extraCode 为5308和5309。
监听设备捕获异常事件:
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')) {}});
监听 SDK 重新捕获失败事件:
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();
检测远程用户的麦克风和摄像头开/关状态
这通常用于确认如何显示远程用户的麦克风开/关状态。
假设有两个用户 A 和 B。
1. A 成功进入房间后,如果房间内还有另一个锚点 B,A 会收到 REMOTE_USER_ENTER 事件。
2. 此时,A 可以假设 B 默认没有打开麦克风或摄像头。
3. 如果 A 收到 B 发来的 REMOTE_AUDIO_AVAILABLE 或 REMOTE_VIDEO_AVAILABLE 事件,则表示 B 已打开麦克风或摄像头。
4. 如果 A 收到来自 B 的 REMOTE_AUDIO_UNAVAILABLE 或 REMOTE_VIDEO_UNAVAILABLE 事件,则表示 B 已关闭麦克风或摄像头。