IOS Help: After upgrading to 3.7.1 ads no longer pause game music

I am having trouble after upgrading to Unity Ad 3.7.1: Video ads no longer pause the background music. This is the case for both by regular and rewarded ads. The ads play normally, but my BGM just keeps running behind them.

Is there anything I need to set so that the audio is properly paused as in previous versions?

Alternatively, if I set the master mixer to -80 in OnUnityAdsDidStart, will that mute the ad too?

Thanks in advance

1 Like

I noticed this in other ad networks too - not just Unity.
Now we just manually pause the music during ad play.

1 Like

I also have same problem on UnityAds 3.7.1.

I confirmed this issue does not occur on Android.
On Android, when the ad is displayed, the BGM of the app paused.

I guess it’s strange that only iOS works differently.

Thank you.

1 Like

Noticed the same! But my android version might still be running 3.6.1, so grain of salt on my side.

Here is what I ended up doing for anyone who needs a quick fix in two parts. Slight changes from my version to make is generic. (Hoping this is typo free!)

  1. I made a a script to do a bit of fading that I could put on my ad playing GameObjects
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Audio;

public class AudioFadeScript : MonoBehaviour
{
    public AudioMixer bgmMixer; // Set in inspector
    public float fadeDuration = 0.75f;
    public float defaultVolue = 0.0f;
    public string mixerVolumeString = "BGMVolume";

    [Header("Internal tracking")]
    [SerializeField] private float audioRestoreValue = 0.0f;
    [SerializeField] private static float AUDIO_OFF_VALUE = -80.0f;
    [SerializeField]private bool fadingBGM = false;
    [SerializeField]private bool restoringBGM = false;

    void Start()
    {
        audioRestoreValue = defaultVolume;

        float myBGMVolume;
        bool safety = bgmMixer.GetFloat(mixerVolumeString,out myBGMVolume);

        if (!safety) {
            Debug.LogWarning("AudioFadeScript: Mixer is not properly configured.");
        }

        if (audioRestoreValue != myBGMVolume ) {
            Debug.LogWarning("AudioFadeScript: Starting audio does not match current audio. Audio glitch may occur");
        }
    }

    void OnDisable() {
        bgmMixer.SetFloat(mixerVolumeString, audioRestoreValue);
    }

    void OnDestroy() {
        bgmMixer.SetFloat(mixerVolumeString, audioRestoreValue);
    }

    public void StartFadeBGM() {
        if (fadingBGM) return;
        if (restoringBGM) {
            StopCoroutine("RestoreMusic");
            restoringBGM = false;
        }
        StartCoroutine("FadeMusic");
    }

    public void RestoreBGM() {
        if (restoringBGM) return;
        if (fadingBGM) {
            StopCoroutine("FadeMusic");
            fadingBGM = false;
        }
        StartCoroutine("RestoreMusic");
    }

    private IEnumerator FadeMusic() {
        float myBGMVolume;
        bgmMixer.GetFloat(mixerVolumeString,out myBGMVolume); // we could use PlayerInfoController.instance.prefs.Audio_BGMVolume;
        audioRestoreValue = myBGMVolume;

        if ( audioRestoreValue == AUDIO_OFF_VALUE ) {
            yield break;
        }
        fadingBGM = true;
        float maths = audioRestoreValue - AUDIO_OFF_VALUE; //0 - -80.0 = 80.0;
      
        while (myBGMVolume > AUDIO_OFF_VALUE) {
            bgmMixer.GetFloat(mixerVolumeString,out myBGMVolume); // Set myBGMVolume
            float foo = myBGMVolume - ( maths* Time.deltaTime / fadeDuration );
            bgmMixer.SetFloat(mixerVolumeString, foo > AUDIO_OFF_VALUE ? foo : AUDIO_OFF_VALUE );
            yield return null;
        }

        fadingBGM = false;
        yield return null;  
    }

    private IEnumerator RestoreMusic() {
        if ( audioRestoreValue == AUDIO_OFF_VALUE ) {
            yield break;
        }
      
        restoringBGM = true;
        float myBGMVolume;
        bgmMixer.GetFloat(mixerVolumeString,out myBGMVolume);
        float maths = audioRestoreValue - AUDIO_OFF_VALUE; //0 - -80.0 = 80.0;
      
        while (myBGMVolume < audioRestoreValue) {
            bgmMixer.GetFloat(mixerVolumeString,out myBGMVolume); // Set myBGMVolume
            float foo = myBGMVolume + ( maths * Time.deltaTime / fadeDuration );
            bgmMixer.SetFloat(mixerVolumeString, foo < audioRestoreValue ? foo : audioRestoreValue );
            yield return null;
        }  
        restoringBGM = false;
        yield return null;
    }
}
  1. In my IUnityAdsListener I add use the above
public class PlayVideoExample : MonoBehaviour, IUnityAdsListener
{
    public AudioFadeScript audioFadeScript;
    public string mySurfacingId; // set in editor
...
...
...
    public void ShowAd() {
            if (audioFadeScript != null) audioFadeScript.StartFadeBGM();
        Advertisement.Show(mySurfacingId);
    }

    public void OnUnityAdsDidFinish(string surfacingId, ShowResult showResult) {
        if (audioFadeScript != null) audioFadeScript.RestoreBGM();
    }

    public void OnUnityAdsDidError (string message) {
        if (audioFadeScript != null) audioFadeScript.RestoreBGM();
        Debug.LogWarning (message);
    }
}

This problems can cause much more suffisticated problems for some of us. Because not only keeps the music playing, but the whole game will be calculated in the background. The OnApplicationFocus method no longer gets invoked, which for our app creates some huge problems.
For us we will downgrade to 3.5.2 again and hope that this won’t affect the ECPM too much.

Let’s hope, that a new version is released soon, that fixes the bug.

@esunityuser , @xjjon , @ijustcom , @Master_Zen

Thanks for reporting this.

I think for the time being, we would encourage you to pause the audio manually in your games.

I hadn’t seen this aspect reported before. I don’t believe the Ads SDK handles this; it should be done by the engine itself. Which version of the Unity engine are you using?

2020.3.5f1 and Unity Ads 3.7.1

I have the same problem on 2019.4.15 and Unity Ads 3.7.1

Having the same issue on Unity 2018.4.33, and 3.7.1 iOS only.
Everything worked fine on 3.5.0.

It won’t work for all. Audio is only a one thing amongst many others that are not paused when ad is in the foreground. The whole game is running beneath it. If this was meant to be “by design”, which I doubt, It’s a breaking change. So actually it’s a bit more serious issue than it might seems to be at first glance.
I’m forced to roll back to previous version.

Updated to 3.7.3. I have not confirmed if the expected pause behaviour has been restored but I can confirm that the pause-the-music mitigation I made as a work around in 3.7.1 did not break anything with 3.7.3 :slight_smile:

We are using Unity 2020.1.15f1. Other versions haven’t been tested.

I am using unity ads mediation facing the same issue what should i do

This is fixed in version 4.0.0 of Unity Ads package. If using Unity Mediation, you can upgrade to 0.4.0 of the package to get that version of Unity Ads.