Bạn có thể sử dụng IMA SDK để kiếm tiền từ sự kiện phát trực tiếp cũng như video theo yêu cầu. Đối với sự kiện phát trực tiếp, bạn cần tạo một yêu cầu quảng cáo mới cho mỗi điểm chèn quảng cáo. Hãy phân bổ các yêu cầu này để đảm bảo rằng không phải tất cả người xem đều yêu cầu quảng cáo cùng một lúc và làm chậm (các) máy chủ quảng cáo.
Để hỗ trợ việc này, IMA SDK có thuộc tính AdsRequest.liveStreamPrefetchSeconds. Thuộc tính này chỉ định số giây tối đa mà SDK phải đợi trước khi liên hệ với máy chủ quảng cáo sau khi bạn gọi AdsLoader.requestAds(). Thời gian yêu cầu thực tế sẽ được chọn ngẫu nhiên. Ví dụ: nếu bạn đặt AdsRequest.liveStreamPrefetchSeconds thành 30, thì SDK sẽ đợi từ 0 đến 30 giây sau khi bạn gọi AdsLoader.requestAds() để thực sự gửi yêu cầu đến máy chủ.
Tìm nạp trước sự kiện phát trực tiếp trong thực tế
Bạn nên tìm nạp trước điểm chèn quảng cáo tiếp theo ngay khi điểm chèn quảng cáo hiện tại kết thúc. Điều này đảm bảo khoảng thời gian tối đa cho cửa sổ tìm nạp trước của bạn. Giả sử bạn có 5 phút giữa các điểm chèn quảng cáo. Khi một điểm chèn quảng cáo hoàn tất, bạn có thể yêu cầu điểm chèn quảng cáo tiếp theo với khoảng thời gian tìm nạp trước là 290 giây (5 phút trừ đi 10 giây, để đảm bảo các yêu cầu được gửi vào cuối khoảng thời gian tìm nạp trước có đủ thời gian để giải quyết):
Objective-C
- (void)adsManager:(IMAAdsManager *)adsManager didReceiveAdEvent:(IMAAdEvent *)event {
...
switch (event.type) {
...
case kIMAAdEvent_ALL_ADS_COMPLETED:
IMAAdsRequest *request = [[IMAAdsRequest alloc]
initWithAdTagUrl: self.adTagUrl
adDisplayContainer: self.adDisplayContainer
avPlayerVideoDisplay: self.avPlayerVideoDisplay
pictureInPictureProxy: self.pictureInPictureProxy
userContext: nil];
// set a delay between the end of the last ad
// in the last request, and the first ad from
// the new request
Float64 adGap = 30;
// make sure the request occurs at least five
// seconds before starting the new set of ads
request.liveStreamPrefetchSeconds = adGap - 5;
[self.adsLoader requestAdsWithRequest:request];
// start new ads after adGap seconds have elapsed
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, adGap * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
[adsManager start];
});
break;
...
}
...
}
Swift
func adsManager(_ adsManager: IMAAdsManager!, didReceive event: IMAAdEvent!) {
switch event.type {
...
case IMAAdEventType.ALL_ADS_COMPLETED:
let request = IMAAdsRequest(
adTagUrl: AdTagUrl,
adDisplayContainer: adDisplayContainer,
contentPlayhead: contentPlayhead,
userContext: nil)
// set a delay between the end of the last ad
// in the last request, and the first ad from
// the new request
let adGap = 30
// make sure the request occurs at least five
// seconds before starting the new set of ads
request.liveStreamPrefetchSeconds = adGap - 5
adsLoader.requestAds(with: request)
// start new ads after adGap seconds have elapsed
DispatchQueue.main.asyncAfter(deadline: .now() + adGap) {
adsManager.start()
}
break
...
}
}