미디어 장치

본 튜토리얼은 주로 다음의 내용을 소개합니다.

마이크 및 카메라 켜기/끄기

이 기능을 구현할 수 있는 3개 옵션이 있습니다. 저희는 첫 번째 옵션을 추천합니다.
장치를 끈 후 3개 옵션의 차이점
옵션
소프트웨어 또는 하드웨어
수집용 램프 켜기
음소거 패키지 계속 보내기
로컬 카메라 미리보기 가능
음소거 매개변수 사용
소프트웨어
Yes
Yes
No
stopLocalVideo() 및 stopLocalAudio() 사용
하드웨어
No
No
No
배포 매개변수 사용
하드웨어
Yes
No
Yes
원격 사용자의 경우 이 3개 계획은 동일하게 작동됩니다.
카메라를 끄면 방 안에 있는 기타 사용자들은 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 camera
await trtc.updateLocalVideo({ mute: false });
await trtc.startLocalAudio();
// mute the microphone
await trtc.updateLocalAudio({ mute: true });
// unmute the microphone
await trtc.updateLocalAudio({ mute: false });
설명:
또한, 마이크 또는 카메라가 음소거된 후 비트율(약 1kbps)이 아주 낮은 하나의 음소거 데이터 패킷을 송신하게 됩니다.
trtc.stopLocalVideo() & trtc.stopLocalAudio() 를 호출하여 카메라와 마이크를 끌 수 있습니다.
이 옵션에서 카메라와 마이크는 물리적으로 꺼지며, 장치의 물리적 수집용 램프가 꺼집니다.
// Turn off the camera
await 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 microphone
await trtc.stopLocalAudio();
// Turn on the microphone
await trtc.startLocalAudio();

배포 매개변수 사용

updateLocalVideo()updateLocalAudio()의 publish 매개변수를 사용하여 비디오 및 오디오 스트림을 배포할지 여부를 제어할 수 있습니다.
이 옵션에서, 소프트웨어 수준에서 오디오 및 비디오 스트림의 배포 상태를 제어할 수 있습니다. 실제로 장치는 여전히 활성 상태에 있으며, 장치의 물리적 수집용 램프는 여전히 켜져 있습니다.
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 camera
if (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 microphone
if (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이벤트

DEVICE_CHANGED 이벤트를 모니터링함으로써 장치 삽입, 장치 제거 및 장치 시작 동작을 감지할 수 있습니다.
trtc.on(TRTC.EVENT.DEVICE_CHANGED, (event) => {
// Device insertion
if (event.action === 'add') {
// Camera insertion
if (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 camera
trtc.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 failed
if (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 failed
if (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가 마이크 또는 카메라를 켰음을 의미합니다.