1

我已经使用 cocoapods(pod 'GoogleAds-IMA-iOS-SDK','~> 3.2.1')将 Google IMA sdk 集成到我的项目中。我有一个名为 VideoPresenter 的单独类来与 sdk 交互。这个演示者是一个名为 VideoCell 的集合视图单元的属性,它使用 AVPlayer 播放一些视频内容(我正在尝试将预卷视频添加到此视频内容中)。广告加载程序总是失败并显示错误消息“IMA SDK 加载超时”。我该如何解决?以下是presenter的代码:

#import "VideoAdPresenter.h"
#import <GoogleInteractiveMediaAds/GoogleInteractiveMediaAds.h>

@interface VideoAdPresenter () <IMAAdsLoaderDelegate,      IMAAdsManagerDelegate>
@property (nonatomic, strong) IMAAVPlayerContentPlayhead *contentPlayhead;
@property (nonatomic, strong) IMAAdsLoader *adsLoader;
@property (nonatomic, strong) IMAAdsManager *adsManager;

@end

NSString *const kTestAppAdTagUrl =
@"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%3Dlinear&correlator=";


@implementation VideoAdPresenter

- (id)initWithAvPlayer:(AVPlayer *)anAvPlayer
{
    self = [super init];
    if(self) {
        self.contentPlayhead = [[IMAAVPlayerContentPlayhead alloc]initWithAVPlayer:anAvPlayer];
        [self setupAdsLoader];
    }
    return self;
}

- (void)setupAdsLoader
{
   self.adsLoader = [[IMAAdsLoader alloc] initWithSettings:nil];
   self.adsLoader.delegate = self;
}

- (void)requestAds {
   if(!self.videoAdTagUrl || [self.videoAdTagUrl isEqualToString:@""]) {
    NSLog(@"Nil or empty video ad tag url (%@) passed to video ad presenter: %@", self.videoAdTagUrl, [self description]);
    return;
}
    IMAAdDisplayContainer *adDisplayContainer =      [[IMAAdDisplayContainer alloc] initWithAdContainer:self.adContainerView companionSlots:nil];
    IMAAdsRequest *request = [[IMAAdsRequest alloc]initWithAdTagUrl:kTestAppAdTagUrl adDisplayContainer:adDisplayContainer contentPlayhead:self.contentPlayhead userContext:nil];

    [self.adsLoader requestAdsWithRequest:request];
}

#pragma mark - ads loader delegate

- (void)adsLoader:(IMAAdsLoader *)loader adsLoadedWithData: (IMAAdsLoadedData *)adsLoadedData {
    // Grab the instance of the IMAAdsManager and set ourselves as the delegate.
    self.adsManager = adsLoadedData.adsManager;
    self.adsManager.delegate = self;

    IMAAdsRenderingSettings *adsRenderingSettings =  [[IMAAdsRenderingSettings alloc] init];
    adsRenderingSettings.webOpenerPresentingController = self.presentingController;

    [self.adsManager    initializeWithAdsRenderingSettings:adsRenderingSettings];
}

- (void)adsLoader:(IMAAdsLoader *)loader failedWithErrorData: (IMAAdLoadingErrorData *)adErrorData {
    // Something went wrong loading ads. Log the error and play the   content.
    NSLog(@"Error loading ads: %@", adErrorData.adError.message);
    if(self.delegate && [self.delegate respondsToSelector:@selector(videoAdPresenterRequestedContentResume:)]) {
        [self.delegate videoAdPresenterRequestedContentResume:self];
    }
}




#pragma mark - ads manager delegate
- (void)adsManager:(IMAAdsManager *)adsManager didReceiveAdEvent: (IMAAdEvent *)event {
    // When the SDK notified us that ads have been loaded, play them.
    if (event.type == kIMAAdEvent_LOADED) {
        [adsManager start];
    }
}

- (void)adsManager:(IMAAdsManager *)adsManager didReceiveAdError:(IMAAdError *)error {
    // Something went wrong with the ads manager after ads were loaded. Log the error and play the
    // content.
    NSLog(@"AdsManager error: %@", error.message);
    if(self.delegate && [self.delegate respondsToSelector:@selector(videoAdPresenterRequestedContentResume:)]) {
        [self.delegate videoAdPresenterRequestedContentResume:self];
    }
}

- (void)adsManagerDidRequestContentPause:(IMAAdsManager *)adsManager   {
    // The SDK is going to play ads, so pause the content.
    if(self.delegate && [self.delegate respondsToSelector:@selector(videoAdPresenterRequestedContentPause:)]) {
        [self.delegate videoAdPresenterRequestedContentPause:self];
    }
}

- (void)adsManagerDidRequestContentResume:(IMAAdsManager *)adsManager {
    // The SDK is done playing ads (at least for now), so resume the content.
    if(self.delegate && [self.delegate respondsToSelector:@selector(videoAdPresenterRequestedContentResume:)]) {
        [self.delegate videoAdPresenterRequestedContentResume:self];
    }
}
@end
4

1 回答 1

1

原因是演示者是从后台线程调用的。从主线程做同样的事情解决了这个问题。

于 2016-09-13T18:31:15.557 回答