Audio mixing and sound effects

Updated

Play audio files and add sound effects to enhance the audio experience.

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 use-cases, 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 game.

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.

  • Set a valid path to the music or sound effect file to be played.

Play sound effects and music

This section shows you how to implement playing sound effects and add audio mixing in your game.

To manage audio mixing and sound effects, Video SDK provides the following APIs:

FunctionSound effectAudio mixing
Play or stop playing a specific audio filePreloadEffect
UnloadEffect
PlayEffect
StopEffect
StopAllEffects
StartAudioMixing
StopAudioMixing
Pause or resume playing an audio filePauseEffect
PauseAllEffects
ResumeEffect
ResumeAllEffects
PauseAudioMixing
ResumeAudioMixing
Get and adjust playback position and volumeSetEffectPosition
GetEffectCurrentPosition
GetEffectsVolume
SetEffectsVolume
SetVolumeOfEffect
GetAudioMixingCurrentPosition
SetAudioMixingPosition
GetAudioMixingPublishVolume
AdjustAudioMixingPublishVolume
GetAudioMixingPlayoutVolume
AdjustAudioMixingPlayoutVolume
Report playback status of audio filesOnAudioEffectFinishedOnAudioMixingStateChanged

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:

// Preload sound files before joining a channel
RtcEngine.PreloadEffect(1, "File Path 1", 0);
RtcEngine.PreloadEffect(2, "File Path 2", 0);
RtcEngine.PreloadEffect(3, "File Path 3", 0);

// Play preloaded sound files after joining a channel
RtcEngine.PlayEffect(1, "File Path 1", 1, 0, 0, 0, true, 0);
RtcEngine.PlayEffect(2, "File Path 2", 1, 0, 0, 0, true, 0);
RtcEngine.PlayEffect(3, "File Path 3", 1, 0, 0, 0, true, 0);

// Triggered when local sound file playback ends
public override void OnAudioEffectFinished(int soundId)
{
  Debug.Log("effect play finish :" + soundId);
}

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:

RtcEngine.StartAudioMixing("File Path", false, -1);

public override void OnAudioMixingStateChanged(
  AUDIO_MIXING_STATE_TYPE state,
  AUDIO_MIXING_REASON_TYPE errorCode)
{
  Debug.Log(string.Format(
    "AUDIO_MIXING_STATE_TYPE: {0}, AUDIO_MIXING_REASON_TYPE: {1}",
    state, errorCode));
}

Control playback using the following methods:

  • PauseAudioMixing: Pause playback.
  • ResumeAudioMixing: Resume playback.
  • StopAudioMixing: Stop playing.
  • SetAudioMixingPosition: Set the playing position of the current audio file.
  • AdjustAudioMixingPlayoutVolume: Adjust the volume of the current audio file played locally.
  • AdjustAudioMixingPublishVolume: Adjust the volume of the current audio file played at the remote end.

If you play a short sound effect file using StartAudioMixing, or a long music file using PlayEffect, the playback may fail.

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 project

Agora provides an open source AudioMixing project on GitHub for your reference. Download or view the source code for a more detailed example.

API reference