Headphone Monitoring and Channel Settings
Feature Overview
ZEGO Express SDK provides headphone monitoring and dual-channel functionality, which developers can set as needed.
- Headphone monitoring refers to headphone Capture monitoring. After inserting headphones (ordinary headphones or Bluetooth headphones) into the device, you can hear the sound Captured by the device's microphone from the local headphone side.
- Dual channel refers to two audio channels. When hearing sound, you can judge the specific position of the sound source based on the phase difference of the sound between the left and right ears. ZEGO Express SDK defaults to mono audio Capture. When developers have high audio quality requirements, they can enable dual-channel Capture functionality. Dedicated dual-channel Capture devices can Capture dual-channel audio data and publish it.
Example Source Code Download
Please refer to Download Example Source Code to get the source code.
For related source code, please check files in the "src/Examples/AdvancedAudioProcessing/EarReturnAndChannelSettings" directory.
Prerequisites
Before setting headphone monitoring and channel functionality, please ensure:
- A project has been created in ZEGOCLOUD Console, and valid AppID and Server address have been obtained. For details, please refer to Console - Project Information.
- ZEGO Express SDK has been integrated into the project, and basic audio/video streaming functionality has been implemented. For details, please refer to Quick Start - Integration and Quick Start - Implementation.
Usage Steps
Preview Local Media Stream
Call the createZegoStream interface to create a ZegoLocalStream instance object localStream for previewing the local media stream.
const localStream = await zg.createZegoStream();
// Preview the stream before or during publishing, and mount the playback component to the page component container DOM element.
localStream.playVideo(document.querySelector("#local-video"));Set Headphone Monitoring
Enable Headphone Monitoring
After enabling preview or starting publishing, you can call localStream's playAudio interface to start playing audio, i.e., enable headphone monitoring functionality. After the speaker speaks, they will hear their own voice.
After enabling headphone monitoring, you need to use it with headphones plugged in. Otherwise, the sound from external speakers will also be Captured, causing echo issues.
// localStream is the ZegoLocalStream instance object created by createZegoStream
localStream.playAudio();Set Headphone Monitoring Volume
After enabling preview or starting publishing, you can call localStream's setVolume interface to adjust the headphone monitoring volume.
Where volume is the playback volume size, value range [0, 100], default is 100.
// volume value range [0,100]
localStream.setVolume(volume);Set Channels
Create Stream and Set Dual Channel
Call the createZegoStream interface to create a stream, and set the number of channels through the channelCount parameter of audio under the camera object. channelCount values:
- 1: Indicates mono
- 2: Indicates dual channel
const localStream = await zg.createZegoStream({camera: {
video: true,
audio: {
// Set to 2 represents dual channel
channelCount: 2
}
}})
// Publish stream
zg.startPublishingStream(streamID, localStream);