إعداد حزمة تطوير البرامج لإعلانات الوسائط التفاعلية لعرض الإعلانات أثناء التشغيل

تسهِّل حِزم تطوير البرامج (SDK) لإعلانات الوسائط التفاعلية دمج إعلانات الوسائط المتعددة في مواقعك الإلكترونية وتطبيقاتك. يمكن لحِزم تطوير البرامج لإعلانات الوسائط التفاعلية طلب الإعلانات من أيّ خادم إعلانات متوافق مع نموذج عرض إعلانات الفيديو (VAST) وإدارة تشغيل الإعلانات في تطبيقاتك. باستخدام حِزم تطوير البرامج لميزة "إدراج إعلان ديناميكي" في إعلانات الوسائط التفاعلية (IMA DAI SDK)، تقدّم التطبيقات طلبًا لبث الإعلان والفيديو، سواء كان محتوى فيديو عند الطلب أو محتوى بث مباشر. بعد ذلك، تُرسِل حزمة تطوير البرامج (SDK) بثًا مدمجًا للفيديو، ما يُغنيك عن إدارة التبديل بين الإعلانات والفيديوهات التي تعرض محتوى داخل تطبيقك.

اختيار حلّ DAI الذي يهمّك

إدراج إعلان ديناميكي في مجموعات الإعلانات

يوضّح هذا الدليل كيفية دمج حزمة تطوير البرامج لتكنولوجيا "إدراج الإعلان الديناميكي" في إعلانات الوسائط التفاعلية في تطبيق مشغّل فيديو بسيط. إذا أردت الاطّلاع على نموذج مكتمل للدمج أو اتّباعه، نزِّل PodServingExample من GitHub.

نظرة عامة على ميزة "إدراج إعلان ديناميكي" في إعلانات الوسائط التفاعلية

يتضمّن تنفيذ ميزة "الإعلانات الديناميكية أثناء عرض الفيديو" في IMA أربعة مكوّنات رئيسية لحزمة تطوير البرامج (SDK) كما هو موضّح في هذا الدليل:

  • IMAAdDisplayContainer: عنصر حاوية يقع أعلى عنصر تشغيل الفيديو ويضم عناصر واجهة مستخدم الإعلان.
  • IMAAdsLoader – عنصر يطلب أحداث البث ويعالج الأحداث التي تسبّبها عناصر استجابة طلب البث. يجب إنشاء مثيل واحد فقط لتحميل الإعلانات، والذي يمكن إعادة استخدامه طوال فترة استخدام التطبيق.
  • IMAStreamRequest – إما IMAPodVODStreamRequest أو IMAPodStreamRequest.
  • IMAStreamManager – عنصر يعالج أحداث إدراج الإعلانات الديناميكية وتفاعلاتها مع الخلفية في ميزة "إدراج الإعلانات الديناميكية". يتعامل مدير البث أيضًا مع إشعارات التتبّع ويعيد توجيه أحداث البث والإعلانات إلى الناشر.

بالإضافة إلى ذلك، لتشغيل أحداث البث التي تتضمن مجموعة من المحتوى، يجب تنفيذ معالج VTP مخصّص. يُرسِل معالِج VTP المخصّص رقم تعريف البث إلى شريكك التقني في الفيديو (VTP) مع أي معلومات أخرى يحتاج إليها لعرض بيان بث يتضمّن كلّ من المحتوى والإعلانات المُدمَجة. سيقدّم لك مزوّد خدمة VTP تعليمات حول كيفية تنفيذ معالِج VTP المخصّص.

المتطلبات الأساسية

قبل البدء، يجب توفّر ما يلي:

ستحتاج أيضًا إلى المَعلمات المستخدَمة لطلب البث من حزمة تطوير البرامج لإعلانات الوسائط التفاعلية.

مَعلمات البث المباشر
رمز الشبكة رمز الشبكة لحسابك على "مدير إعلانات 360".
مثال: 51636543
مفتاح مادة العرض المخصّصة مفتاح مادة العرض المخصّصة الذي يحدّد حدث عرض المجموعة في "مدير الإعلانات 360". يمكن أن يتم إنشاء هذا الجدول من قِبل جهة خارجية تُعدّ البيان أو شريك عرض مجموعات المحتوى التابع لجهة خارجية.
مثال: google-sample
مَعلمات بث الفيديوهات عند الطلب
رمز الشبكة رمز الشبكة لحسابك على "مدير إعلانات 360".
مثال: 51636543

