开始使用

IMA SDK 可让您轻松地将多媒体广告集成到您的网站和应用中。IMA SDK 可以 向任意 <ph type="x-smartling-placeholder"></ph> 符合 VAST 标准的广告服务器,并管理应用中的广告播放。借助 IMA 客户端 SDK 您控制内容视频播放,而 SDK 处理广告播放。广告播放 独立的视频播放器,位于应用的内容视频播放器之上。

本指南将演示如何使用 Android VideoView 显示内容和广告。跟随已完成的样书学习 集成,请下载 GitHub 中的 BasicExample

<ph type="x-smartling-placeholder">

IMA 客户端概览

实施 IMA 客户端涉及四个主要的 SDK 组件,本示例演示了这些组件 指南:

  • AdDisplayContainer: 用于呈现广告的容器对象。
  • AdsLoader: 用于请求广告并处理广告请求响应中事件的对象。您只能 实例化一个广告加载器,该加载器可在应用的整个生命周期内重复使用。
  • AdsRequest: 用于定义广告请求的对象。广告请求会指定 VAST 广告代码的网址,以及 其他参数(例如广告尺寸)
  • AdsManager: 一个对象,包含对广告请求的响应、控制广告播放以及监听广告 所有事件。

前提条件

1. 创建新的 Android Studio 项目

如需创建 Android Studio 项目,请完成以下步骤:

  1. 启动 Android Studio。
  2. 选择 Start a new Android Studio project
  3. Choose your project 页面中,选择 Empty Activity 模板。
  4. 点击下一步
  5. Configure your project 页面中,为项目命名并选择 Java 作为语言。
  6. 点击完成

2. 将 IMA SDK 添加到您的项目中

首先,在应用级 build.gradle 文件中,将 IMA SDK 的导入项添加到 依赖项部分。由于 IMA SDK 较大,请在此处实施并启用 MultiDex。这个 对于 minSdkVersion 设为 20 或更低的应用而言,是必需的。此外,请添加新的 compileOptions:用于指定 Java 版本兼容性信息。

app/build.gradle
android {
    namespace 'com.google.ads.interactivemedia.v3.samples.videoplayerapp'
    compileSdkVersion 34

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_17
        targetCompatibility JavaVersion.VERSION_17
    }

    defaultConfig {
        applicationId "com.google.ads.interactivemedia.v3.samples.videoplayerapp"
        minSdkVersion 21
        targetSdkVersion 34
        multiDexEnabled true
        versionCode 1
        versionName "1.0"
    }

    ...
}

dependencies {
    implementation 'androidx.appcompat:appcompat:1.6.1'
    implementation 'androidx.browser:browser:1.6.0'
    implementation 'androidx.media:media:1.6.0'
    implementation 'com.google.ads.interactivemedia.v3:interactivemedia:3.35.1'

    ...
}

3. 添加 IMA SDK 所需的权限

添加 IMA SDK 请求广告所需的用户权限。

app/src/main/AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.project name">

    <!-- Required permissions for the IMA SDK -->
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

    ...

</manifest>

4. 更新应用布局

更新应用的布局以添加 VideoView,以便同时播放内容和 广告。

app/src/main/res/layout/activity_my.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MyActivity"
    tools:ignore="MergeRootFrame">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="0.4"
        android:orientation="vertical"
        android:id="@+id/videoPlayerContainer" >

        <VideoView
            android:id="@+id/videoView"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

        <ImageButton
            android:id="@+id/playButton"
            android:contentDescription="@string/play_description"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:src="@drawable/ic_action_play_over_video"
            android:background="@null" />

    </RelativeLayout>

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="0.6"
        android:id="@+id/videoDescription" >

        <TextView
            android:id="@+id/playerDescription"
            android:text="@string/app_name"
            android:textAlignment="center"
            android:gravity="center_horizontal"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:textSize="@dimen/font_size" />

    </FrameLayout>

</LinearLayout>

5. 将 IMA 导入主活动

