Android上的IMA SDK问题

时间:2019-03-07 01:24:59

标签: android imasdk

我已将IMA SDK添加到我的项目中以播放VAST广告。我发现了一个奇怪的问题-在会话onAdError(AdErrorEvent adErrorEvent)onAdEvent(AdEvent adEvent)的10-15分钟后没有被调用。因此,如果我运行我的应用程序并从列表中选择一个视频文件,那么我会看到广告,并且在广告制作完成后开始播放视频。在这种情况下,onAdEvent(AdEvent adEvent)被称为(ALL_ADS_COMPLETED)。如果我选择下一个视频,服务器将返回空的VAST XML,因此将调用onAdError(AdErrorEvent adErrorEvent),并启动另一个视频。 10-15分钟后,如果我从列表中选择一个视频,则两个事件均未调用,并且没有任何反应,因为我的播放器正在从这些事件的列表中开始播放视频。

BasicExample 项目中的所有代码。

源代码:

        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);

    }

依赖项:

    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 个答案:

没有答案