Skip to main content

You are looking at Video Calling v3.x Docs. The newest version is  Video Calling 4.x

Play Audio Effect or Music File

Introduction

During real-time audio and video interaction, for better atmosphere or entertainment, users often need to play audio effects or background music and make all users in the channel hear them. For example, users might add fighting sounds when playing an action game or include an accompaniment when singing. Agora provides two independent groups of methods so that you can play audio effect files and a music file separately.

Sample project

Agora provides an open-source demo project on GitHub that implements playing audio effect files and a music file. You can view the source code on Github or download the project to try it out.

Audio effect files

In this context, the audio effects are ambient sounds with a short duration, such as applause, cheers, fighting sounds, and gunshots. Usually, you can play multiple audio effects at the same time.

Agora provides a group of methods to play and manage audio effect files. Supported functions include the following:

  • Play local or online audio effect files.
  • Set the spatial position, number of loops, playback position, volume, and other playback options of an audio effect.
  • Flexibly control the playing, pausing, resumption, and stopping of audio effect files, individually or globally.
Supported audio effect file formats include MP3, AAC, M4A, MP4, WAV, and 3GP. See Media Formats Supported by Android.

To play audio effect files, see the following steps:

  1. Before joining a channel, call the getAudioEffectManager method to get the IAudioEffectManager class.
  2. Call the preloadEffect method to preload the local audio effect file.
  3. After joining a channel, call the playEffect method to play the audio effect file. After completing playing an audio effect file, the SDK triggers the onAudioEffectFinished callback.
Call the audio effect methods in the IAudioEffectManager class.

Getting the audio manager class

The Agora SDK provides the IAudioEffectManager class to manage audio effect files. Before playing an audio effect file, you need to get the IAudioEffectManager class.


_6
// Imports the IAudioEffectManager class.
_6
import io.agora.rtc.IAudioEffectManager;
_6
_6
// Calls getAudioEffectManager to get the IAudioEffectManager class.
_6
private IAudioEffectManager audioEffectManager;
_6
audioEffectManager = engine.getAudioEffectManager();

Specifying an audio effect file

Before playing an audio effect file, you need to set filePath and soundId to specify the audio effect file. The representing of two parameters is as follows:

  • filePath: The audio effect file path, including the local and online file path. The SDK searches for audio effect files in this path.
  • soundId: The audio effect ID, which you define. This ID must be unique. The SDK identifies an audio effect file based on its audio effect ID. Common solutions for defining audio effect IDs include incrementing the ID and using hashCode for the audio effect file name.

Preloading

The SDK supports preloading audio effect files. To improve performance, you can add the audio effect files into the memory in advance. Preloading is not required. Agora recommends that you choose whether to preload audio effect files according to your needs.

  • If you need to play a specified audio effect repeatedly, Agora recommends preloading the audio effect file.
  • If an audio effect file size is large, Agora recommends not preloading the audio effect file.

