[Unity] [iOS] Getting exception related to orientation when trying to play Interstitial Ad

557 views
Skip to first unread message

Leonardo Bilck

unread,
May 6, 2019, 4:35:48 PM5/6/19
to Google Mobile Ads SDK Developers
Hello,

We are trying to update our app with the latest AdMob for Unity SDK, but we are getting the following error on iOS:
  • 2019-05-06 16:50:57.824261-0300 monicatv[262:5076] <Google> Presenting the interstitial threw exception: Supported orientations has no common orientation with the application, and [GADFullScreenAdViewController shouldAutorotate] is returning YES with trace: ...

Everything works fine on Android but not on iOS.

Here are some code snippets that lead to the bug:

private IEnumerator PlayNextVideoCoRoutine()
        {
            MediaPlayerCtrl.Stop();
            MarkCurrentVideoAsWatched();
            var backupOrientation = Screen.orientation;
            // Reverts to AutoRotation (workaround for AdMob)
            // Please note that at this moment the app has probably locked the orientation to Landscape for gameplay purposes
            Screen.orientation = ScreenOrientation.AutoRotation;
            while(Screen.orientation != ScreenOrientation.AutoRotation) yield return null;
            yield return null;

            yield return AdsController.Instance.TryPlayVideoAdCoRoutine();

            yield return null;
            yield return null;

            Screen.orientation = backupOrientation;

            while(Screen.orientation != backupOrientation) yield return null;
            yield return null;
            
            StartPlayRandomVideoCoroutine(_playingMultipleVideos);
            _controlsPlaybackGroup.PerformTransitionControlsGroup(VideoPlayerControlState.Inactive);
        }

public IEnumerator TryPlayVideoAdCoRoutine()
        {
            if (!CanShowAds) yield break;

            Debug.LogFormat("TRYING TO PLAY VIDEO AD - {0}", GameController.Instance.UserData.MustShowVideoAdToUserAsap);
            if (!GameController.Instance.UserData.MustShowVideoAdToUserAsap) yield break;

            Debug.Log("PLAYING VIDEO AD");
            Future<bool> adResultFuture = new Future<bool>();
            PlayVideoAd(adResultFuture);

            while (!adResultFuture.IsFinished) yield return null;

            if (adResultFuture.Value) SaveVideosWatchedUserData();

            Debug.Log("FINISHED PLAYING VIDEO AD");
            yield return null;
        }

private void PlayVideoAd(Future<bool> adResultFuture)
        {
            if (Application.isEditor)
            {
                Debug.LogWarning("PLAYING MOCK VIDEO AD!!!");
                EvtVideoAdPlayed?.Invoke();
                adResultFuture.Value = true;
                return;
            }

            TryInit();

            if (_adResultFuture != null) Debug.LogWarning("[AdController] Trying to play video ad but there is already an Ad Result Future defined!");

            _adResultFuture = adResultFuture;

            if (!_interstitialAd.IsLoaded())
            {
                Debug.Log("[AdController] Video Not Available");
                
                adResultFuture.Value = false;
                return;
            }

            Debug.LogFormat("[AdController] Playing Video Ad");
            _interstitialAd.OnAdClosed += (sender, args) =>
            {
                Debug.LogFormat("[AdController] Ad Finished Playing");
                adResultFuture.Value = true;
            };
            _interstitialAd.Show();
        }


Exception Stack.txt

mobileadssdk...@google.com

unread,
May 7, 2019, 2:39:07 AM5/7/19
to Leonardo Bilck, Google Mobile Ads SDK Developers
Hi,

Thank you for reporting this.

I already checked this with the team and this crash can also be triggered by publishers attempting to present SDK VCs on their own. Any publishers doing this need to update their own apps, as there's not much we can do to guard against publishers using our internals.

That said, I suggest to check our sample app implementation in reference, to avoid this crash.

Regards,
Teejay Pimentel
Mobile Ads SDK Team

--

---
You received this message because you are subscribed to the Google Groups "Google Mobile Ads SDK Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-admob-ads-sdk+unsub...@googlegroups.com.
To post to this group, send email to google-admob-ads-sdk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-admob-ads-sdk/5998d37d-45a0-4159-9acf-57fe82c2b19f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Leonardo Bilck

unread,
May 7, 2019, 4:11:01 PM5/7/19
to google-adm...@googlegroups.com
I have managed to fix/workaround this issue.

How? 
  1. Just before displaying an Interstitial Ad I force Unity's orientation to AutoRotation;
  2. Since Unity doesn't have a callback for orientation change AND doesn't set the current orientation to "AutoRotation" I wait an arbitrary time (1 sec) to make sure that the rotation properties are properly set;
  3. Show Ad (that is already loaded)
The main issue in this solution is that the orientation could be set/enforced by the Google Mobile Ads plugin, which in turn could properly handle the orientation changes.

This is the workaround code that I modified:

