Bạn có thể sử dụng SDK IMA để 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. Phân tách các yêu cầu này để đảm bảo rằng tất cả người xem đều không 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.
Để trợ giúp việc này, SDK IMA có AdsRequest.liveStreamPrefetchSeconds
thuộc tính này. Thuộc tính này chỉ định số giây tối đa cho SDK
sẽ đợi trước khi liên lạc 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. Cho
ví dụ: nếu bạn đặt AdsRequest.liveStreamPrefetchSeconds
thành 30, SDK
sẽ đợi từ 0 đến 30 giây sau khi bạn gọi AdsLoader.requestAds()
để thực sự
thực hiện yêu cầu đến máy chủ.
Tính năng tìm nạp trước trong 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 sau khi hoàn tất điểm chèn quảng cáo. Điều này đảm bảo khoảng thời gian tối đa sẵn có 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 cửa sổ tìm nạp trước là 290 giây (5 phút trừ 10 giây, để đảm bảo yêu cầu được gửi vào cuối cửa sổ 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
...
}
}