إنشاء مشروع Xcode جديد

في Xcode، أنشئ مشروعًا جديدًا لنظام التشغيل iOS باستخدام Objective-C باسم "PodServingExample".

إضافة حزمة تطوير البرامج لإعلانات الوسائط التفاعلية (IMA DAI SDK) إلى مشروع Xcode

استخدِم إحدى الطريقتَين التاليتَين لتثبيت حزمة تطوير البرامج (SDK) لميزة "عرض الإعلانات أثناء التشغيل" في IMA.

تثبيت حزمة تطوير البرامج (SDK) باستخدام CocoaPods (الخيار المفضّل)

‫CocoaPods هو أداة لإدارة الملحقات لمشاريع Xcode، وهو الطريقة المُقترَحة لتثبيت حزمة تطوير البرامج لإعلانات الوسائط التفاعلية (IMA DAI SDK). لمزيد من المعلومات حول تثبيت CocoaPods أو استخدامه، يُرجى الاطّلاع على مستندات CocoaPods. بعد تثبيت CocoaPods، اتّبِع التعليمات التالية لتثبيت حزمة تطوير البرامج لإعلانات الوسائط التفاعلية (IMA DAI SDK):

  1. في الدليل نفسه الذي يتضمّن ملف PodServingExample.xcodeproj، أنشِئ ملفًا متنصًّا يًا باسم Podfile وأضِف الإعدادات التالية:

    source 'https://github.com/CocoaPods/Specs.git'
    
    platform :ios, '14'
    
    target 'PodServingExample' do
      pod 'GoogleAds-IMA-iOS-SDK'
    end
    

  2. من الدليل الذي يحتوي على Podfile، شغِّل:

    pod install --repo-update

تثبيت حزمة تطوير البرامج (SDK) باستخدام Swift Package Manager

تتوافق حزمة تطوير البرامج لإعلانات الوسائط التفاعلية مع Swift Package Manager بدءًا من الإصدار 3.18.4. اتّبِع الخطوات التالية لاستيراد حزمة Swift.

  1. في Xcode، ثبِّت حزمة Swift الخاصة بحزمة تطوير البرامج (SDK) لميزة "الإعلانات الديناميكية أثناء عرض الفيديو" من IMA من خلال الانتقال إلى ملف > إضافة حِزم.

  2. في الطلب الذي يظهر، ابحث عن حزمة Swift لـ IMA DAI SDK في مستودع GitHub:

    https://github.com/googleads/swift-package-manager-google-interactive-media-ads-ios
    
  3. اختَر إصدار حزمة Swift لـ IMA DAI SDK التي تريد استخدامها. بالنسبة إلى المشاريع الجديدة، ننصحك باستخدام الإصدار التالي.

عند الانتهاء، يحلّ Xcode تبعيات الحزمة وينزّلها في الخلفية. لمزيد من التفاصيل حول كيفية إضافة التبعيات للحِزم، يُرجى الاطّلاع على مقالة Apple.

تنزيل حزمة SDK وتثبيتها يدويًا

إذا كنت لا تريد استخدام Swift Package Manager أو CocoaPods، يمكنك تنزيل حزمة تطوير البرامج لـ IMA DAI SDK وإضافتها يدويًا إلى مشروعك.

  1. من صفحة تنزيل حزمة تطوير البرامج لإعلانات الوسائط التفاعلية لنظام التشغيل iOS، نزِّل أحدث إصدار من حزمة تطوير البرامج لإدراج إعلان ديناميكي لإعلانات الوسائط التفاعلية واستخرِجه.
  2. افتح BasicExample.xcodeproj.
  3. في اللوحة اليمنى، انقر على اسم المشروع.
    انقر على اسم المشروع في اللوحة اليمنى.
  4. في اللوحة الوسطى، انقر على مراحل الإنشاء.
    انقر على "مراحل الإنشاء" في اللوحة الوسطى.
  5. وسِّع قسم ربط الثنائي بالمكتبات.
  6. في أسفل قائمة المكتبات، انقر على رمز علامة الجمع [+].
  7. انقر على إضافة غير ذلك.
  8. في الدليل الذي استخرجت منه حزمة تطوير البرامج (SDK) التي تم تنزيلها، اختَر GoogleInteractiveMediaAds.framework وانقر على فتح.
  9. في أسفل قائمة المكتبات، انقر على رمز الإضافة [+] مرة أخرى.
  10. في عمود الحالة، تأكَّد من ضبط قيمة GoogleInteractiveMediaAds.framework على Required.
  11. أدرِج علامة الربط -ObjC في إعدادات الإنشاء. للحصول على المزيد من المعلومات، يُرجى الاطّلاع على Apple QA1490.

