# Audio mixing and sound effects (/en/realtime-media/voice/build/enhance-the-audio-experience/audio-mixing-and-sound-effects/unity)

> For AI agents: see the complete documentation index at [llms.txt](/llms.txt).

Voice 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. Voice 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 [#understand-the-tech-7]

    Voice 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 [#prerequisites-7]

    Ensure that you have:

    * Implemented the [SDK quickstart](../index.mdx) in your project.

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

    ## Play sound effects and music [#play-sound-effects-and-music-6]

    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:

    | Function                                    | Sound effect                                                                                                                     | Audio mixing                                                                                                                                                                                                         |
    | ------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
    | Play or stop playing a specific audio file  | `PreloadEffect`<br /> `UnloadEffect`<br /> `PlayEffect`<br /> `StopEffect`<br /> `StopAllEffects`                                | `StartAudioMixing`<br /> `StopAudioMixing`                                                                                                                                                                           |
    | Pause or resume playing an audio file       | `PauseEffect`<br /> `PauseAllEffects`<br /> `ResumeEffect`<br /> `ResumeAllEffects`                                              | `PauseAudioMixing`<br /> `ResumeAudioMixing`                                                                                                                                                                         |
    | Get and adjust playback position and volume | `SetEffectPosition`<br /> `GetEffectCurrentPosition`<br /> `GetEffectsVolume`<br /> `SetEffectsVolume`<br /> `SetVolumeOfEffect` | `GetAudioMixingCurrentPosition`<br /> `SetAudioMixingPosition`<br /> `GetAudioMixingPublishVolume`<br /> `AdjustAudioMixingPublishVolume`<br /> `GetAudioMixingPlayoutVolume`<br /> `AdjustAudioMixingPlayoutVolume` |
    | Report playback status of audio files       | `OnAudioEffectFinished`                                                                                                          | `OnAudioMixingStateChanged`                                                                                                                                                                                          |

    ### Play sound effects [#play-sound-effects-6]

    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:

    ```csharp
    // 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 [#incorporate-audio-mixing-6]

    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:

    ```csharp
    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.

    <CalloutContainer type="warning">
      <CalloutDescription>
        If you play a short sound effect file using `StartAudioMixing`, or a long music file using `PlayEffect`, the playback may fail.
      </CalloutDescription>
    </CalloutContainer>

    ## Reference [#reference-7]

    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 [#sample-project-3]

    Agora provides an open source [AudioMixing](https://github.com/AgoraIO-Extensions/Agora-Unity-Quickstart/blob/main/API-Example-Unity/Assets/API-Example/Examples/Advanced/AudioMixing/AudioMixing.cs) project on GitHub for your reference. Download or view the source code for a more detailed example.

    ### API reference [#api-reference-7]

    * [`PreloadEffect`](https://api-ref.agora.io/en/video-sdk/unity/4.x/API/class_irtcengine.html#api_irtcengine_preloadeffect)

    * [`PlayEffect`](https://api-ref.agora.io/en/video-sdk/unity/4.x/API/class_irtcengine.html#api_irtcengine_playeffect3)

    * [`OnAudioEffectFinished`](https://api-ref.agora.io/en/video-sdk/unity/4.x/API/class_irtcengineeventhandler.html#callback_irtcengineeventhandler_onaudioeffectfinished)

    * [`StartAudioMixing`](https://api-ref.agora.io/en/video-sdk/unity/4.x/API/class_irtcengine.html#api_irtcengine_startaudiomixing)

    * [`OnAudioMixingStateChanged`](https://api-ref.agora.io/en/video-sdk/unity/4.x/API/class_irtcengineeventhandler.html#callback_irtcengineeventhandler_onaudiomixingstatechanged)

    * [`PauseAudioMixing`](https://api-ref.agora.io/en/video-sdk/unity/4.x/API/class_irtcengine.html#api_irtcengine_pauseaudiomixing)

    * [`ResumeAudioMixing`](https://api-ref.agora.io/en/video-sdk/unity/4.x/API/class_irtcengine.html#api_irtcengine_resumeaudiomixing)

    * [`StopAudioMixing`](https://api-ref.agora.io/en/video-sdk/unity/4.x/API/class_irtcengine.html#api_irtcengine_stopaudiomixing)

    * [`AdjustAudioMixingPlayoutVolume`](https://api-ref.agora.io/en/video-sdk/unity/4.x/API/class_irtcengine.html#api_irtcengine_adjustaudiomixingplayoutvolume)

    * [`AdjustAudioMixingPublishVolume`](https://api-ref.agora.io/en/video-sdk/unity/4.x/API/class_irtcengine.html#api_irtcengine_adjustaudiomixingpublishvolume)

    
  
