Voice Call
  • iOS
  • Android : Java
  • Web
  • Flutter
  • React Native
  • Electron
  • Unity3D
  • Cocos Creator
  • Windows
  • macOS
  • Linux
  • Overview
  • Develop your app
    • Quick start
    • Enhance basic feature
      • Use Tokens for authentication
      • Set up common audio config
      • Check the room connection status
  • Best practices
    • Implement a live audio room
  • Upgrade using advanced features
    • Advanced features
      • Configure the audio
        • Beautify & Change the voice
      • Improve audio quality
        • Test network and devices in advance
        • Monitor streaming quality
        • Visualize the sound level
      • Message signaling
        • Broadcast real-time messages to a room
        • Convey extra information using SEI
        • Quotas and limits
      • Play media files
        • Play media files
        • Play sound effects
      • Mix the streams
      • Record media data
      • Encrypt the streams
    • Distincitve features
      • Join multiple rooms
      • Customize the audio
      • Play streams via URL
      • Low-latency live streaming
  • Resources & Reference
    • SDK
    • Sample code
    • API reference
    • Debugging
    • FAQs
    • Key concepts
  • Documentation
  • Voice Call
  • Upgrade using advanced features
  • Advanced features
  • Play media files
  • Play sound effects

Play sound effects

Last updated:2023-03-09 17:06

Introduction

Audio effects mainly refer to short audio effects played to enhance the sense of reality or to enhance the atmosphere of the scene. For example: during the live broadcast, there are often scenes where audio effects are played, such as applause, gift audio effects, prompt audio, etc. In the game, sometimes it is necessary to play bullets, collision audios, etc.

ZEGOCLOUD’s SDKs provide an audio effect player, unified management of audio effects through ZegoEffectAudioPlayer, supports audio effect playback (multiple audio effects can be overlapped), playback control (such as pause playback, volume adjustment, set playback progress), preload audio effects and other functions.

Support Formats

The sound effect player supports playing local audio files in MP3, M4A, AAC, and WAV formats.

Prerequisites

Before you begin, make sure you complete the following:

  • Create a project in ZEGOCLOUD Admin Console and get the AppID and AppSign of your project.

  • Refer to the Quick Start doc to complete the SDK integration and basic function implementation.

Create an Audio Effect Player

Call the createAudioEffectPlayer method of ZegoExpressEngine to create an audio effect player instance.

The engine currently only supports the creation of one instance at the same time, and it will return null after exceeding.

ZegoAudioEffectPlayer audioEffectPlayer = ZegoExpressEngine.getEngine().createAudioEffectPlayer();

Play Control

(Optional) Set Event Callback for Audio Effect Player

Set Event Callback for Audio Effect Player

You can call the setEventHandler method of the audio effect player to set event callbacks for the player as needed, Used to monitor notifications such as "audio effect playback status change".

audioEffectPlayer.setEventHandler(new IZegoAudioEffectPlayerEventHandler() {
    @Override
    public void onAudioEffectPlayStateUpdate(ZegoAudioEffectPlayer audioEffectPlayer, int audioEffectID, ZegoAudioEffectPlayState state, int errorCode) {
                        Log.d("[ZEGO]", "onAudioEffectPlayStateUpdate errorCode:" + errorCode + "  audioEffectID:" + audioEffectID + "  state:" + state);
    }
});

Start Playing

Call start method to play audio effects, currently only supports 12 at the same time, and only It is a local file and does not support playing network resources. "AudioEffectID" needs to be globally unique.

  • If you have pre-loaded audio effects through the loadResource method, you only need Pass in the "audioEffectID" when preloading, and pass the "path" (path of the audio effect resource) field blank.

  • If you need to repeat by ZegoAudioEffectPlayConfig in "playCount" configure the number of repetitions. If it is set to "0", it means that the playback is repeated indefinitely, until the user manually calls stop to stop.

int audioEffectID = 1;
ZegoAudioEffectPlayConfig config = new ZegoAudioEffectPlayConfig();
config.playCount = 10;
config.isPublishOut = true;
audioEffectPlayer.start(audioEffectID,"/storage/emulated/0/Android/data/im.zego.express.example.video/files/3-s.mp3",config);   