为 IMA SDK 添加 import 语句。然后,更新 MyActivity 类扩展 AppCompatActivity。通过 AppCompatActivity 类允许在旧版系统上支持较新的平台功能 Android 设备。然后,添加一组将在应用中使用的私有变量。

app/src/main/java/com/example/project name/MyActivity.java

import android.content.Context;
import android.media.AudioManager;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.MediaController;
import android.widget.VideoView;
import com.google.ads.interactivemedia.v3.api.AdDisplayContainer;
import com.google.ads.interactivemedia.v3.api.AdErrorEvent;
import com.google.ads.interactivemedia.v3.api.AdEvent;
import com.google.ads.interactivemedia.v3.api.AdsLoader;
import com.google.ads.interactivemedia.v3.api.AdsManager;
import com.google.ads.interactivemedia.v3.api.AdsManagerLoadedEvent;
import com.google.ads.interactivemedia.v3.api.AdsRenderingSettings;
import com.google.ads.interactivemedia.v3.api.AdsRequest;
import com.google.ads.interactivemedia.v3.api.ImaSdkFactory;
import com.google.ads.interactivemedia.v3.api.ImaSdkSettings;
import com.google.ads.interactivemedia.v3.api.player.VideoProgressUpdate;
import java.util.Arrays;

...

public class MyActivity extends AppCompatActivity {

  private static final String LOGTAG = "IMABasicSample";
  private static final String SAMPLE_VIDEO_URL =
      "https://storage.googleapis.com/gvabox/media/samples/stock.mp4";

  /**
   * IMA sample tag for a single skippable inline video ad. See more IMA sample tags at
   * https://developers.google.com/interactive-media-ads/docs/sdks/html5/client-side/tags
   */
  private static final String SAMPLE_VAST_TAG_URL =
      "https://pubads.g.doubleclick.net/gampad/ads?iu=/21775744923/external/"
          + "single_preroll_skippable&sz=640x480&ciu_szs=300x250%2C728x90&gdfp_req=1&output=vast"
          + "&unviewed_position_start=1&env=vp&impl=s&correlator=";

  // Factory class for creating SDK objects.
  private ImaSdkFactory sdkFactory;

  // The AdsLoader instance exposes the requestAds method.
  private AdsLoader adsLoader;

  // AdsManager exposes methods to control ad playback and listen to ad events.
  private AdsManager adsManager;

  // The saved content position, used to resumed content following an ad break.
  private int savedPosition = 0;

  // This sample uses a VideoView for content and ad playback. For production
  // apps, Android's Exoplayer offers a more fully featured player compared to
  // the VideoView.
  private VideoView videoPlayer;
  private MediaController mediaController;
  private View playButton;
  private VideoAdPlayerAdapter videoAdPlayerAdapter;

}

6. 创建 VideoAdPlayerAdapter 类

使用 VideoView 创建一个 VideoAdPlayerAdapter 类, 并根据 IMA 的 <ph type="x-smartling-placeholder"></ph> VideoAdPlayer 界面。此类将处理内容和广告播放,并包含 方法。

app/src/main/java/com/example/project name/VideoAdPlayerAdapter.java
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.net.Uri;
import android.util.Log;
import android.widget.VideoView;
import com.google.ads.interactivemedia.v3.api.AdPodInfo;
import com.google.ads.interactivemedia.v3.api.player.AdMediaInfo;
import com.google.ads.interactivemedia.v3.api.player.VideoAdPlayer;
import com.google.ads.interactivemedia.v3.api.player.VideoProgressUpdate;
import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

/** Example implementation of IMA's VideoAdPlayer interface. */
public class VideoAdPlayerAdapter implements VideoAdPlayer {

  private static final String LOGTAG = "IMABasicSample";
  private static final long POLLING_TIME_MS = 250;
  private static final long INITIAL_DELAY_MS = 250;
  private final VideoView videoPlayer;
  private final AudioManager audioManager;
  private final List<VideoAdPlayerCallback> videoAdPlayerCallbacks = new ArrayList<>();
  private Timer timer;
  private int adDuration;