إنشاء مشغّل فيديو بسيط

نفِّذ مشغّل فيديو في أداة التحكّم في العرض الرئيسية، باستخدام مشغّل وسائط صوتية ومرئية مُدرَج في عرض واجهة مستخدم. تستخدِم حزمة تطوير البرامج لإعلانات الوسائط التفاعلية عرض واجهة المستخدِم لعرض عناصر واجهة المستخدِم للإعلان.

Objective-CSwift
#import "ViewController.h"

#import <AVKit/AVKit.h>

/// Content URL.
static NSString *const kBackupContentUrl =
    @"http://devimages.apple.com/iphone/samples/bipbop/bipbopall.m3u8";

@interface ViewController ()
/// Play button.
@property(nonatomic, weak) IBOutlet UIButton *playButton;

@property(nonatomic, weak) IBOutlet UIView *videoView;
/// Video player.
@property(nonatomic, strong) AVPlayer *videoPlayer;
@end

@implementation ViewController

- (void)viewDidLoad {
  [super viewDidLoad];
  self.view.backgroundColor = [UIColor blackColor];

  // Load AVPlayer with the path to your content.
  NSURL *contentURL = [NSURL URLWithString:kBackupContentUrl];
  self.videoPlayer = [AVPlayer playerWithURL:contentURL];

  // Create a player layer for the player.
  AVPlayerLayer *playerLayer = [AVPlayerLayer playerLayerWithPlayer:self.videoPlayer];

  // Size, position, and display the AVPlayer.
  playerLayer.frame = self.videoView.layer.bounds;
  [self.videoView.layer addSublayer:playerLayer];
}

- (IBAction)onPlayButtonTouch:(id)sender {
  [self.videoPlayer play];
  self.playButton.hidden = YES;
}

@end
// Copyright 2024 Google LLC. All rights reserved.
//
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this
// file except in compliance with the License. You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software distributed under
// the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
// ANY KIND, either express or implied. See the License for the specific language governing
// permissions and limitations under the License.

import AVFoundation
import UIKit

class ViewController: UIViewController {

  /// Content URL.
  static let backupStreamURLString =
    "http://devimages.apple.com/iphone/samples/bipbop/bipbopall.m3u8"

  /// Play button.
  @IBOutlet private weak var playButton: UIButton!

  @IBOutlet private weak var videoView: UIView!
  /// Video player.
  private var videoPlayer: AVPlayer?

  override func viewDidLoad() {
    super.viewDidLoad()

    playButton.layer.zPosition = CGFloat(MAXFLOAT)

    // Load AVPlayer with path to our content.
    // note: this unwrap is safe because the URL is a constant string.
    let contentURL = URL(string: ViewController.backupStreamURLString)!
    videoPlayer = AVPlayer(url: contentURL)

    // Create a player layer for the player.
    let playerLayer = AVPlayerLayer(player: videoPlayer)

    // Size, position, and display the AVPlayer.
    playerLayer.frame = videoView.layer.bounds
    videoView.layer.addSublayer(playerLayer)
  }

  @IBAction func onPlayButtonTouch(_ sender: Any) {
    videoPlayer?.play()
    playButton.isHidden = true
  }
}

بدء تحميل الإعلانات

استورِد حزمة تطوير البرامج (SDK) لـ IMA في أداة التحكّم في العرض واستخدِم بروتوكولَي IMAAdsLoaderDelegate و IMAStreamManagerDelegate لمعالجة أحداث أداة تحميل الإعلانات ومدير البث.

