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:

  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.

This article was originally posted on the Agora blog here:

How to Play Audio Using the Agora SDK in Android

--

--

--

boemommopelwa@gmail.com|Android developer| Tech Enthusiast|Innovative thinking is my passion | Learn and experience more to discover more

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Apple vs Android

Storing Android Build Config Fields in a Properties File

Kotlin Flow in Android — Process asynchronous stream

Speed up your Android Dynamic Features flow testing

Google IMA ads in Flutter

Android RecyclerView Part 2 — Creating RecyclerView with multiple view-types

How to Mute Audio and Adjust Volume During a Video Call in Android Using the Agora SDK

Future Builder: Flutter

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Boemo Mmopelwa

Boemo Mmopelwa

boemommopelwa@gmail.com|Android developer| Tech Enthusiast|Innovative thinking is my passion | Learn and experience more to discover more

More from Medium

Android Background Service without Hiccup

Requestly integration with android. A useful debugger

Themes, Styles, and Attributes

Find the introduction Sliders and Huawei Account Kit Integration in Money Management Android app…