  // The saved ad position, used to resumed ad playback following an ad click-through.
  private int savedAdPosition;
  private AdMediaInfo loadedAdMediaInfo;

  public VideoAdPlayerAdapter(VideoView videoPlayer, AudioManager audioManager) {
    this.videoPlayer = videoPlayer;
    this.videoPlayer.setOnCompletionListener(
        (MediaPlayer mediaPlayer) -> notifyImaOnContentCompleted());
    this.audioManager = audioManager;
  }
}

7. 替换 VideoAdPlayer 方法

替换以下 VideoAdPlayer 方法:

playAd() 方法用于设置内容或广告网址,并设置要开始播放的监听器 在媒体加载后播放。

app/src/main/java/com/example/project name/VideoAdPlayerAdapter.java
/** Example implementation of IMA's VideoAdPlayer interface. */
public class VideoAdPlayerAdapter implements VideoAdPlayer {

  ...

  @Override
  public void addCallback(VideoAdPlayerCallback videoAdPlayerCallback) {
    videoAdPlayerCallbacks.add(videoAdPlayerCallback);
  }

  @Override
  public void loadAd(AdMediaInfo adMediaInfo, AdPodInfo adPodInfo) {
    // This simple ad loading logic works because preloading is disabled. To support
    // preloading ads your app must maintain state for the currently playing ad
    // while handling upcoming ad downloading and buffering at the same time.
    // See the IMA Android preloading guide for more info:
    // https://developers.google.com/interactive-media-ads/docs/sdks/android/client-side/preload
    loadedAdMediaInfo = adMediaInfo;
  }

  @Override
  public void pauseAd(AdMediaInfo adMediaInfo) {
    Log.i(LOGTAG, "pauseAd");
    savedAdPosition = videoPlayer.getCurrentPosition();
    stopAdTracking();
  }

  @Override
  public void playAd(AdMediaInfo adMediaInfo) {
    videoPlayer.setVideoURI(Uri.parse(adMediaInfo.getUrl()));

    videoPlayer.setOnPreparedListener(
        mediaPlayer -> {
          adDuration = mediaPlayer.getDuration();
          if (savedAdPosition > 0) {
            mediaPlayer.seekTo(savedAdPosition);
          }
          mediaPlayer.start();
          startAdTracking();
        });
    videoPlayer.setOnErrorListener(
        (mediaPlayer, errorType, extra) -> notifyImaSdkAboutAdError(errorType));
    videoPlayer.setOnCompletionListener(
        mediaPlayer -> {
          savedAdPosition = 0;
          notifyImaSdkAboutAdEnded();
        });
  }

  @Override
  public void release() {
    // any clean up that needs to be done.
  }

  @Override
  public void removeCallback(VideoAdPlayerCallback videoAdPlayerCallback) {
    videoAdPlayerCallbacks.remove(videoAdPlayerCallback);
  }

  @Override
  public void stopAd(AdMediaInfo adMediaInfo) {
    Log.i(LOGTAG, "stopAd");
    stopAdTracking();
  }

  /** Returns current volume as a percent of max volume. */
  @Override
  public int getVolume() {
    return audioManager.getStreamVolume(AudioManager.STREAM_MUSIC)
        / audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
  }

8. 设置广告跟踪

为了注册广告事件 VideoAdPlayerCallback.onAdProgress 必须作为内容和广告进度进行调用。为了支持此功能,请设置一个计时器,以便按设定的时间间隔调用 onAdProgress()

app/src/main/java/com/example/project name/VideoAdPlayerAdapter.java
/** Example implementation of IMA's VideoAdPlayer interface. */
public class VideoAdPlayerAdapter implements VideoAdPlayer {

  ...

