IMA SDK Issue on Android

1.6k Views Asked by At

I added IMA SDK to my project for playing VAST ads. I found a strange problem - after 10-15 minutes of session onAdError(AdErrorEvent adErrorEvent) and onAdEvent(AdEvent adEvent) are not called. So if I run my app and select a video file from the list, I see ad and when ad is finished video is started. In this case onAdEvent(AdEvent adEvent) is called (ALL_ADS_COMPLETED). If I select next video, server returns empty VAST XML, so onAdError(AdErrorEvent adErrorEvent) is called, and another video is started. After 10-15 minutes if I select a video from the list, both events not called, and nothing happens, because my player is starting to play video from the list from these events.

All code from BasicExample project.

Source code:

        OnCreate() {
             mSdkFactory = ImaSdkFactory.getInstance();
             mAdsLoader = mSdkFactory.createAdsLoader(this);
             mAdUiContainer = (ViewGroup) findViewById(R.id.videoPlayerWithAdPlayback);

             mAdsLoader.addAdErrorListener(this);
             mAdsLoader.addAdsLoadedListener(new AdsLoadedListener() {
             @Override
             public void onAdsManagerLoaded(AdsManagerLoadedEvent adsManagerLoadedEvent) {
             // Ads were successfully loaded, so get the AdsManager instance. AdsManager has
             // events for ad playback and errors.
                mAdsManager = adsManagerLoadedEvent.getAdsManager();

             // Attach event and error event listeners.
                mAdsManager.addAdErrorListener(MainActivity.this);
                mAdsManager.addAdEventListener(MainActivity.this);
                mAdsManager.init();
              }
             });
            }

            @Override
            public void onAdEvent(AdEvent adEvent) {
            Log.i(LOGTAG, "Event: " + adEvent.getType());

            // These are the suggested event types to handle. For full list of all ad event
           // types, see the documentation for AdEvent.AdEventType.
           switch (adEvent.getType()) {
                case LOADED:
                                    // AdEventType.LOADED will be fired when ads are ready to be played.
                                    // AdsManager.start() begins ad playback. This method is ignored for VMAP or
                                    // ad rules playlists, as the SDK will automatically start executing the
                                    // playlist.
                   mAdsManager.start();
                   break;
                case CONTENT_PAUSE_REQUESTED:
                                    // AdEventType.CONTENT_PAUSE_REQUESTED is fired immediately before a video
                                    // ad is played.
                   mIsAdDisplayed = true;
                   mVideoPlayer.pause();
                   break;
                case CONTENT_RESUME_REQUESTED:
                                    // AdEventType.CONTENT_RESUME_REQUESTED is fired when the ad is completed
                                    // and you should start playing your content.
                   mIsAdDisplayed = false;
                   mVideoPlayer.play();
                   break;
                case ALL_ADS_COMPLETED:
                   mIsAdDisplayed = true;
                   if (mAdsManager != null) {
                         mAdsManager.destroy();
                         mAdsManager = null;
                   }
                   mVideoPlayer.play();
                   break;
                   default:
                   break;
                       }
                   }

        @Override
        public void onAdError(AdErrorEvent adErrorEvent) {
            Log.e(LOGTAG, "Ad Error: " + adErrorEvent.getError().getMessage());
                            mVideoPlayer.play();
                        }

        private void requestAds(String adTagUrl) {

             AdDisplayContainer adDisplayContainer = mSdkFactory.createAdDisplayContainer();
             adDisplayContainer.setAdContainer(mAdUiContainer);

             // Create the ads request.
             AdsRequest request = mSdkFactory.createAdsRequest();
             request.setAdTagUrl(adTagUrl);
             appendLog("VAST Tag: " + adTagUrl);
             request.setAdDisplayContainer(adDisplayContainer);
             request.setContentProgressProvider(new ContentProgressProvider() {
             @Override
             public VideoProgressUpdate getContentProgress() {
                  if (mIsVastAdDisplayed || exoPlayer == null || exoPlayer.getDuration() <= 0) {
                  return VideoProgressUpdate.VIDEO_TIME_NOT_READY;
                                }
                  return new VideoProgressUpdate(exoPlayer.getCurrentPosition(),
                                        exoPlayer.getDuration());
                            }
                        });

                        // Request the ad. After the ad is loaded, onAdsManagerLoaded() will be called.
                  mAdsLoader.requestAds(request);

    }

Dependencies:

    implementation 'com.google.ads.interactivemedia.v3:interactivemedia:3.10.6'
    implementation 'com.google.android.gms:play-services-ads-identifier:16.0.0'
<string name="ad_tag_url"><![CDATA[https://pubads.g.doubleclick.net/gampad/ads?sz=640x480&iu=/124319096/external/single_ad_samples&ciu_szs=300x250&impl=s&gdfp_req=1&env=vp&output=vast&unviewed_position_start=1&cust_params=deployment%3Ddevsite%26sample_ct%3Dskippablelinear&correlator=]]></string>
0

There are 0 best solutions below