メディアデバイス

このチュートリアルでは、方法を説明します。

マイクとカメラのオン/オフ

この機能を実現するには 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();

リモートユーザーのマイクとカメラのオン/オフ状態の検出

通常、リモートユーザーのマイクのオン/オフの表示方法を確認するために使用します。
2人のユーザー 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 がマイクまたはカメラをオフにしたことを示します。