  private void startAdTracking() {
    Log.i(LOGTAG, "startAdTracking");
    if (timer != null) {
      return;
    }
    timer = new Timer();
    TimerTask updateTimerTask =
        new TimerTask() {
          @Override
          public void run() {
            VideoProgressUpdate progressUpdate = getAdProgress();
            notifyImaSdkAboutAdProgress(progressUpdate);
          }
        };
    timer.schedule(updateTimerTask, POLLING_TIME_MS, INITIAL_DELAY_MS);
  }

  private void notifyImaSdkAboutAdEnded() {
    Log.i(LOGTAG, "notifyImaSdkAboutAdEnded");
    savedAdPosition = 0;
    for (VideoAdPlayer.VideoAdPlayerCallback callback : videoAdPlayerCallbacks) {
      callback.onEnded(loadedAdMediaInfo);
    }
  }

  private void notifyImaSdkAboutAdProgress(VideoProgressUpdate adProgress) {
    for (VideoAdPlayer.VideoAdPlayerCallback callback : videoAdPlayerCallbacks) {
      callback.onAdProgress(loadedAdMediaInfo, adProgress);
    }
  }

  /**
   * @param errorType Media player's error type as defined at
   *     https://cs.android.com/android/platform/superproject/+/master:frameworks/base/media/java/android/media/MediaPlayer.java;l=4335
   * @return True to stop the current ad playback.
   */
  private boolean notifyImaSdkAboutAdError(int errorType) {
    Log.i(LOGTAG, "notifyImaSdkAboutAdError");

    switch (errorType) {
      case MediaPlayer.MEDIA_ERROR_UNSUPPORTED:
        Log.e(LOGTAG, "notifyImaSdkAboutAdError: MEDIA_ERROR_UNSUPPORTED");
        break;
      case MediaPlayer.MEDIA_ERROR_TIMED_OUT:
        Log.e(LOGTAG, "notifyImaSdkAboutAdError: MEDIA_ERROR_TIMED_OUT");
        break;
      default:
        break;
    }
    for (VideoAdPlayer.VideoAdPlayerCallback callback : videoAdPlayerCallbacks) {
      callback.onError(loadedAdMediaInfo);
    }
    return true;
  }

  public void notifyImaOnContentCompleted() {
    Log.i(LOGTAG, "notifyImaOnContentCompleted");
    for (VideoAdPlayer.VideoAdPlayerCallback callback : videoAdPlayerCallbacks) {
      callback.onContentComplete();
    }
  }

  private void stopAdTracking() {
    Log.i(LOGTAG, "stopAdTracking");
    if (timer != null) {
      timer.cancel();
      timer = null;
    }
  }

  @Override
  public VideoProgressUpdate getAdProgress() {
    long adPosition = videoPlayer.getCurrentPosition();
    return new VideoProgressUpdate(adPosition, adDuration);
  }
}

9. 在 onCreate 方法中启动 IMA

覆盖 onCreate 方法并添加所需的变量赋值,以启动 IMA。在此步骤中,创建以下内容:

  • ImaSdkSettings
  • AdsLoader
。 <ph type="x-smartling-placeholder">

此步骤还会创建一个 VideoAdPlayerAdapter,即您稍后创建的类 。

最后,将播放按钮设置为请求广告,然后在点击该按钮后隐藏。

app/src/main/java/com/example/project name/MyActivity.java

...

public class MyActivity extends AppCompatActivity {

  ...

  private VideoView videoPlayer;
  private MediaController mediaController;
  private View playButton;
  private VideoAdPlayerAdapter videoAdPlayerAdapter;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_my);

    // Create the UI for controlling the video view.
    mediaController = new MediaController(this);
    videoPlayer = findViewById(R.id.videoView);
    mediaController.setAnchorView(videoPlayer);
    videoPlayer.setMediaController(mediaController);

    // Create an ad display container that uses a ViewGroup to listen to taps.
    ViewGroup videoPlayerContainer = findViewById(R.id.videoPlayerContainer);
    AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
    videoAdPlayerAdapter = new VideoAdPlayerAdapter(videoPlayer, audioManager);

    sdkFactory = ImaSdkFactory.getInstance();

