דרישות מוקדמות
- משלבים את פורמט המודעה המותאמת.
GADMediaContent
מודעות מותאמות מספקות גישה למחלקה GADMediaContent שמשמשת לקבלת מידע על תוכן מדיה, שיכול להיות סרטון או תמונה. הוא משמש גם כדי לשלוט בהפעלה של מודעות וידאו ולהאזין לאירועי הפעלה. אפשר לגשת לאובייקט של תוכן המדיה דרך המאפיין .mediaContent במודעה.
האובייקט GADMediaContent מכיל מידע כמו יחס הגובה-רוחב ומשך הסרטון. בקטע הקוד הבא אפשר לראות איך מקבלים את יחס הגובה-רוחב ואת משך הזמן של מודעה מותאמת.
Swift
if myNativeAd.mediaContent.hasVideoContent { let mediaAspectRatio = CGFloat(myNativeAd.mediaContent.aspectRatio) let duration = myNativeAd.mediaContent.duration }
Objective-C
if(myNativeAd.mediaContent.hasVideoContent) { CGFloat mediaAspectRatio = myNativeAd.mediaContent.aspectRatio; NSTimeInterval duration = myNativeAd.mediaContent.duration; }
GADVideoController
האובייקט GADMediaContent כולל הפניה לאובייקט GADVideoController. האובייקט GADVideoController מאפשר לבעלי תוכן דיגיטלי להגיב לאירועים בסרטונים.
אפשר לקבל את האובייקט GADVideoController באמצעות קריאה ל-GADMediaContent.videoController.
קריאות חוזרות (callback) לאירועי וידאו
כדי לטפל באירועי וידאו ספציפיים, צריך לכתוב מחלקה שמטמיעה את הפרוטוקול GADVideoControllerDelegate. כל השיטות של הפרוטוקול הן אופציונליות.
בדוגמה הבאה אפשר לראות איך מטמיעים את פרוטוקול הנציג:
Swift
class ViewController: NativeAdLoaderDelegate, VideoControllerDelegate { private var adLoader: AdLoader? func viewDidLoad() { super.viewDidLoad() let videoOptions = VideoOptions() videoOptions.customControlsRequested = true adLoader = AdLoader( adUnitID: "ca-app-pub-3940256099942544/3986624511", rootViewController: self, adTypes: [.native], options: [videoOptions]) adLoader?.delegate = self adLoader?.load(Request()) } func adLoader( _ adLoader: AdLoader?, didReceive nativeAd: NativeAd? ) { // Set the videoController's delegate to be notified of video events. nativeAd?.mediaContent.videoController.delegate = self } // VideoControllerDelegate methods func videoControllerDidPlayVideo(_ videoController: VideoController) { // Implement this method to receive a notification when the video controller // begins playing the ad. } func videoControllerDidPauseVideo(_ videoController: VideoController) { // Implement this method to receive a notification when the video controller // pauses the ad. } func videoControllerDidEndVideoPlayback(_ videoController: VideoController) { // Implement this method to receive a notification when the video controller // stops playing the ad. } func videoControllerDidMuteVideo(_ videoController: VideoController) { // Implement this method to receive a notification when the video controller // mutes the ad. } func videoControllerDidUnmuteVideo(_ videoController: VideoController) { // Implement this method to receive a notification when the video controller // unmutes the ad. } }
Objective-C
@interface ViewController () <GADNativeAdLoaderDelegate, GADVideoControllerDelegate> @property(nonatomic, strong) GADAdLoader *adLoader; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; GADVideoOptions *videoOptions = [[GADVideoOptions alloc] init]; videoOptions.customControlsRequested = YES; self.adLoader = [[GADAdLoader alloc] initWithAdUnitID:@"ca-app-pub-3940256099942544/3986624511" rootViewController:self adTypes:@[ GADAdLoaderAdTypeNative ] options:@[ videoOptions ]]; self.adLoader.delegate = self; [self.adLoader loadRequest:[GADRequest request]]; } - (void)adLoader:(GADAdLoader *)adLoader didReceiveNativeAd:(GADNativeAd *)nativeAd { // Set the videoController's delegate to be notified of video events. nativeAd.mediaContent.videoController.delegate = self; } // GADVideoControllerDelegate methods - (void)videoControllerDidPlayVideo:(nonnull GADVideoController *)videoController { // Implement this method to receive a notification when the video controller // begins playing the ad. } - (void)videoControllerDidPauseVideo:(nonnull GADVideoController *)videoController { // Implement this method to receive a notification when the video controller // pauses the ad. } - (void)videoControllerDidEndVideoPlayback:(nonnull GADVideoController *)videoController { // Implement this method to receive a notification when the video controller // stops playing the ad. } - (void)videoControllerDidMuteVideo:(nonnull GADVideoController *)videoController { // Implement this method to receive a notification when the video controller // mutes the ad. } - (void)videoControllerDidUnmuteVideo:(nonnull GADVideoController *)videoController { // Implement this method to receive a notification when the video controller // unmutes the ad. } @end
כדי לקבל הנחיות נוספות לגבי עיבוד של מודעות מותאמות, כדאי לקרוא את המדיניות וההנחיות בנושא מודעות מותאמות.