Mute audio of game when ad is playing

Hi,

When an Unity Ad is showing in my game the audio (music) of the game can still be heard. This happens in the Unity editor when i’m in iOS build mode and the ads are in test mode (not sure if this will persist when the game is on app store).

Is their a way to properly disable the audio of the game right before showing the ad and activate it again after the ad?
I used AudioSource.Pause = true; but this will interupt the audio quite harsh. I tried an enumerator to give it a little delay, but this won’t work if I add this just in front of the ShowAd() method.

Any idea how to do this the correct way? This is my code for showing the add:

    //Unity Ads Show When Ready
    public void ShowAd ()
    {
        #if UNITY_EDITOR
            StartCoroutine (WaitForAd());
        #endif

        if (Advertisement.IsReady ())
            Advertisement.Show();
   }

    //Pauses the game to show ads
    IEnumerator WaitForAd ()
    {
        float currentTimeScale = Time.timeScale;
        Time.timeScale = 0f;
        yield return null;

        while (Advertisement.isShowing)
        yield return null;

        Time.timeScale = currentTimeScale;
    }


public void ShowAdsNow (){

            int number = Random.Range(1,6);
            if (number == 4) {
                ShowAd ();

            }
}

so reduce the volume over a half second or so before, then restart and bring the volume back up when you start it again…?

1 Like

I’d suggest using the Audio Mixer (Unity - Manual: Audio Mixer) if you want to perform global sound adjustment, like changing volume. You have the option to assign Audio sources to different sound groups, or just put all audio sources into a single group if you’re just changing the volume of everything together.

1 Like

Thanks for the tips guys. This helped a lot.

This problem only exists in test mode. When you use non-test App ID and Unit ID, there is no problem

Just out of quriosity, did you use the audimixer or just lower the sound while the ads where playing?

Hi @LarsE Not sure, it’s been a while. I’ll open my project tomorrow and let you know

Thanks, I thought this would be automaticly handled by Unity, but seems like i have the same issue. Lowering volume seems like a fairly easy way to go though.