Pause/Resume/Stop Playing

  1. Call pause method to pause the audio effect specified by "audioEffectID", call pauseAll method pauses all audio effects that are playing.

  2. After the audio effect is paused, call the resume method to resume the audio effect specified by "audioEffectID", call resumeAll method will resume all paused audio effects.

  3. Call stop method to stop playing the audio effect specified by "audioEffectID", call stopAll method will stop all audio effects.

audioEffectPlayer.pause(audioEffectID);
audioEffectPlayer.resume(audioEffectID);
audioEffectPlayer.stop(audioEffectID);
audioEffectPlayer.pauseAll();
audioEffectPlayer.resumeAll();
audioEffectPlayer.stopAll();

Adjust Volume

  1. Call setVolume method to set the audio effect volume specified by "audioEffectID", The value range is [0, 200] and the default value is "100".

  2. Call setVolumeAll method to set the volume of all audio effects at the same time, the value range is [0, 200] and the default value is "100".

int volume =70;
audioEffectPlayer.setVolume(audioEffectID, volume);

// Set the volume of all sound effects
audioEffectPlayer.setVolumeAll(volume);

Play Progress Control

  1. Call getTotalDuration method to get the total duration of a single audio effect.

  2. Call getCurrentProgress method to get the current playback progress of the audio effect.

  3. Call seekTo method to set the playback progress as needed.

// Get the total duration of the sound effect
long totalDuration = audioEffectPlayer.getTotalDuration(audioEffectID);
// Get the current playback progress of the sound effect
long progress = audioEffectPlayer.getCurrentProgress(audioEffectID);
// Set playback progress
audioEffectPlayer.seekTo(audioEffectID, 1, new IZegoAudioEffectPlayerSeekToCallback() {
    @Override
    public void onSeekToCallback(int errorCode) {
        Log.d("[ZEGO]", "onSeekToCallback errorCode:" + errorCode);
    }
});

(Optional) Preload Resources

Preload Resources

In scenarios where the same audio effect is frequently played, the SDK provides the function of preloading the audio effect file into the memory in order to optimize the performance of repeatedly reading and decoding the file.

Call loadResource method to load the audio effect resource, you can use "callback" Parameter to monitor the result of loading, display after successful loading, it can be played. It supports up to 15 local audio effect files to be preloaded at the same time (network resources are not supported), and the duration of a single audio effect file cannot exceed 30 seconds, otherwise an error will be reported when loading.

When the loaded audio effects are used, you can call the unloadResource interface to unload to release the related resources. Otherwise SDK will be ZegoAudioEffectPlayer automatically uninstalls audio loaded instance is released.

Preloading is an unnecessary operation. It is recommended to improve performance or to play a specific audio effect repeatedly.

// Load sound effect resources
audioEffectPlayer.loadResource(audioEffectID, "/storage/emulated/0/Android/data/im.zego.express.example.video/files/3-s.mp3", new IZegoAudioEffectPlayerLoadResourceCallback() {
    @Override
    public void onLoadResourceCallback(int i) {
        Log.d("[ZEGO]", "onLoadResourceCallback errorCode:" + i );

    }
});

// Uninstall sound effect resources
audioEffectPlayer.unloadResource(audioEffectID);

Destroy the Media Player

After using the audio effect player, you need to call the engine’s destroyAudioEffectPlayer method to destroy and release in time Resources occupied by this player.

engine.destroyAudioEffectPlayer(audioEffectPlayer);

FAQ

  1. What is the difference between a audio effect player and a media player?

    • The media player is mainly used to play videos and longer music, and supports the playback of network resources. At most 4 player instances can be created at the same time, and one instance can only play one audio and video resource.
    • The audio effect player is mainly used for audio resources with a short playing time, and does not support playing network resources. Only one audio effect player instance can be created at a time. The audio effect player supports concurrent playback of multiple audio resources. One instance can play up to 12 audio resources at the same time.
Page Directory