How to Play Audio Using the Agora SDK in Android
The Agora SDK has a play audio feature. You can use this feature to play songs, voice recordings, and sound effects for entertainment or educational purposes during a video call.
In this tutorial, you will learn how to play audio files during a video call using the Agora SDK in Android.
Prerequisites and requirements
- An Agora developer account (see How to Get Started with Agora).
- Knowledge of how to create a live-streaming Android application using Agora. Check out the tutorial here.
- Basic knowledge of Android development.
- Android Studio.
- An Android device.
Adding dependencies in Gradle
Add the following dependencies in your build.gradle file in the app module, and sync to download the Agora third-party library. Ensure that you always use the latest Agora library version:
Adding permissions in the Manifest.xml file
Add the following permissions in the Manifest.xml file:
Creating an instance of the RtcEngine
Now, let’s create an RtcEngine instance by initializing the RtcEngine and passing the IRtcEngineEventHandler and your App ID to the create method. IRtcEngineEventHandler is an abstract class that provides the default implementation:
Loading the audio file before the user plays it
To make the audio file processing fast, you have to load the audio file in advance. The preloadEffect() function takes in the audio ID and the audio file path. This function will load the selected audio file in advance. This function should be called before the user joins the video call:
The playEffect() function plays the audio file passed in the arguments. The playEffect() function takes in the following parameters:
- soundId: The sound ID of the audio effect file to be played.
- filePath: The file path of the audio effect file.
- loopCount: The number of playback loops.
- Pitch: The pitch of the audio effect.
- pan: Sets the spatial position of the effect.
- volume: Sets the volume percentage.
Next, call the playEffect() function to play the audio file in advance:
Agora Android SDK supports only the following audio file types:
Adjusting the volume
You can set the volume percentage of the video call audio by calling the adjustPlaybackSignalVolume() function and passing the volume value/percentage. By default, the video call volume is set to 100. Use this function to adjust the volume of the audio file being played:
The following code fetches the audio file path and plays the audio when the user presses the play button:
Use the stopAudioMixing() function to stop the audio when the stop button is pressed.
The resumeAudioMixing() function resumes the audio being played.
Integrating the play audio feature with the Agora Video Call SDK
You now know how to play audio files using the Agora SDK methods. The following code block shows you how to integrate the Agora play audio feature in a video streaming application:
Testing the app demo
In this tutorial, you have learned how to use the Agora SDK to:
- Preload audio
- Play and pause audio
- Adjust the audio volume
Hooray, you now know how to play audio files during a video call using the Agora SDK!
Thank you for reading. You can learn more on how to play audio using Agora SDK here, and you can check out more Agora features on GitHub here. If you want to copy or reference the SDK I was using, check it on GitHub here.
This article was originally posted on the Agora blog here: