Range Audio
Function Overview
Concept Explanation
- Range: The range within which a listener receives audio.
- Orientation: Refers to the listener's position and facing direction in the game world coordinates. For details, refer to
5 Set the listener's current position. - Listener: A user in the room who receives audio.
- Speaker: A user in the room who sends audio.
Function Description
Since version 2.11.0, ZEGO Express SDK has added a game voice module, mainly including: range audio, 3D audio effects, and team voice.
| Function | Description |
|---|---|
Range Audio | Listeners in the room have a range limit for receiving audio. If a speaker is beyond this range from the listener, they cannot hear the sound. To ensure voice clarity, when more than 20 people nearby are speaking, only the sounds of the 20 speakers closest to the listener can be heard. If the maximum range for audio reception is set to R, and the distance between the speaker and the listener is r, then:
![]() The above diagram only shows the example when the range audio mode is "World". For more sound reachability under different mode combinations, please refer to Set Common Voice Mode. |
| 3D Audio Effects | Sound has 3D spatial perception and attenuates with distance. |
Common Voice Mode | Players can choose to join a team and freely switch between "World", "Team Only", and "Secret Team" voice modes in the room.
Note
|
Applicable Scenarios
The game voice feature is suitable for battle royale games and metaverse scenarios.
In battle royale games, team voice provides team formation functionality. Teams can be changed before and after the game starts. Developers don't need to worry about stream grouping and stream publishing/playing implementation, and can directly implement team voice functionality.
In battle royale games and metaverse scenarios, 3D audio effects capability is provided. When listening to speaker audio effects, there is a sense of direction and distance, making the scene feel more realistic.
Download Sample Source Code
Please refer to Download Sample Source Code to get the source code.
For related source code, please check the files in the "src/Examples/Others/RangeAudio" directory.
Prerequisites
Before implementing range audio, ensure that:
- You have created a project in the ZEGOCLOUD Console and applied for a valid AppID and Server address. For details, refer to Console - Project Information.
- You have integrated ZEGO Express SDK into your project and implemented basic audio/video stream publishing and playing functionality. For details, refer to Quick Start - Integration and Quick Start - Implementation.
And ensure the development environment meets the following requirements:
- Prepare a Windows or macOS computer connected to the internet.
- Chrome browser version 56 or above is recommended. For browser compatibility information, please refer to 7 Browser Compatibility.
Important Notes
When using the range audio feature, please pay attention to the following important notes to avoid integration issues.
If you are already using the real-time audio/video features of ZEGO Express SDK, please note the following:
- Since the range audio module is implemented based on the stream publishing and playing interfaces of ZegoExpressEngine, you don't need to focus on the concept of stream publishing and playing when using it. In range audio scenarios, the concept of publishing audio stream changes to "enable microphone", and the concept of playing audio stream changes to "enable speaker". It's recommended not to use the startPublishingStream and startPlayingStream interfaces for stream publishing and playing operations while integrating the range audio feature, to avoid effect conflicts.
- Related callbacks for stream publishing and playing in the range audio module (publisherStateUpdate and playerStateUpdate) will no longer take effect.
- After calling ZegoExpressEngine's on interface in the range audio module to register roomStreamUpdate and streamExtraInfoUpdate event callbacks, please do not call ZegoExpressEngine's off interface to unregister all event callbacks when integrating the range audio feature.
Usage Steps
The above diagram only shows the core steps and interfaces for implementing game voice functionality. Developers can refer to the detailed introduction in the following documentation to implement other related interfaces as needed.
1 Import SDK
- Run the
npm i zego-express-engine-webrtccommand to install dependencies.
npm downloaded package supports TypeScript language (recommended).
- Import the SDK in the "index.js" file.
import { ZegoExpressEngine } from 'zego-express-engine-webrtc'2 Create Engine
1. Create engine
Create a ZegoExpressEngine engine instance, pass the applied AppID to the appID parameter, and pass the access server address to the server parameter.
- "server" is the access server address. For how to obtain it, please refer to Console - Project Information.
- For SDK version 3.6.0 and above, server can be set to an empty string, null, undefined, or any string, but cannot be left empty.
Initialize the ZegoExpressEngine instance zg and the range audio feature instance rangeAudio.
const zg = new ZegoExpressEngine(appID, server);
const rangeAudio = zg.createRangeAudioInstance();2. Listen to range audio event callbacks
If you need to register callbacks, developers can implement certain methods in ZegoRangeAudioEvent as needed, and call the on interface through the range audio instance rangeAudio to set callbacks.
rangeAudio.on("microphoneStateUpdate", (state, errorCode, extendedData) => {
if (state === 0) {
// Microphone sound off
} else if(state === 1) {
// Enabling microphone
} else if(state === 2) {
// Microphone on and sending sound
}
})3 Support browsers that don't allow autoplay sound
Use the isAudioContextRunning interface to check if the browser supports autoplay sound. If isSupport is false, you can guide the user to click a DOM element on the interface to trigger a click event, and call the resumeAudioContext interface in the click event to start autoplay.
<button onclick="enableAutoPlay()">Click me</button>const isSupport = rangeAudio.isAudioContextRunning();
async function enableAutoPlay() {
// result indicates whether enable was successful
const result = await rangeAudio.resumeAudioContext();
}4 Login Room
1. Get Login Token
Logging into a room requires a Token for identity verification. For how to obtain it, please refer to User Permission Control. For quick debugging, you can use the console to generate a temporary Token.
2. Login Room
Call the loginRoom interface through the ZegoExpressEngine instance zg, pass in the room ID parameter roomID, token, and user parameter user, and pass in the config parameter as needed to login to the room.
- Before logging into the room, please register all callbacks that need to be monitored after logging into the room. After successfully logging into the room, you can receive relevant callbacks.
- The values of
roomID,userID, anduserNameparameters are all custom.userNameis not required. - Both
roomIDanduserIDmust be unique. It's recommended that developers setuserIDto a meaningful value and associate it with their own business account system.
// Login room, returns true if successful
const result = await zg.loginRoom(roomID, token, {userID, userName});5 Set the listener's current position
Call the updateSelfPosition interface to add your own orientation, or update your position and facing direction in the world coordinate system when your orientation changes.
- If you don't call this interface to set position information before calling enableSpeaker to enable the speaker, you won't be able to hear sounds from anyone other than your team.
- The coordinate values of the three axes of your own coordinate system can be obtained through the rotation angle conversion matrix of a third-party 3D engine.
| Parameter Name | Description |
| position | Your coordinates in the world coordinate system. The parameter is a number array of length 3, with the three values representing the coordinate values of forward, right, and up respectively. |
| axisForward | The unit vector of the forward axis of your own coordinate system. The parameter is a number array of length 3, with the three values representing the coordinate values of forward, right, and up respectively. |
| axisRight | The unit vector of the right axis of your own coordinate system. The parameter is a number array of length 3, with the three values representing the coordinate values of forward, right, and up respectively. |
| axisUp | The unit vector of the up axis of your own coordinate system. The parameter is a number array of length 3, with the three values representing the coordinate values of forward, right, and up respectively. |

