Back to Blog

How to Play Audio Using the Agora SDK in Android

How to Play Audio Using the Agora SDK in Android

Boemo is a software developer who embraces innovative approaches. He likes diving deep into complex concepts in order to learn and write articles that can help the reader understand complex methodologies in a simple and fun way.


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:

  1. soundId: The sound ID of the audio effect file to be played.
  2. filePath: The file path of the audio effect file.
  3. loopCount: The number of playback loops.
  4. Pitch: The pitch of the audio effect.
  5. pan: Sets the spatial position of the effect.
  6. 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:

  • MP3
  • AAC
  • M4A
  • 3GP
  • WAV

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:

Playing audio

The following code fetches the audio file path and plays the audio when the user presses the play button:

Stop playing

Use the stopAudioMixing() function to stop the audio when the stop button is pressed.

Resume playing

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:

If you are not familiar with building a one-to-one video call app using Agora SDK, check this tutorial on GitHub written by Hermes. The above code uses the same concepts taught in that tutorial.

Testing the app demo

Summary

In this tutorial, you have learned how to use the Agora SDK to:

  • Preload audio
  • Play and pause audio
  • Adjust the audio volume

Conclusion

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.

Other Resources

If you are getting confused in the process, you can check out Agora’s documentation. You can also join Agora’s Slack channel here.

RTE Telehealth 2023
Join us for RTE Telehealth - a virtual webinar where we’ll explore how AI and AR/VR technologies are shaping the future of healthcare delivery.

Try Agora for Free

Sign up and start building! You don’t pay until you scale.
Try for Free
Get Started with Agora thumbnail