أضِف السمات الخاصة التالية لتخزين مكوّنات أداة تطوير البرامج لإعلانات الوسائط التفاعلية (IMA SDK) الرئيسية:

  • IMAAdsLoader: تدير طلبات البث طوال مدة تطبيقك.
  • IMAAdDisplayContainer: تُستخدَم لإدارة إدراج عناصر واجهة مستخدِم الإعلان وإدارتها.
  • IMAAVPlayerVideoDisplay: تُستخدم للتواصل بين حزمة تطوير البرامج لإعلانات الوسائط التفاعلية ومشغل الوسائط، كما تعالج metadata المبرمَجة.
  • IMAStreamManager: تدير تشغيل البث وتنشئ الأحداث ذات الصلة بالإعلانات.

يمكنك بدء تشغيل أداة تحميل الإعلانات وحاوية عرض الإعلانات وعرض الفيديو بعد تحميل المشهد.

Objective-CSwift

@import GoogleInteractiveMediaAds;

// ...

@interface ViewController () <IMAAdsLoaderDelegate, IMAStreamManagerDelegate>
/// The entry point for the IMA DAI SDK to make DAI stream requests.
@property(nonatomic, strong) IMAAdsLoader *adsLoader;
/// The container where the SDK renders each ad's user interface elements and companion slots.
@property(nonatomic, strong) IMAAdDisplayContainer *adDisplayContainer;
/// The reference of your video player for the IMA DAI SDK to monitor playback and handle timed
/// metadata.
@property(nonatomic, strong) IMAAVPlayerVideoDisplay *imaVideoDisplay;
/// References the stream manager from the IMA DAI SDK after successful stream loading.
@property(nonatomic, strong) IMAStreamManager *streamManager;

// ...

@end

@implementation ViewController

- (void)viewDidLoad {
  [super viewDidLoad];

  // ...

  self.adsLoader = [[IMAAdsLoader alloc] initWithSettings:nil];
  self.adsLoader.delegate = self;

  // Create an ad display container for rendering each ad's user interface elements and companion
  // slots.
  self.adDisplayContainer =
      [[IMAAdDisplayContainer alloc] initWithAdContainer:self.videoView
                                          viewController:self
                                          companionSlots:nil];

  // Create an IMAAVPlayerVideoDisplay to give the SDK access to your video player.
  self.imaVideoDisplay = [[IMAAVPlayerVideoDisplay alloc] initWithAVPlayer:self.videoPlayer];
}

import GoogleInteractiveMediaAds
// ...

class ViewController: UIViewController, IMAAdsLoaderDelegate, IMAStreamManagerDelegate {
  // ...

  /// The entry point for the IMA DAI SDK to make DAI stream requests.
  private var adsLoader: IMAAdsLoader?
  /// The container where the SDK renders each ad's user interface elements and companion slots.
  private var adDisplayContainer: IMAAdDisplayContainer?
  /// The reference of your video player for the IMA DAI SDK to monitor playback and handle timed
  /// metadata.
  private var imaVideoDisplay: IMAAVPlayerVideoDisplay!
  /// References the stream manager from the IMA DAI SDK after successfully loading the DAI stream.
  private var streamManager: IMAStreamManager?

  // ...