public IEnumerator TryPlayVideoAdCoRoutine()
        {
            if (!CanShowAds) yield break;

#if UNITY_EDITOR || DEVELOPMENT_BUILD
            Debug.LogFormat("[AdsController] TRYING TO PLAY VIDEO AD - {0}", GameController.Instance.UserData.MustShowVideoAdToUserAsap);
#endif
            if (!GameController.Instance.UserData.MustShowVideoAdToUserAsap) yield break;

            
#if UNITY_EDITOR || DEVELOPMENT_BUILD
            Debug.LogFormat("[AdsController] Changing Screen Orientation to {0}", ScreenOrientation.AutoRotation);
#endif
            var backupOrientation = Screen.orientation;
            Screen.orientation = ScreenOrientation.AutoRotation;
            
            yield return null;

            const float orientationChangeTimeout = 1f;

            float nextTimeout = Time.unscaledTime + orientationChangeTimeout;
            while(Screen.orientation != ScreenOrientation.AutoRotation && Time.unscaledTime < nextTimeout)
                yield return null;
            
#if UNITY_EDITOR || DEVELOPMENT_BUILD
            Debug.LogFormat("[AdsController] Screen Orientation Changed to {0}", Screen.orientation);
#endif
            
            Debug.Log("[AdsController] PLAYING VIDEO AD");
            Future<bool> adResultFuture = new Future<bool>();
            PlayVideoAd(adResultFuture);

            while (!adResultFuture.IsFinished) yield return null;
            
            Screen.orientation = backupOrientation;
            yield return null;
            nextTimeout = Time.unscaledTime + orientationChangeTimeout;
            while(Screen.orientation != backupOrientation && Time.unscaledTime < nextTimeout)
                yield return null;
            
#if UNITY_EDITOR || DEVELOPMENT_BUILD
            Debug.LogFormat("[AdsController] Screen Orientation Changed BACK to {0}", Screen.orientation);
#endif
            
            if (adResultFuture.Value) SaveVideosWatchedUserData();

            Debug.Log("[AdsController] FINISHED PLAYING VIDEO AD");
            yield return null;
        }

If anyone is interested I have attached my AdsController class in this post. Obviously it won't work out of the box since it contains some references to other classes in the project.
AdsController.cs

mobileadssdk...@google.com

unread,
May 8, 2019, 1:32:07 AM5/8/19
to Leonardo Bilck, Google Mobile Ads SDK Developers
Hi,

Thank you for getting back to us and providing a workaround for this. 

If you encounter any other issue, please let us know. We will be happy to assist you.

Regards,
Teejay Pimentel
Mobile Ads SDK Team

            yield return null;

            Screen.orientation = backupOrientation;

            while(Screen.orientation != backupOrientation) yield return null;
            yield return null;
            
            StartPlayRandomVideoCoroutine(_playingMultipleVideos);
            _controlsPlaybackGroup.PerformTransitionControlsGroup(VideoPlayerControlState.Inactive);
        }

public IEnumerator TryPlayVideoAdCoRoutine()
        {
            if (!CanShowAds) yield break;

            Debug.LogFormat("TRYING TO PLAY VIDEO AD - {0}", GameController.Instance.UserData.MustShowVideoAdToUserAsap);
            if (!GameController.Instance.UserData.MustShowVideoAdToUserAsap) yield break;

            Debug.Log("PLAYING VIDEO AD");
            Future<bool> adResultFuture = new Future<bool>();
            PlayVideoAd(adResultFuture);

            while (!adResultFuture.IsFinished) yield return null;

            if (adResultFuture.Value) SaveVideosWatchedUserData();

            Debug.Log("FINISHED PLAYING VIDEO AD");
            yield return null;
        }

private void PlayVideoAd(Future<bool> adResultFuture)
        {
            if (Application.isEditor)
            {
                Debug.LogWarning("PLAYING MOCK VIDEO AD!!!");
                EvtVideoAdPlayed?.Invoke();
                adResultFuture.Value = true;
                return;
            }

            TryInit();

            if (_adResultFuture != null) Debug.LogWarning("[AdController] Trying to play video ad but there is already an Ad Result Future defined!");

            _adResultFuture = adResultFuture;

            if (!_interstitialAd.IsLoaded())
            {
                Debug.Log("[AdController] Video Not Available");
                
                adResultFuture.Value = false;
                return;
            }

            Debug.LogFormat("[AdController] Playing Video Ad");
            _interstitialAd.OnAdClosed += (sender, args) =>
            {
                Debug.LogFormat("[AdController] Ad Finished Playing");
                adResultFuture.Value = true;
            };
            _interstitialAd.Show();
        }

--

---
You received this message because you are subscribed to the Google Groups "Google Mobile Ads SDK Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-admob-ads-sdk+unsubscrib...@googlegroups.com.

--

---
You received this message because you are subscribed to the Google Groups "Google Mobile Ads SDK Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-admob-ads-sdk+unsub...@googlegroups.com.
To post to this group, send email to google-admob-ads-sdk@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages