我已使用cocoapods(pod'GoogleAds-IMA-iOS-SDK','〜> 3.2.1')将Google IMA sdk集成到我的项目中。我有一个名为VideoPresenter的独立类与sdk进行交互。此演示者是名为VideoCell的collectionview单元的属性,它使用AVPlayer播放一些视频内容(我正在尝试将预卷视频添加到此视频内容中)。广告加载程序始终失败,并显示错误消息“IMA SDK加载超时”。我该如何解决?以下是演示者的代码:
#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
答案 0 :(得分:0)
原因是演示者是从后台线程调用的。从主线程做同样的事情解决了这个问题。