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