    AdDisplayContainer adDisplayContainer =
        ImaSdkFactory.createAdDisplayContainer(videoPlayerContainer, videoAdPlayerAdapter);

    // Create an AdsLoader.
    ImaSdkSettings settings = sdkFactory.createImaSdkSettings();
    adsLoader = sdkFactory.createAdsLoader(this, settings, adDisplayContainer);

    // When the play button is clicked, request ads and hide the button.
    playButton = findViewById(R.id.playButton);
    playButton.setOnClickListener(
        view -> {
          videoPlayer.setVideoPath(SAMPLE_VIDEO_URL);
          requestAds(SAMPLE_VAST_TAG_URL);
          view.setVisibility(View.GONE);
        });
  }

}

10. 添加 AdsLoader 监听器

addAdErrorListeneraddAdsLoadedListener。在 AdsLoadedListener 中,创建 AdsManager,并设置 AdsManager 错误监听器。

app/src/main/java/com/example/project name/MyActivity.java
  @Override
  protected void onCreate(Bundle savedInstanceState) {

  ...

    // Create an AdsLoader.
    ImaSdkSettings settings = sdkFactory.createImaSdkSettings();
    adsLoader = sdkFactory.createAdsLoader(this, settings, adDisplayContainer);

    // Add listeners for when ads are loaded and for errors.
    adsLoader.addAdErrorListener(
        new AdErrorEvent.AdErrorListener() {
          /** An event raised when there is an error loading or playing ads. */
          @Override
          public void onAdError(AdErrorEvent adErrorEvent) {
            Log.i(LOGTAG, "Ad Error: " + adErrorEvent.getError().getMessage());
            resumeContent();
          }
        });
    adsLoader.addAdsLoadedListener(
        new AdsLoader.AdsLoadedListener() {
          @Override
          public void onAdsManagerLoaded(AdsManagerLoadedEvent adsManagerLoadedEvent) {
            // Ads were successfully loaded, so get the AdsManager instance. AdsManager has
            // events for ad playback and errors.
            adsManager = adsManagerLoadedEvent.getAdsManager();

            // Attach event and error event listeners.
            adsManager.addAdErrorListener(
                new AdErrorEvent.AdErrorListener() {
                  /** An event raised when there is an error loading or playing ads. */
                  @Override
                  public void onAdError(AdErrorEvent adErrorEvent) {
                    Log.e(LOGTAG, "Ad Error: " + adErrorEvent.getError().getMessage());
                    String universalAdIds =
                        Arrays.toString(adsManager.getCurrentAd().getUniversalAdIds());
                    Log.i(
                        LOGTAG,
                        "Discarding the current ad break with universal "
                            + "ad Ids: "
                            + universalAdIds);
                    adsManager.discardAdBreak();
                  }
                });
          }
        });

11. 处理 IMA 广告事件

使用以下代码监听 IMA 广告事件: AdsManager.addAdEventListener。使用 switch 语句,为以下 IMA 事件设置操作:

代码段包含注释,其中提供了有关如何使用事件的详细信息。部署 事件后,您可以调用 AdsManager.init()

app/src/main/java/com/example/project name/MyActivity.java
        adsLoader.addAdsLoadedListener(
        new AdsLoader.AdsLoadedListener() {
          @Override
          public void onAdsManagerLoaded(AdsManagerLoadedEvent adsManagerLoadedEvent) {

            ...

            adsManager.addAdEventListener(
                new AdEvent.AdEventListener() {
                  /** Responds to AdEvents. */
                  @Override
                  public void onAdEvent(AdEvent adEvent) {
                    if (adEvent.getType() != AdEvent.AdEventType.AD_PROGRESS) {
                      Log.i(LOGTAG, "Event: " + adEvent.getType());
                    }
                    // These are the suggested event types to handle. For full list of
                    // all ad event types, see AdEvent.AdEventType documentation.
                    switch (adEvent.getType()) {
                      case LOADED:
                        // AdEventType.LOADED is fired when ads are ready to play.

                        // This sample app uses the sample tag
                        // single_preroll_skippable_ad_tag_url that requires calling
                        // AdsManager.start() to start ad playback.
                        // If you use a different ad tag URL that returns a VMAP or
                        // an ad rules playlist, the adsManager.init() function will
                        // trigger ad playback automatically and the IMA SDK will
                        // ignore the adsManager.start().
                        // It is safe to always call adsManager.start() in the
                        // LOADED event.
                        adsManager.start();
                        break;
                      case CONTENT_PAUSE_REQUESTED:
                        // AdEventType.CONTENT_PAUSE_REQUESTED is fired when you
                        // should pause your content and start playing an ad.
                        pauseContentForAds();
                        break;
                      case CONTENT_RESUME_REQUESTED:
                        // AdEventType.CONTENT_RESUME_REQUESTED is fired when the ad
                        // you should play your content.
                        resumeContent();
                        break;
                      case ALL_ADS_COMPLETED:
                        // Calling adsManager.destroy() triggers the function
                        // VideoAdPlayer.release().
                        adsManager.destroy();
                        adsManager = null;
                        break;
                      case CLICKED:
                        // When the user clicks on the Learn More button, the IMA SDK fires
                        // this event, pauses the ad, and opens the ad's click-through URL.
                        // When the user returns to the app, the IMA SDK calls the
                        // VideoAdPlayer.playAd() function automatically.
                        break;
                      default:
                        break;
                    }
                  }
                });
            AdsRenderingSettings adsRenderingSettings =
                ImaSdkFactory.getInstance().createAdsRenderingSettings();
            adsManager.init(adsRenderingSettings);
          }

12. 处理广告和内容之间的切换

在本部分中,创建 pauseContentForAdsresumeContent。 方法。这些方法会重复使用播放器 内容和广告您需要跟踪内容位置 在广告插播时间点之后继续播放。

app/src/main/java/com/example/project name/MyActivity.java
/** Main activity. */
public class MyActivity extends AppCompatActivity {

  ...

  private void pauseContentForAds() {
    Log.i(LOGTAG, "pauseContentForAds");
    savedPosition = videoPlayer.getCurrentPosition();
    videoPlayer.stopPlayback();
    // Hide the buttons and seek bar controlling the video view.
    videoPlayer.setMediaController(null);
  }

  private void resumeContent() {
    Log.i(LOGTAG, "resumeContent");

    // Show the buttons and seek bar controlling the video view.
    videoPlayer.setVideoPath(SAMPLE_VIDEO_URL);
    videoPlayer.setMediaController(mediaController);
    videoPlayer.setOnPreparedListener(
        mediaPlayer -> {
          if (savedPosition > 0) {
            mediaPlayer.seekTo(savedPosition);
          }
          mediaPlayer.start();
        });
    videoPlayer.setOnCompletionListener(
        mediaPlayer -> videoAdPlayerAdapter.notifyImaOnContentCompleted());
  }
}

13. 提出广告请求

现在,添加 requestAds 方法以构建 AdsRequest 并使用它来调用 AdsLoader.requestAds()

app/src/main/java/com/example/project name/MyActivity.java
/** Main activity. */
public class MyActivity extends AppCompatActivity {

  ...

  private void requestAds(String adTagUrl) {
    // Create the ads request.
    AdsRequest request = sdkFactory.createAdsRequest();
    request.setAdTagUrl(adTagUrl);
    request.setContentProgressProvider(
        () -> {
          if (videoPlayer.getDuration() <= 0) {
            return VideoProgressUpdate.VIDEO_TIME_NOT_READY;
          }
          return new VideoProgressUpdate(
              videoPlayer.getCurrentPosition(), videoPlayer.getDuration());
        });

    // Request the ad. After the ad is loaded, onAdsManagerLoaded() will be called.
    adsLoader.requestAds(request);
  }
}

大功告成!现在,您将通过 IMA SDK 请求并展示广告。如需详细了解其他 SDK 功能,请参阅其他指南或 GitHub 上的示例