Điều kiện tiên quyết
- Nhập SDK quảng cáo trên thiết bị di động của Google (dưới dạng một SDK riêng hoặc một phần của Firebase).
- Tích hợp Định dạng quảng cáo gốc.
MediaContent
Quảng cáo gốc cung cấp quyền truy cập vào đối tượngMediaContent
dùng để lấy thông tin về nội dung nghe nhìn. Nội dung đó có thể là
video hoặc hình ảnh. Nội dung này cũng được dùng để kiểm soát hoạt động phát lại quảng cáo dạng video và nghe
các sự kiện phát lại. Bạn có thể lấy đối tượng MediaContent
bằng cách gọi
NativeAd.getMediaContent()
.
Đối tượng MediaContent
chứa các thông tin, chẳng hạn như tỷ lệ khung hình và
thời lượng của video. Đoạn mã dưới đây cho biết cách lấy tỷ lệ khung hình và
thời lượng của quảng cáo gốc.
Java
if (myNativeAd.getMediaContent().hasVideoContent()) { float mediaAspectRatio = myNativeAd.getMediaContent().getAspectRatio(); float duration = myNativeAd.getMediaContent().getDuration(); ... }
Kotlin
if (myNativeAd.getMediaContent().hasVideoContent()) { val mediaAspectRatio: Float = myNativeAd.getMediaContent().getAspectRatio() val duration: Float = myNativeAd.getMediaContent().getDuration() ... }
VideoOptions
Lớp VideoOptions
cho phép các ứng dụng định cấu hình cách hoạt động của các tài sản video gốc.
Bạn phải gán các đối tượng VideoOptions
cho đối tượng NativeAdOptions
đã sử dụng khi tạo AdLoader
:
Java
VideoOptions videoOptions = new VideoOptions.Builder() .setStartMuted(false) .build(); NativeAdOptions adOptions = new NativeAdOptions.Builder() .setVideoOptions(videoOptions) .build(); AdLoader adLoader = new AdLoader.Builder(this, "ca-app-pub-3940256099942544/2247696110") .forNativeAd( ... ) .withNativeAdOptions(adOptions) .build();
Kotlin
val videoOptions = VideoOptions.Builder() .setStartMuted(false) .build() val adOptions = NativeAdOptions.Builder() .setVideoOptions(videoOptions) .build() val adLoader = AdLoader.Builder(this, "ca-app-pub-3940256099942544/2247696110") .forNativeAd( ... ) .withNativeAdOptions(adOptions) .build()
Lệnh gọi lại cho các sự kiện video
Để xử lý các sự kiện video cụ thể, hãy viết một lớp mở rộng lớp trừu tượng
VideoLifecycleCallbacks
và gọi
setVideoLifecycleCallbacks()
trên VideoController
. Sau đó, hãy chỉ ghi đè các hàm gọi lại mà bạn muốn.
Java
myNativeAd.getMediaContent().getVideoController() .setVideoLifecycleCallbacks(new VideoLifecycleCallbacks() { /** Called when video playback first begins. */ @Override public void onVideoStart() { // Do something when the video starts the first time. Log.d("MyApp", "Video Started"); } /** Called when video playback is playing. */ @Override public void onVideoPlay() { // Do something when the video plays. Log.d("MyApp", "Video Played"); } /** Called when video playback is paused. */ @Override public void onVideoPause() { // Do something when the video pauses. Log.d("MyApp", "Video Paused"); } /** Called when video playback finishes playing. */ @Override public void onVideoEnd() { // Do something when the video ends. Log.d("MyApp", "Video Ended"); } /** Called when the video changes mute state. */ @Override public void onVideoMute(boolean isMuted) { // Do something when the video is muted. Log.d("MyApp", "Video Muted"); } });
Kotlin
myNativeAd.getMediaContent().getVideoController().setVideoLifecycleCallbacks { /** Called when video playback first begins. */ override fun onVideoStart() { // Do something when the video starts the first time. Log.d("MyApp", "Video Started") } /** Called when video playback is playing. */ override fun onVideoPlay() { // Do something when the video plays. Log.d("MyApp", "Video Played") } /** Called when video playback is paused. */ override fun onVideoPause() { // Do something when the video pauses. Log.d("MyApp", "Video Paused") } /** Called when video playback finishes playing. */ override fun onVideoEnd() { // Do something when the video ends. Log.d("MyApp", "Video Ended") } /** Called when the video changes mute state. */ override fun onVideoMute(boolean isMuted) { // Do something when the video is muted. Log.d("MyApp", "Video Muted") } }