current position:Home>How to realize Android entertainment live broadcast / game special sound effect

How to realize Android entertainment live broadcast / game special sound effect

2022-07-17 07:37:56Zego instant developer

1 Function introduction

We're on the air , In order to enhance the sense of reality , The short effect sound that needs to be played to set off the scene atmosphere . for example : Applause 、 Laughter 、 Gift sound 、 Cue tone, etc . In the game , Sometimes you need to play bullets 、 Impact sound, etc .

ZegoExpress SDK Provide sound file player , adopt ZegoAudioEffectPlayer Unified management of sound effects , Support sound effect play ( Multiple sound effects can be overlapped )、 Playback controls ( Such as pause play 、 Volume adjustment 、 Set the playback schedule )、 Pre loading sound effects and other functions .

2 Support formats

The sound file player supports playing MP3、M4A、AAC、WAV Local audio file in format .

3 Sample source code download

Please refer to Download sample source code Access to the source code .

Please check the relevant source code “/ZegoExpressExample/AdvancedAudioProcessing/src/main/java/im/zego/advancedaudioprocessing/audioeffectplayer” A file in a directory .

4 Prerequisite

Before implementing the function of sound file player , Please make sure :

5 Use steps

5.1 Create a sound player

call ZegoExpressEngine Of createAudioEffectPlayer Method to create a sound player instance .

Only one instance can be created at the same time , When exceeded, it will return to null.

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

5.2 Playback controls

5.2.1 ( Optional ) Set event callback for sound player

Sound player event callback settings

You can call the... Of the sound player as needed setEventHandler Method to set the event callback for the player , For monitoring “ Sound playback status changes ” Wait for notice .

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

5.2.2 Start playing

call start Method to play the sound effect , At present, only simultaneous playback is supported 12 individual , And can only be local files , Playing network resources is not supported . “audioEffectID” Need to keep the whole situation unique .

  • If it has passed loadResource Method preloads the sound effects , Then you only need to pass in the “audioEffectID”,“path”( The path of sound resources ) Just leave the field blank .
  • If you need to play repeatedly, you can use ZegoAudioEffectPlayConfig in “playCount” Configure the number of repetitions . If set to “0”, Play infinity means repeat , Until the user manually calls stop stop it .
int audioEffectID = 1;
ZegoAudioEffectPlayConfig config = new ZegoAudioEffectPlayConfig();
config.playCount = 10;
config.isPublishOut = true;

5.2.3 Pause / recovery / Stop playing

  1. call pause Method can pause the playback “audioEffectID” Specified sound effect , call pauseAll Method pauses all playing sound effects .
  2. After the sound effect pauses , call resume Method to resume playback “audioEffectID” Specified sound effect , call resumeAll Method restores all paused sound effects .
  3. call stop Method can stop playing “audioEffectID” Specified sound effect , call stopAll Method stops playing all sound effects .

5.2.4 Adjust the volume

  1. call setVolume Method can be set “audioEffectID” Specified sound volume , The value range is [0, 200], The default value is “100”.
  2. call setVolumeAll Method sets the volume of all sound effects at the same time , The value range is [0, 200], The default value is “100”.
int volume =70;
audioEffectPlayer.setVolume(audioEffectID, volume);

//  Set the volume of all sound effects 

5.2.5 Play progress control

  1. call getTotalDuration Method to obtain the total duration of a single sound effect .
  2. call getCurrentProgress Method to obtain the current playback progress of the sound effect .
  3. call seekTo Method can set the playback progress as needed .
//  The total time to get the sound effect 
long totalDuration = audioEffectPlayer.getTotalDuration(audioEffectID);
//  Get the current playback progress of the sound effect 
long progress = audioEffectPlayer.getCurrentProgress(audioEffectID);
//  Set the playback schedule 
audioEffectPlayer.seekTo(audioEffectID, 1, new IZegoAudioEffectPlayerSeekToCallback() {
    public void onSeekToCallback(int errorCode) {
        Log.d("[ZEGO]", "onSeekToCallback errorCode:" + errorCode);

5.3 ( Optional ) Preload resources

Preload resources

In a scene where the same sound effect is played frequently ,SDK In order to optimize the performance of repeatedly reading and decoding files , It provides the function of preloading sound effect files into memory .

call loadResource Method to load sound effect resources , It can be done by “callback” Parameter to listen for the loading result , The display can only be played after loading successfully . Support simultaneous preloading at most 15 A local sound file ( Network resources are not supported ), And the duration of a single sound effect file cannot exceed 30 s, Otherwise, an error will be reported .

When the loaded sound effect is used , You can call unloadResource Interface uninstall , To release resources . otherwise SDK Will be in ZegoAudioEffectPlayer The loaded sound effects will be automatically unloaded when the instance is released .

Preloading is not required , In order to improve performance or when you need to play a specific sound effect repeatedly, it is recommended to use .

//  Load sound resources 
audioEffectPlayer.loadResource(audioEffectID, "/storage/emulated/0/Android/data/", new IZegoAudioEffectPlayerLoadResourceCallback() {
    public void onLoadResourceCallback(int i) {
        Log.d("[ZEGO]", "onLoadResourceCallback errorCode:" + i );                    


//  Uninstall sound resources 

5.4 Destroy the media player

After using the sound player , Need to call in time destroyAudioEffectPlayer Method destruction , Release the resources occupied by the player .


6 API Reference list

Method describe
createAudioEffectPlayer Create a sound player instance
setEventHandler Set the sound player callback
start Play sound effects
pause Pause playing a single sound effect
pauseAll Pause all sound effects
resume Resume playing a single sound effect
resumeAll Resume playing all sound effects
stop Stop playing a single sound effect
stopAll Stop playing all sound effects
setVolume Adjust sound volume
setVolumeAll Adjust the volume of all sound effects
getTotalDuration Control playback progress
getCurrentProgress Get the current playback progress
seekTo Set the specified playback progress
loadResource Preload resources
unloadResource Unload resources
destroyAudioEffectPlayer Destroy the sound player instance

7 What's the difference between a sound player and a media player ?

  • Media player is mainly used to play video and long music , Support playing network resources . At most... Can be created at the same time 4 Player instances , An instance can only play one audio and video .
  • The sound effect player is mainly used for playing sound effects with short time , Playing network resources is not supported . Only one instance of sound player can be created at the same time , The sound player supports simultaneous playback of multi-channel sound effects , At most one instance can be played simultaneously 12 A sound effect .

obtain Demo

obtain In this paper, the Demo、 Developing documents 、 Technical support .
obtain SDK Business activities 、 Hot products .
Registration constitutes ZEGO Developer account number , Quick start .

copyright notice
author[Zego instant developer],Please bring the original link to reprint, thank you.

Random recommended