const position = [0,0,0];
const axisForward = [1,0,0];
const axisRight = [0,1,0];
const axisUp = [0,0,1];
rangeAudio.updateSelfPosition(position, axisForward, axisRight, axisUp);6 Add or update speaker position information
After successfully logging into the room, call the updateAudioSource interface to update the speaker position.
- In World mode: Need to update the positions of the listener and all speakers in the room. If the speaker position is not set or the speaker is beyond the listener's range, there will be situations where sound cannot be heard.
- Here, speaker refers to other people in the room, and listener refers to yourself.
- userID: The ID of another speaking user in the room.
- position: The speaker's coordinates in the world coordinate system. The parameter is a number array of length 3, with the three values representing the coordinate values of forward, right, and up respectively.
const userID = "other";
const position = [1,0,0];
rangeAudio.updateAudioSource(userID, position);7 (Optional) Set audio receive range
Call the setAudioReceiveRange interface to set the maximum range for the listener to receive audio, that is, starting from yourself, a 3D space with the set distance as the volume in 3D space. After setting this range, when 3D audio effects are enabled, sound will attenuate with increasing distance until it exceeds the set range, after which there will be no more sound. Team voice will not be restricted by this value and will not have 3D audio effects.
If not set, it means there is no distance limit for receiving audio, and you can hear the voices of all people in the room.
rangeAudio.setAudioReceiveRange(100);8 (Optional) Implement 3D audio effects
Call the enableSpatializer interface to set 3D audio effects. When enable is true, it means 3D audio effects are enabled. At this time, the audio of non-team members in the room will produce spatial perception changes as the speaker's distance and direction from yourself change. When it's false, it means 3D audio effects are disabled. (Can be enabled or disabled at any time)
This feature only affects people outside the team.
// enable is true to enable, false to disable. Default is disabled.
rangeAudio.enableSpatializer(true);9 Enable microphone and speaker
After successfully logging into the room:
-
Call the enableMicrophone interface to set whether to enable the microphone. When
enableistrue, it means enabled; when it'sfalse, it means disabled. (Can be enabled or disabled at any time)Developers can listen to the microphoneStateUpdate event callback to get the updated microphone status.
-
Call the enableSpeaker interface to set whether to enable the speaker. When
enableistrue, it means starting to play stream and play audio; when it's "false", it means stopping playing stream and playing audio. (Can be enabled or disabled at any time)
After calling the enableSpeaker interface, when the maximum playing stream limit is exceeded (currently 20 streams), it will prioritize playing team member audio streams (team mode needs to be set), then play audio streams closest to itself in the world range.
rangeAudio.on("microphoneStateUpdate", (state, errorCode, extendedData) => {
if (state === 0) {
// Microphone sound off
} else if(state === 1) {
// Enabling microphone
} else if(state === 2) {
// Microphone on and sending sound
}
})
// Enable microphone, enable is true to enable, false to disable.
rangeAudio.enableMicrophone(enable);
// Enable speaker, enable is true to enable, false to disable.
rangeAudio.enableSpeaker(enable);10 (Optional) Join team, set voice mode
Join Team
Call the setTeamID interface to set the team ID you want to join as needed (ID can be changed at any time). After setting the ID, you can directly join. After joining a team, communication with team members in the same team is not limited by range audio and 3D audio effects.
// Parameter teamID passed as a string means joining the team of the corresponding teamID.
rangeAudio.setTeamID("teamID");
// Parameter teamID not passed or passed as undefined means leaving the team.
rangeAudio.setTeamID();Set Common Voice Mode
Call the setRangeAudioMode interface to set the range audio mode. When the mode parameter is 0, it means you can hear everyone's voice; when it's 1, it means you can only hear voices of other members in the same team.
| Voice Mode | Parameter Value | Function Description |
|---|---|---|
| World | 0 | After setting this mode, this user can talk with team members and talk with other people in World mode within range. |
| Team Only | 1 | After setting this mode, this user can only talk with team members. |
// Parameter mode can be 0 or 1.
// 0 is World mode, can communicate with everyone logged into the room.
// 1 is Team Only mode, only communicate with team members.
rangeAudio.setRangeAudioMode(1);Under different range audio modes, the receivability of speaker voices varies.
- Assuming user A's mode is "World", the receivability of user B's voice under different range audio modes is as follows:
| Same Team | Within Max Range | Range Audio Mode | Can A hear B's voice | Can B hear A's voice |
|---|---|---|---|---|
| Yes | Yes | World | Yes | Yes |
| Team Only | Yes | Yes | ||
| No | World | Yes | Yes | |
| Team Only | Yes | Yes | ||
| No | Yes | World | Yes | Yes |
| Team Only | No | No | ||
| No | World | No | No | |
| Team Only | No | No |
- Assuming user A's mode is "Team Only", the receivability of user B's voice under different range audio modes is as follows:
| Same Team | Within Max Range | Range Audio Mode | Can A hear B's voice | Can B hear A's voice |
|---|---|---|---|---|
| Yes | Yes | World | Yes | Yes |
| Team Only | Yes | Yes | ||
| No | World | Yes | Yes | |
| Team Only | Yes | Yes | ||
| No | Yes | World | No | No |
| Team Only | No | No | ||
| No | World | No | No | |
| Team Only | No | No |
Set Custom Voice Mode
- Custom voice mode and common voice mode cannot be used at the same time.
- If you need to use custom voice mode, please ensure all users in game voice have an online SDK version of 2.24.0 or above.
With custom voice mode, you can freely control audio send/receive logic to complete various audio interactions. For example: Assuming A, B, C are members of the same team, and C is within A's reception range, you can achieve the expected audio experience through the configuration in the table.
| User | Speaking Expectation | Listening Expectation | Speaking Mode Configuration | Listening Mode Configuration | Remarks |
|---|---|---|---|---|---|
| A | Send audio to team members and other users within range | Listen to audio from team members and other users within range | All | All | A can communicate with both B and C |
| B | Only send audio to team members | Only listen to audio from team members | Team | Team | B can only communicate with A |
| C | Only send audio to users within range | Only listen to audio from users within range | World | World | C can only communicate with A |
| Voice Mode | Parameter Value | Function Description | |
|---|---|---|---|
| Speaking Mode | All Mode | 0 | Speak to All mode, everyone in the room can hear the speaker's voice. |
| World Mode | 1 | Speak to World mode, only people within range can hear the speaker's voice. | |
| Team Mode | 2 | Speak to Team mode, only team members can hear the speaker's voice (not limited by range). | |
| Listening Mode | All Mode | 0 | Listen to All mode, can hear voices of everyone in the room. |
| World Mode | 1 | Listen to World only mode, only hear voices of people within range. | |
| Team Mode | 2 | Listen to Team only mode, only hear voices of members in the same team (not limited by range). | |
zg.setRangeAudioCustomMode(0, 0);11 Leave Room
Call the logoutRoom interface to leave the room. After leaving, the microphone and speaker will be automatically disabled (i.e., unable to send your own audio and unable to hear other people's audio), and the speaker information list will be cleared.
zg.logoutRoom(roomID)FAQ
- How many streams can be played simultaneously within the listening range?
Range audio supports playing up to 20 streams simultaneously.
To ensure voice clarity, when more than 20 people nearby are speaking, only the sounds of the 20 speakers closest to you can be heard. If there are more than 20 people at the same distance, it's determined by the order in which each userID is first passed when calling the updateAudioSource interface.
- Does "range" in range audio refer to listening range or speaking range?
The "range" in range audio refers to listening range.
- Do team members have 3D audio effects when speaking?
Voice within a team has the effect of ordinary calling, without 3D audio effects for now.
- If I'm already calling the publishing stream interface, will there be conflicts when using range audio?
Range audio currently uses the main channel to send audio. If you are already using the main channel, there will be conflicts.
Browser Compatibility
- For browser compatibility of range audio, please refer to Which browsers does ZEGO Express Web SDK support.
- Browser compatibility for 3D audio effects is as follows:
| Browser | Compatibility |
|---|---|
| Chrome | 14 and above |
| Edge | 79 and above |
| Firefox | 53 and above |
| Opera | 15 and above |
| IE | Not supported |
| Safari | 14.1 and above |
| WebView Android | 55 and above |
| Firefox for Android | 53 and above |
| Opera Android | 42 and above |
| Safari on iOS | 14.5 and above |
| Samsung Internet | 6.0 and above |