To preload multiple audio effect files, you need to call preloadEffect multiple times.

  • The SDK only supports preloading local audio effect files.
  • Call preloadEffect before joining a channel.
  • After calling preloadEffect, the audio effect file occupies memory until you call unloadEffect or the user leaves the channel.

  • _7
    // Sets the audio effect ID.
    _7
    int id = 0;
    _7
    // Preloads the specified local audio effect file into the memory.
    _7
    audioEffectManager.preloadEffect(id++, "your file path");
    _7
    _7
    // Unloads the preloaded audio effect file.
    _7
    audioEffectManager.unloadEffect(id);

    Playing and stopping

    Call playEffect to play the audio effect file. You can call playEffect multiple times to play multiple audio effect files at the same time according to your requirements. When playing an audio effect file, you can set the playback options, such as number of loops, pitch, volume, and playback position.

    Call playEffect after joining a channel.

    _17
    // Starts to play the audio effect file.
    _17
    audioEffectManager.playEffect(
    _17
    0, // Sets the audio effect ID.
    _17
    "your file path", // Sets the path of the audio effect file.
    _17
    -1, // Sets the number of times the audio effect loops. -1 represents an infinite loop.
    _17
    1, // Sets the pitch of the audio effect. The value range is 0.5 to 2.0, where 1.0 is the original pitch.
    _17
    0.0, // Sets the spatial position of the audio effect. The value range is -1.0 to 1.0. -1.0 represents the audio effect occurs on the left; 0 represents the audio effect occurs in the front; 1.0 represents the audio effect occurs on the right.
    _17
    100, // Sets the volume of the audio effect. The value range is 0 to 100. 100 represents the original volume.
    _17
    true, // Sets whether to publish the audio effect to the remote users. true represents that both the local user and remote users can hear the audio effect; false represents that only the local user can hear the audio effect.
    _17
    0 // Sets the playback position (ms) of the audio effect file. 0 represents that the playback starts at the 0 ms mark of the audio effect file.
    _17
    );
    _17
    _17
    // Occurs when the local audio effect playback finishes.
    _17
    @Override
    _17
    public void onAudioEffectFinished(int soundId) {
    _17
    super.onAudioEffectFinished(soundId);
    _17
    }

    After successfully playing audio effect files, you can stop playing either a specified file or all audio effect files.


    _5
    // Stops playing a specified audio effect file.
    _5
    audioEffectManager.stopEffect(id);
    _5
    _5
    // Stops playing all audio effect files.
    _5
    audioEffectManager.stopAllEffects();

    Pausing and resumption

    When you play audio effect files, you can pause or resume playing either a specified file or all audio effect files.

    Call this group of methods after playEffect.

    _11
    // Pauses playing a specified audio effect file.
    _11
    audioEffectManager.pauseEffect(id);
    _11
    _11
    // Pauses playing all audio effect files.
    _11
    audioEffectManager.pauseAllEffects();
    _11
    _11
    // Resumes playing a specified audio effect file.
    _11
    audioEffectManager.resumeEffect(id);
    _11
    _11
    // Resumes playing all audio effect files.
    _11
    audioEffectManager.resumeAllEffects();

    Playback position

    If you need to adjust the playback position after playing the audio effect file, you can call this group of methods. For example, you can call this group of methods to adjust the playback position during looped playback of the audio effect file without stopping the playback.

  • Call getEffectDuration after joining a channel, and call other methods in this group after playEffect.
  • This group of methods only applies to the local audio effect files.

  • _8
    // Gets the total duration of a specified local audio effect file.
    _8
    audioEffectManager.getEffectDuration("your file path");
    _8
    _8
    // Sets the playback position (ms) of a specified local audio effect file. 500 represents that the playback starts at the 500 ms mark of the audio effect file.
    _8
    audioEffectManager.setEffectPosition(id, 500);
    _8
    _8
    // Gets the current playback position of a specified local audio effect file.
    _8
    audioEffectManager.getEffectCurrentPosition(id);

    Volume

    After the audio effect file starts to play, you can call this group of methods to adjust the playback volume. For example, you can call this group of methods to adjust the playback volume during looped playback of the audio effect file without stopping the playback.

    Call this group of methods after playEffect.

    _8
    // Sets the playback volume of all audio effect files. The value range is 0 to 100. 100 represents the original volume.
    _8
    audioEffectManager.setEffectsVolume(50.0);
    _8
    _8
    // Sets the playback volume of a specified audio effect file. The value range is 0 to 100. 100 represents the original volume.
    _8
    audioEffectManager.setVolumeOfEffect(id, 50.0);
    _8
    _8
    // Gets the playback volume of all audio effect files. The volume range is 0 to 100. 100 represents the original volume.
    _8
    audioEffectManager.getEffectsVolume();

    API reference

    Audio mixing

    Audio mixing means mixing a music file with the audio captured by a microphone. Users who use the audio mixing function often play a relatively long music file and only play one music file at a time. For example, users can play accompaniment when singing or play background music when chatting.

    Agora provides a group of methods to play and manage the music file. Supported functions include the following:

    • Play the local or online music file.
    • Set the number of playbacks , playback position, volume, pitch, and other playback options of a music file.
    • Flexibly control the playing, pausing, resumption, and stopping of the music file.
    • Report the current playback state of the music file and the reason for the change.
  • Supported audio effect file formats include MP3, AAC, M4A, MP4, WAV, and 3GP. See Media Formats Supported by Android.
  • The Android system version needs to be 4.2 or later, and the API level need to be 16 or later.
  • If you call this group of methods on an emulator, you can only play the MP3 files in the /sdcard/ folder.
  • After successfully calling startAudioMixing, the SDK triggers the onAudioMixingStateChanged callback when the playback state of the music file changes.

    Playing and stopping

    Call startAudioMixing to play a music file. When playing a music file, you can set the playback options, such as number of loops and playback position.

    If you call startAudioMixing again when the SDK is playing a music file, the SDK automatically stops playing the previous music file and starts playing the next music file.

    _14
    // Starts to play the music file.
    _14
    engine.startAudioMixing(
    _14
    "your file path", // Specifies the absolute path of the local or online music file that you want to play.
    _14
    false, // Sets whether to only play a music file on the local client. true represents that only the local user can hear the music; false represents that both the local user and remote users can hear the music.
    _14
    false, // Sets whether to replace the audio captured by the microphone with a music file. true represents that the user can only hear music; false represents that the user can hear both the music and the audio captured by the microphone.
    _14
    -1, // Sets the number of times to play the music file. 1 represents play the file once.
    _14
    0 // Sets the playback position (ms) of the music file. 0 represents that the playback starts at the 0 ms mark of the music file.
    _14
    );
    _14
    _14
    // Occurs when the state of the local user's music file changes.
    _14
    @Override
    _14
    public void onAudioMixingStateChanged(int state, int reason) {
    _14
    super.onAudioMixingStateChanged(state, reason);
    _14
    }

    After successfully playing the music file, you can call stopAudioMixing to stop the playback.


    _2
    // Stops playing the music file.
    _2
    engine.stopAudioMixing();

    Pausing and resumption

    When you play the music file, you can pause or resume playing the music file.

    Call this group of methods after calling startAudioMixing.

    _5
    // Pauses playing the music file.
    _5
    engine.pauseAudioMixing();
    _5
    _5
    // Resumes playing the music file.
    _5
    engine.resumeAudioMixing();

    Playback position

    When the music file is playing, you can call this group of methods to adjust the playback position of the music file without stopping the playback.

    Call this group of methods after calling startAudioMixing and receiving the onAudioMixingStateChanged(MEDIA_ENGINE_AUDIO_EVENT_MIXING_PLAY) callback.

    _8
    // Gets the total duration of the current music file.
    _8
    engine.getAudioMixingDuration();
    _8
    _8
    // Sets the playback position (ms) of the current music file. 500 represents that the playback starts at the 500 ms mark of the music file.
    _8
    engine.setAudioMixingPosition(500);
    _8
    _8
    // Gets the current playback position of the music file.
    _8
    engine.getAudioMixingCurrentPosition();

    Volume and pitch

    After successfully playing the music file, you can call this group of methods to adjust the playback volume and pitch of the music file without stopping the playback.

    Call this group of methods after calling startAudioMixing and receiving the onAudioMixingStateChanged(MEDIA_ENGINE_AUDIO_EVENT_MIXING_PLAY) callback.

    _17
    // Adjusts the playback volume of the current music file for both local and remote playback. The value range is 0 to 100. 100 represents the original volume.
    _17
    engine.adjustAudioMixingVolume(50);
    _17
    _17
    // Adjusts the playback volume of the current music file for the remote playback. The value range is 0 to 100. 100 represents the original volume.
    _17
    engine.adjustAudioMixingPublishVolume(50);
    _17
    _17
    // Adjusts the playback volume of the current music file for the local playback. The value range is 0 to 100. 100 represents the original volume.
    _17
    engine.adjustAudioMixingPlayoutVolume(50);
    _17
    _17
    // Gets the playback volume of the current music file for the local playback. The volume range is 0 to 100. 100 represents the original volume.
    _17
    engine.getAudioMixingPlayoutVolume();
    _17
    _17
    // Gets the playback volume of the current music file for the remote playback. The volume range is 0 to 100. 100 represents the original volume.
    _17
    engine.getAudioMixingPublishVolume();
    _17
    _17
    // Sets the pitch of the current music file. The value range is -12 to 12. 0 represents the original pitch; 1 represents raising the original pitch by a semitone.
    _17
    engine.setAudioMixingPitch(5);

    Playback speed

    After successfully playing a music file, you can call setAudioMixingPlaybackSpeed to set the playback speed of the current music file.

    Call this method after calling startAudioMixing and receiving the onAudioMixingStateChanged(MEDIA_ENGINE_AUDIO_EVENT_MIXING_PLAY) callback.

    _3
    // Agora recommends that you limit this value to between 50 and 400.
    _3
    // 50 represents half the original speed, 100 represents the original speed, and 400 represents 4 times the original speed.
    _3
    engine.setAudioMixingPlaybackSpeed(50);

    Playback track

    After successfully playing a music file, you can call this group of methods to set the playback track of the current music file.

    Call this group of methods after calling startAudioMixing and receiving the onAudioMixingStateChanged(MEDIA_ENGINE_AUDIO_EVENT_MIXING_PLAY) callback.

    _5
    // Gets the number of audio tracks of the current music file.
    _5
    engine.getAudioTrackCount();
    _5
    // Specifies the playback track of the current music file.
    _5
    // The range of index is [0, getAudioTrackCount()).
    _5
    engine.selectAudioTrack(2);

    Playback channel mode

    After successfully playing a music file, you can call setAudioMixingDualMonoMode to set the following channel mode of the current music file:

    • AUDIO_MIXING_DUAL_MONO_AUTO: Original mode.
    • AUDIO_MIXING_DUAL_MONO_L: Left channel mode. This mode replaces the audio of the right channel with the audio of the left channel, which means the user can only hear the audio of the left channel.
    • AUDIO_MIXING_DUAL_MONO_R: Right channel mode. This mode replaces the audio of the left channel with the audio of the right channel, which means the user can only hear the audio of the right channel.
    • AUDIO_MIXING_DUAL_MONO_MIX: Mixed channel mode. This mode mixes the audio of the left channel and the right channel, which means the user can hear the audio of the left channel and the right channel at the same time.
  • Call this method after calling startAudioMixing and receiving the onAudioMixingStateChanged(MEDIA_ENGINE_AUDIO_EVENT_MIXING_PLAY) callback.
  • This method only applies to stereo audio files.

  • _1
    engine.setAudioMixingDualMonoMode(AUDIO_MIXING_DUAL_MONO_AUTO);

    API reference

    Reference

    When playing a music file, you can also refer to the following articles:

    Video Calling