Audio mixing and sound effects
Video SDK makes it simple for you to publish audio captured through the microphone to subscribers in a channel. In some real-time audio and video scenarios, such as games or karaoke, you need to play sound effects or mix in music files to enhance the atmosphere and add interest. Video SDK enables you to add sound effects and mix in pre-recorded audio.
This page shows you how to implement audio mixing and playing sound effects in your app.
Understand the tech
Video SDK provides APIs that enable you to implement:
-
Audio mixing
Mix in music file such as background music with microphone audio. Using this feature, you can play only one file at a time.
-
Sound effects
Play audios with a short duration. For example, applause, cheers, or gunshots. You can play multiple sound effects at the same time.
Prerequisites
Ensure that you have:
- Implemented the SDK quickstart in your project.
- Audio files in one of the supported formats.
Implement audio features
This section shows you how to implement playing sound effects and add audio mixing in your app.
To manage audio mixing and voice effects, Video SDK provides the following APIs:
Function | Sound effect | Audio mixing |
---|---|---|
Play or stop playing a specific audio file | preloadEffect unloadEffect playEffect stopEffect stopAllEffects | startAudioMixing stopAudioMixing |
Pause or resume playing an audio file | pauseEffect pauseAllEffects resumeEffect resumeAllEffects | pauseAudioMixing resumeAudioMixing |
Get and adjust playback position and volume | setEffectPosition getEffectCurrentPosition getEffectsVolume setEffectsVolume setVolumeOfEffect | getAudioMixingCurrentPosition setAudioMixingPosition getAudioMixingPublishVolume adjustAudioMixingPublishVolume getAudioMixingPlayoutVolume adjustAudioMixingPlayoutVolume |
Report playback status of audio files | onAudioEffectFinished | onAudioMixingStateChanged |
Play sound effects
Before joining a channel, call preloadEffect
to preload the sound effect file. After joining the channel, call playEffect
to play the specified sound effect file. To play multiple sound effect files simultaneously, set multiple sound effect IDs and call playEffect
multiple times. After a sound effect is played, the Video SDK triggers the onAudioEffectFinished
callback.
To implement this logic, refer to the following sample code:
Incorporate audio mixing
To play a music file, call startAudioMixing
before or after joining a channel. After you successfully call this method, Video SDK triggers the onAudioMixingStateChanged
callback when the mixing status changes. This callback also reports the reason for the state change.
To implement this logic, refer to the following code:
Reference
This section contains content that completes the information on this page, or points you to documentation that explains other aspects to this product.
Sample projects
Agora provides the following open source sample projects on GitHub for your reference.
Download or view the source code for more detailed examples.