logo
Video Call
On this page

Range Audio

2024-09-27

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.

FunctionDescription

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:

  • When r < R, it means the speaker is within the normal range, and the listener can hear the sound.
  • When r ≥ R, it means the speaker is beyond the maximum range, and the listener cannot hear the sound.

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 EffectsSound 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.

  • World: Players can talk with other players in the world range, and can also talk with teammates.
  • Team Only: Players can only talk with teammates.
  • Secret Team: Players can talk with teammates and can only hear voices from other players in the world range.
Note
  • Communication between teammates is not affected by "range" and "3D audio effects". For details, please refer to Set Common Voice Mode.
  • If you want to customize voice send/receive capabilities, please refer to Set Custom Voice Mode.

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:

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

Warning

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

  1. Run the npm i zego-express-engine-webrtc command to install dependencies.
Note

npm downloaded package supports TypeScript language (recommended).

  1. 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.

Note
  • "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.

Warning
  • 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, and userName parameters are all custom. userName is not required.
  • Both roomID and userID must be unique. It's recommended that developers set userID to 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.

Note
  • 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 NameDescription
positionYour 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.
axisForwardThe 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.
axisRightThe 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.
axisUpThe 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.

Warning
  • 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.

Note

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)

Warning

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 enable is true, it means enabled; when it's false, 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 enable is true, 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)

Warning

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 ModeParameter ValueFunction Description
World0After setting this mode, this user can talk with team members and talk with other people in World mode within range.
Team Only1After 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 TeamWithin Max RangeRange Audio ModeCan A hear B's voiceCan B hear A's voice
YesYesWorldYesYes
Team OnlyYesYes
NoWorldYesYes
Team OnlyYesYes
NoYesWorldYesYes
Team OnlyNoNo
NoWorldNoNo
Team OnlyNoNo
  • Assuming user A's mode is "Team Only", the receivability of user B's voice under different range audio modes is as follows:
Same TeamWithin Max RangeRange Audio ModeCan A hear B's voiceCan B hear A's voice
YesYesWorldYesYes
Team OnlyYesYes
NoWorldYesYes
Team OnlyYesYes
NoYesWorldNoNo
Team OnlyNoNo
NoWorldNoNo
Team OnlyNoNo

Set Custom Voice Mode

Warning
  • 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.

UserSpeaking ExpectationListening ExpectationSpeaking Mode ConfigurationListening Mode ConfigurationRemarks
 ASend audio to team members and other users within rangeListen to audio from team members and other users within rangeAllAllA can communicate with both B and C
BOnly send audio to team membersOnly listen to audio from team membersTeamTeamB can only communicate with A
COnly send audio to users within rangeOnly listen to audio from users within rangeWorldWorldC can only communicate with A
Voice ModeParameter ValueFunction Description
Speaking ModeAll Mode0Speak to All mode, everyone in the room can hear the speaker's voice.
World Mode1Speak to World mode, only people within range can hear the speaker's voice.
Team Mode2Speak to Team mode, only team members can hear the speaker's voice (not limited by range).
Listening ModeAll Mode0Listen to All mode, can hear voices of everyone in the room.
World Mode1Listen to World only mode, only hear voices of people within range.
Team Mode2Listen 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

  1. 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.

  1. Does "range" in range audio refer to listening range or speaking range?

The "range" in range audio refers to listening range.

  1. 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.

  1. 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

BrowserCompatibility
Chrome14 and above
Edge79 and above
Firefox53 and above
Opera15 and above
IENot supported
Safari14.1 and above
WebView Android55 and above
Firefox for Android53 and above
Opera Android42 and above
Safari on iOS14.5 and above
Samsung Internet6.0 and above

Previous

Audio and Video Track Replacement

Next

Room Connection Status