3D Spatial Audio brings theater-like effects to Video Calling, making it seem as if the sound originates from all around the user. Video SDK provides spatial audio effects that give users an immersive audio experience in scenarios such as e-sports competitions, and online conferences.
You can configure the following spatial audio effects:
Spatial audio effects for users:
By setting the local and remote user's spatial positions, you can create an environment that enables users to experience changes in the distance, position, and orientation of other users in real-time. You can also enable your users to experience multiple audio effects such as audio blurring and air absorption.
Spatial audio effects for media player:
By updating the spatial position of the media player, you can add a sense of space to media resources such as background sounds and musical accompaniment. Agora provides local cartesian coordinate system calculation solution for the media player. This solution calculates the relative positions of the local user and the media player through the Video SDK. You update the spatial coordinates of the local user and the media player, respectively, so that the local user can hear the spatial audio effect of the media player.
The following figure shows the workflow you need to integrate spatial audio into your app.
In order to follow this procedure you must have:
Implemented the SDK quickstart project for Video Calling.
Android Studio 4.1 or higher.
Android SDK API Level 24 or higher.
A mobile device that runs Android 4.1 or higher.
A computer with Internet access.
Ensure that no firewall is blocking your network communication.
To ensure a true spatial experience, Agora recommends using an audio device that supports true binaural playback.
To create the environment necessary to implement spatial audio into your app, do the following:
This section shows how to use the Video SDK to implement spatial audio into your app, step-by-step.
In a real-word application, you report your local spatial position to a server in your environment and retrieve positions of remote users in the channel from your server. In this simple example, you use a single Button
to set the spatial position of a remote user.
To add the button to the UI, in /app/res/layout/activity_main.xml
, add the following code before </RelativeLayout>
:
<Button
android:id="@+id/SpatialAudioButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/JoinButton"
android:layout_alignEnd="@id/LeaveButton"
android:layout_alignStart="@id/JoinButton"
android:onClick="updateSpatialAudioPosition"
android:text="Update remote position" />
Add the required libraries
To set up and configure an instance of the spatial audio engine, import the corresponding Agora libraries. In /app/java/com.example.<projectname>/MainActivity
, add the following to the list of import
statements:
import io.agora.spatialaudio.ILocalSpatialAudioEngine;
import io.agora.spatialaudio.LocalSpatialAudioConfig;
import io.agora.spatialaudio.RemoteVoicePositionInfo;
To implement these features in your app, take the following steps:
Declare the variables you need
You create an instance of ILocalSpatialAudioEngine
to configure spatial audio and set self and remote user positions. In /app/java/com.example.<projectname>/MainActivity
, add the following declarations to the MainActivity
class:
private ILocalSpatialAudioEngine localSpatial;
private int remoteUid = 0; // Stores the uid of the remote user
Instantiate and configure the spatial audio engine
To create an instance of ILocalSpatialAudioEngine
at startup, take the following steps:
When a user launches the app, you create an instance of ILocalSpatialAudioEngine
, configure it and update the user's self position. To do this, add the following method to the MainActivity
class:
private void configureSpatialAudioEngine() {
// Enable spatial audio
agoraEngine.enableSpatialAudio(true);
// Create and initialize the spatial audio engine
LocalSpatialAudioConfig localSpatialAudioConfig = new LocalSpatialAudioConfig();
localSpatialAudioConfig.mRtcEngine = agoraEngine;
localSpatial = ILocalSpatialAudioEngine.create();
localSpatial.initialize(localSpatialAudioConfig);
// Set the audio reception range of the local user in meters
localSpatial.setAudioRecvRange(50);
// Set the length of unit distance in meters
localSpatial.setDistanceUnit(1);
// Define the position of the local user
float[] pos = new float[]{0.0F, 0.0F, 0.0F};
float[] forward = new float[]{1.0F, 0.0F, 0.0F};
float[] right = new float[]{0.0F, 1.0F, 0.0F};
float[] up = new float[]{0.0F, 0.0F, 1.0F};
// Set the position of the local user
localSpatial.updateSelfPosition(pos, forward, right, up);
}
To execute this method at startup, add the following line after setupVideoSDKEngine();
to the onCreate
method:
configureSpatialAudioEngine();
Set the spatial position of a remote user
To update the spatial position of a remote user:
Define the RemoteVoicePositionInfo
and call updateRemotePosition
.
public void updateSpatialAudioPosition(View view){
// Define the remote user's spatial position
RemoteVoicePositionInfo positionInfo = new RemoteVoicePositionInfo();
// The three values represent the front, right, and top coordinates
positionInfo.position = new float[]{10.0F, 0.0F, 0.0F};
positionInfo.forward = new float[]{10.0F, 0.0F, 0.0F};
// Update the spatial position of the specified remote user
localSpatial.updateRemotePosition(remoteUid, positionInfo);
showMessage("Remote user spatial position updated.");
}
To update the spatial position of a specific remote user, you need the uid
of that user. Add the following line to the beginning of the onUserJoined
callback, to store the remoteUid
:
remoteUid = uid;
Spatial audio functionality is currently available as beta.
To ensure that you have implemented spatial audio features into your app:
Generate a temporary token in Agora Console .
In your browser, navigate to the Agora web demo and update App ID, Channel, and Token with the values for your temporary token, then click Join.
In Android Studio, open app/java/com.example.<projectname>/MainActivity
and update appId
, channelName
and token
with the values from Agora Console.
Connect an Android device to your development device.
In Android Studio, click Run app. A moment later, you see the project installed on your device.
If this is the first time you run your app, grant camera and microphone permissions.
Press Join to connect to the same channel as your web demo.
Test spatial audio effects for users
Test spatial audio effects for media player
To setup spatial audio position of your media player, add Media Playing to your app.
Replace the call to localSpatial.updateRemotePosition
in updateSpatialAudioPosition
with the following:
//To configure special audio for media player use the following statement
localSpatial.updatePlayerPositionInfo(mediaPlayer.getMediaPlayerId(), positionInfo);
Press Update remote position
Your app updates the spatial position of the media player in the spatial audio engine. Listen to the audio of the media player. You feel that the location of the media player has shifted.
This completes the information in this page, or points you to documentation that explains other aspects about this product.