  override func viewDidLoad() {
    super.viewDidLoad()

    // ...

    adsLoader = IMAAdsLoader(settings: nil)
    adsLoader?.delegate = self

    // Create an ad display container for rendering each ad's user interface elements and companion
    // slots.
    adDisplayContainer = IMAAdDisplayContainer(
      adContainer: videoView,
      viewController: self,
      companionSlots: nil)

    // Create an IMAAVPlayerVideoDisplay to give the SDK access to your video player.
    imaVideoDisplay = IMAAVPlayerVideoDisplay(avPlayer: videoPlayer)
  }

تقديم طلب بث

عندما يضغط المستخدم على زر التشغيل، يمكنك تقديم طلب بث جديد. استخدِم فئة IMAPodStreamRequest للبث المباشر. بالنسبة إلى أحداث الفيديوهات المسجّلة، استخدِم فئة IMAPodVODStreamRequest.

يتطلّب طلب البث مَعلمات البث، بالإضافة إلى إشارة إلى حاوية عرض الإعلان وعرض الفيديو.

Objective-CSwift

- (IBAction)onPlayButtonTouch:(id)sender {
  [self requestStream];
  self.playButton.hidden = YES;
}

- (void)requestStream {
  // Create a stream request.
  IMAStreamRequest *request;
  if (kStreamType == StreamTypeLive) {
    // Live stream request. Replace the network code and custom asset key with your values.
    request = [[IMAPodStreamRequest alloc] initWithNetworkCode:kNetworkCode
                                                customAssetKey:kCustomAssetKey
                                            adDisplayContainer:adDisplayContainer
                                                  videoDisplay:self.videoDisplay
                                         pictureInPictureProxy:nil
                                                   userContext:nil];
  } else {
    // VOD request. Replace the network code with your value.
    request = [[IMAPodVODStreamRequest alloc] initWithNetworkCode:@kNetworkCode
                                               adDisplayContainer:adDisplayContainer
                                                     videoDisplay:self.videoDisplay
                                            pictureInPictureProxy:nil
                                                      userContext:nil];
  }
  [self.adsLoader requestStreamWithRequest:request];
}

@IBAction func onPlayButtonTouch(_ sender: Any) {
  requestStream()
  playButton.isHidden = true
}

func requestStream() {
  // Create a stream request. Use one of "Livestream request" or "VOD request".
  if ViewController.requestType == StreamType.live {
    // Livestream request.
    let request = IMAPodStreamRequest(
      networkCode: ViewController.networkCode,
      customAssetKey: ViewController.customAssetKey,
      adDisplayContainer: adDisplayContainer!,
      videoDisplay: self.imaVideoDisplay,
      pictureInPictureProxy: nil,
      userContext: nil)
    adsLoader?.requestStream(with: request)
  } else {
    // VOD stream request.
    let request = IMAPodVODStreamRequest(
      networkCode: ViewController.networkCode,
      adDisplayContainer: adDisplayContainer!,
      videoDisplay: self.imaVideoDisplay,
      pictureInPictureProxy: nil,
      userContext: nil)
    adsLoader?.requestStream(with: request)
  }
}

الاستماع إلى أحداث تحميل البث

تستدعي فئة IMAAdsLoader الاستدعاءات IMAAdsLoaderDelegate في حال نجاح عملية الإعداد أو تعذّر طلب البث.

في adsLoadedWithData طريقة التفويض، اضبط IMAStreamManagerDelegate. نقْل معرّف مصدر البيانات إلى معالِج VTP المخصّص، واسترجاع عنوان URL لبيان مصدر البيانات بالنسبة إلى أحداث البث المباشر، حمِّل عنوان URL للبيان في شاشة الفيديو، وابدأ التشغيل. بالنسبة إلى أحداث البث المباشر من الفيديوهات عند الطلب، يجب تمرير عنوان URL لملف البيان إلى loadThirdPartyStream طريقة مدير البث. تطلب هذه الطريقة بيانات أحداث الإعلانات من "مدير إعلانات Google‏ 360"، ثم تحمّل عنوان URL لملف البيان وتبدأ التشغيل.

في failedWithErrorData طريقة المفوَّض، سجِّل الخطأ. يمكنك تشغيل البث الاحتياطي إذا أردت. اطّلِع على أفضل الممارسات المتعلّقة بميزة "الذكاء الاصطناعي التحويلي".

Objective-CSwift

- (void)adsLoader:(IMAAdsLoader *)loader adsLoadedWithData:(IMAAdsLoadedData *)adsLoadedData {
  NSLog(@"Stream created with: %@.", adsLoadedData.streamManager.streamId);
  self.streamManager = adsLoadedData.streamManager;
  self.streamManager.delegate = self;

  // Build the Pod serving Stream URL.
  NSString *streamID = adsLoadedData.streamManager.streamId;
  // Your custom VTP handler takes the stream ID and returns the stream manifest URL.
  NSString *urlString = gCustomVTPHandler(streamID);
  NSURL *streamUrl = [NSURL URLWithString:urlString];
  if (kStreamType == StreamTypeLive) {
    // Load live streams directly into the AVPlayer.
    [self.videoDisplay loadStream:streamUrl withSubtitles:@[]];
    [self.videoDisplay play];
  } else {
    // Load VOD streams using the `loadThirdPartyStream` method in IMA SDK's stream manager.
    // The stream manager loads the stream, requests metadata, and starts playback.
    [self.streamManager loadThirdPartyStream:streamUrl streamSubtitles:@[]];
  }
}

- (void)adsLoader:(IMAAdsLoader *)loader failedWithErrorData:(IMAAdLoadingErrorData *)adErrorData {
  // Log the error and play the backup content.
  NSLog(@"AdsLoader error, code:%ld, message: %@", adErrorData.adError.code,
        adErrorData.adError.message);
  [self.videoPlayer play];
}

func adsLoader(_ loader: IMAAdsLoader, adsLoadedWith adsLoadedData: IMAAdsLoadedData) {
  print("DAI stream loaded. Stream session ID: \(adsLoadedData.streamManager!.streamId!)")
  streamManager = adsLoadedData.streamManager!
  streamManager!.delegate = self

  // Build the Pod serving Stream URL.
  let streamID = streamManager!.streamId
  // Your custom VTP handler takes the stream ID and returns the stream manifest URL.
  let urlString = ViewController.customVTPParser(streamID!)
  let streamUrl = URL(string: urlString)
  if ViewController.requestType == StreamType.live {
    // Live streams can be loaded directly into the AVPlayer.
    imaVideoDisplay.loadStream(streamUrl!, withSubtitles: [])
    imaVideoDisplay.play()
  } else {
    // VOD streams are loaded using the IMA SDK's stream manager.
    // The stream manager loads the stream, requests metadata, and starts playback.
    streamManager!.loadThirdPartyStream(streamUrl!, streamSubtitles: [])
  }
}

func adsLoader(_ loader: IMAAdsLoader, failedWith adErrorData: IMAAdLoadingErrorData) {
  print("Error loading DAI stream. Error message: \(adErrorData.adError.message!)")
  // Play the backup stream.
  videoPlayer.play()
}

تنفيذ معالِج VTP المخصّص

يُرسِل معالِج VTP المخصّص معرّف البث للمشاهد إلى شريكك التقني لفيديو (VTP) مع أي معلومات أخرى يتطلّب منك شريكك التقني لفيديو عرضها في بيان البث الذي يتضمّن كلّ من المحتوى والإعلانات المُدمَجة. سيقدّم لك VTP تعليمات محدّدة حول كيفية تنفيذ معالِج VTP المخصّص.

على سبيل المثال، قد يتضمّن ملف VTP عنوان URL لنموذج البيان يحتوي على الماكرو [[STREAMID]]. في هذا المثال، يُدرِج معالِج الحدث معرّف البث بدلاً من وحدة الماكرو ويعرض عنوان URL للبيان الناتج.

Objective-CSwift

/// Custom VTP Handler.
///
/// Returns the stream manifest URL from the video technical partner or manifest manipulator.
static NSString *(^gCustomVTPHandler)(NSString *) = ^(NSString *streamID) {
  // Insert synchronous code here to retrieve a stream manifest URL from your video tech partner
  // or manifest manipulation server.
  // This example uses a hardcoded URL template, containing a placeholder for the stream
  // ID and replaces the placeholder with the stream ID.
  NSString *manifestUrl = @"YOUR_MANIFEST_URL_TEMPLATE";
  return [manifestUrl stringByReplacingOccurrencesOfString:@"[[STREAMID]]"
                                                withString:streamID];
};

/// Custom VTP Handler.
///
/// Returns the stream manifest URL from the video technical partner or manifest manipulator.
static let customVTPParser = { (streamID: String) -> (String) in
  // Insert synchronous code here to retrieve a stream manifest URL from your video tech partner
  // or manifest manipulation server.
  // This example uses a hardcoded URL template, containing a placeholder for the stream
  // ID and replaces the placeholder with the stream ID.
  let manifestURL = "YOUR_MANIFEST_URL_TEMPLATE"
  return manifestURL.replacingOccurrences(of: "[[STREAMID]]", with: streamID)
}

الاستماع إلى أحداث الإعلانات

يُطلِق IMAStreamManager طُرق IMAStreamManagerDelegate لتمرير أحداث البث والأخطاء إلى تطبيقك.

في هذا المثال، سجِّل أحداث الإعلانات الأساسية في وحدة التحكّم:

Objective-CSwift

- (void)streamManager:(IMAStreamManager *)streamManager didReceiveAdEvent:(IMAAdEvent *)event {
  NSLog(@"Ad event (%@).", event.typeString);
  switch (event.type) {
    case kIMAAdEvent_STARTED: {
      // Log extended data.
      NSString *extendedAdPodInfo = [[NSString alloc]
          initWithFormat:@"Showing ad %ld/%ld, bumper: %@, title: %@, description: %@, contentType:"
                         @"%@, pod index: %ld, time offset: %lf, max duration: %lf.",
                         (long)event.ad.adPodInfo.adPosition, (long)event.ad.adPodInfo.totalAds,
                         event.ad.adPodInfo.isBumper ? @"YES" : @"NO", event.ad.adTitle,
                         event.ad.adDescription, event.ad.contentType,
                         (long)event.ad.adPodInfo.podIndex, event.ad.adPodInfo.timeOffset,
                         event.ad.adPodInfo.maxDuration];

      NSLog(@"%@", extendedAdPodInfo);
      break;
    }
    case kIMAAdEvent_AD_BREAK_STARTED: {
      NSLog(@"Ad break started");
      break;
    }
    case kIMAAdEvent_AD_BREAK_ENDED: {
      NSLog(@"Ad break ended");
      break;
    }
    case kIMAAdEvent_AD_PERIOD_STARTED: {
      NSLog(@"Ad period started");
      break;
    }
    case kIMAAdEvent_AD_PERIOD_ENDED: {
      NSLog(@"Ad period ended");
      break;
    }
    default:
      break;
  }
}

- (void)streamManager:(IMAStreamManager *)streamManager didReceiveAdError:(IMAAdError *)error {
  NSLog(@"StreamManager error with type: %ld\ncode: %ld\nmessage: %@", error.type, error.code,
        error.message);
  [self.videoPlayer play];
}

func streamManager(_ streamManager: IMAStreamManager, didReceive event: IMAAdEvent) {
  print("Ad event \(event.typeString).")
  switch event.type {
  case IMAAdEventType.STARTED:
    // Log extended data.
    if let ad = event.ad {
      let extendedAdPodInfo = String(
        format: "Showing ad %zd/%zd, bumper: %@, title: %@, "
          + "description: %@, contentType:%@, pod index: %zd, "
          + "time offset: %lf, max duration: %lf.",
        ad.adPodInfo.adPosition,
        ad.adPodInfo.totalAds,
        ad.adPodInfo.isBumper ? "YES" : "NO",
        ad.adTitle,
        ad.adDescription,
        ad.contentType,
        ad.adPodInfo.podIndex,
        ad.adPodInfo.timeOffset,
        ad.adPodInfo.maxDuration)

      print("\(extendedAdPodInfo)")
    }
    break
  case IMAAdEventType.AD_BREAK_STARTED:
    print("Ad break started.")
    break
  case IMAAdEventType.AD_BREAK_ENDED:
    print("Ad break ended.")
    break
  case IMAAdEventType.AD_PERIOD_STARTED:
    print("Ad period started.")
    break
  case IMAAdEventType.AD_PERIOD_ENDED:
    print("Ad period ended.")
    break
  default:
    break
  }
}

func streamManager(_ streamManager: IMAStreamManager, didReceive error: IMAAdError) {
  print("StreamManager error with type: \(error.type)")
  print("code: \(error.code)")
  print("message: \(error.message ?? "Unknown Error")")
}

تنظيف مواد عرض "الإعلانات الديناميكية أثناء عرض الفيديو" من IMA

لإيقاف تشغيل البث، وإيقاف جميع عمليات تتبُّع الإعلانات، وتحرير كل مواد عرض البث المحمَّلة، اتصل بالرقم IMAStreamManager.destroy().

شغِّل تطبيقك، وإذا نجحت عملية الإعداد، يمكنك طلب أحداث البث من خلال ميزة "الإعلانات أثناء التشغيل" من Google وتشغيلها باستخدام حزمة تطوير البرامج لإعلانات الوسائط التفاعلية. للتعرّف على ميزات حزمة SDK الأكثر تقدمًا، اطّلِع على عناوين المساعدة الأخرى المدرَجة في الشريط الجانبي الأيمن أو على عيّنات على GitHub.