音量

このチュートリアルでは、音量を検出する方法を説明します。
ローカルマイクとリモートユーザーの音量を検出します。
マイクがミュートされた後、ユーザーが話しているかどうかを検出します。
ローカルマイクとリモートの音量を調整します。

オーディオの音量を検出

TRTC.EVENT.AUDIO_VOLUME イベントをリスニングし、次に trtc.enableAudioVolumeEvaluation() を呼び出して音量コールバックイベントを有効にします。
trtc.on(TRTC.EVENT.AUDIO_VOLUME, event => {
event.result.forEach(({ userId, volume }) => {
// When userId is an empty string, it represents the volume of the local microphone.
const isMe = userId === '';
if (isMe) {
console.log(`my volume: ${volume}`);
} else {
console.log(`user: ${userId} volume: ${volume}`);
}
})
});
// Enable volume event and trigger the event every 500ms
trtc.enableAudioVolumeEvaluation(500);
// For performance reasons, when the page switches to the background,
// the SDK will not throw volume callback events. If you need to receive volume event
// when the page is switched to the background, you can set the second parameter to true.
trtc.enableAudioVolumeEvaluation(500, true);
// To turn off the volume event, pass in an value less than or equal to 0
trtc.enableAudioVolumeEvaluation(-1);

マイクがミュートされた後、ユーザーが話しているかどうかを検出します

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();

オーディオの音量調整

ローカルマイクの採集音量はデフォルトで 100 です。 trtc.updateLocalAudio() の captureVolume パラメータで調整できます。
// Increase the capture volume by setting captureVolume upper than 100.
await trtc.updateLocalAudio({ option: { captureVolume: 200 }});
// Decrease the capture volume by setting captureVolume below 100.
await trtc.updateLocalAudio({ option: { captureVolume: 50 }});
await trtc.updateLocalAudio({ option: { captureVolume: 0 }});
デフォルトのリモートオーディオ音量は 100 です。 trtc.setRemoteAudioVolume() を呼び出して調整できます。
// Increase remote audio volume
await trtc.setRemoteAudioVolume('remote_user_id', 200);
// Decrease remote audio volume
await trtc.setRemoteAudioVolume('remote_user_id', 50);