ข้อกำหนดเบื้องต้น
- ผสานรวมรูปแบบโฆษณาเนทีฟ
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
คุณรับ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(AdManagerRequest()) } 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:[GAMRequest 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
อ่านนโยบายและหลักเกณฑ์สำหรับโฆษณาเนทีฟเพื่อดูคำแนะนำเพิ่มเติมเกี่ยวกับวิธีแสดงโฆษณาเนทีฟ