Voice Call
  • iOS : Objective-C
  • Android
  • Web
  • Flutter
  • React Native
  • Electron
  • Unity3D
  • Cocos Creator
  • Windows
  • macOS
  • Linux
  • Overview
  • Develop your app
    • Integrate the SDK
    • Implement a basic voice call
    • Enhance basic feature
      • Use Tokens for authentication
      • Set up common audio config
      • Check the room connection status
  • 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-05-29 14:42

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 ZegoAudioEffectPlayer, 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 implementing the "Audio effect player" functions, please make sure:

  • ZEGO Express SDK has been integrated into the project to implement basic real-time audio and video functions. For details, please refer to Quick start .
  • A project has been created in ZEGOCLOUD Console and applied for a valid AppID and AppSign. For details, please refer to Console - Project Information .

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 nil after exceeding.

@property (nonatomic, strong) ZegoAudioEffectPlayer *audioEffectPlayer;
self.audioEffectPlayer = [[ZegoExpressEngine sharedEngine] createAudioEffectPlayer];

if (!self.audioEffectPlayer) {

    NSLog(@" Failed to create audio effect player ");

}

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".

[self.audioEffectPlayer setEventHandler:self];
-(void)audioEffectPlayer:(ZegoAudioEffectPlayer *)audioEffectPlayer audioEffectID:(unsigned int)audioEffectID playStateUpdate:(ZegoAudioEffectPlayState)state errorCode:(int)errorCode {

    NSLog(@"Play state update. ID:%d, state:%lu, err:%d", audioEffectID, (unsigned long)state, (int)errorCode);

}

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.

// This example obtains the test.wav resource that has been stored in the App Bundle
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"wav"];

ZegoAudioEffectPlayConfig *config = [[ZegoAudioEffectPlayConfig alloc] init];
config.playCount = 1; // Play once
config.isPublishOut = YES; // Play is mixed into the push stream

// Use 0 as the audioEffectID this time
[self.audioEffectPlayer start:0 path:filePath config: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.

// Pause 0 No. audioEffectID playing resources
[self.audioEffectPlayer pause:0];

// Restore the playback of audioEffectID resource No. 0
[self.audioEffectPlayer resume:0];

// Stop 0 No. audioEffectID playing resources
[self.audioEffectPlayer stop:0];

// Pause the playback of all resources
[self.audioEffectPlayer pauseAll];

// Restore the playback of all resources
[self.audioEffectPlayer resumeAll];

// Stop the playback of all resources
[self.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".

// Set 0 Number audioEffectID volume of 100
[self.audioEffectPlayer setVolume:100 audioEffectID:0];

// Set the volume of all resources to 100
[self.audioEffectPlayer setVolumeAll:100];

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 0 Number audioEffectID the total duration
unsigned long long totalDuration = [self.audioEffectPlayer getTotalDuration:0];

// get 0 No. audioEffectID the current playback progress
unsigned long long currentProgress = [self.audioEffectPlayer getCurrentProgress:0];

// Set 0 No. audioEffectID playback progress as half of the total progress
[self.audioEffectPlayer seekTo:(unsigned long long)(totalDuration / 2) audioEffectID:0 callback:^(int errorCode) {

    NSLog(@"seekTo result: %d", 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.

// This example obtains the test.wav resource that has been stored in the App Bundle
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"wav"];

[self.audioEffectPlayer loadResource:filePath audioEffectID:0 callback:^(int errorCode) {

    NSLog(@"loadResource result, errorCode: %d", errorCode);

}];

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.

[[ZegoExpressEngine sharedEngine] destroyAudioEffectPlayer:self.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