缩短了 IMA 加载时间
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
IMA SDK 会加载依赖项以请求动态广告插播 (DAI) 串流、衡量可见度并报告广告展示次数。通过在发出流式传输请求之前初始化 IMA,您可以在流式传输播放之前最大限度地加载 IMA 依赖项。本页介绍了如何在应用中处理 IMA 加载时间。
拨打 ImaSdkFactory.initialize()
ImaSdkFactory.initialize()
方法会在首次发出流式传输请求之前开始预加载 SDK 资源。如需使用 initialize()
,您需要 IMA 3.35.1 或更高版本。
在应用启动时调用 ImaSdkFactory.initialize()
,或者在应用生命周期中尽早调用 ImaSdkFactory.initialize()
(前提是应用结构允许)。initialize()
调用采用以下参数:
context
:使用应用上下文,该上下文在应用生命周期内始终可用。
settings
:传递已设置值的 ImaSdkSettings
对象。initialize()
调用中使用的 IMA 设置必须与 createAdsLoader()
调用中使用的设置值相同。IMA SDK 会将这些设置(尤其是语言设置)用作缓存键。我们不建议为 initialize()
方法调用中使用的设置使用测试值或虚假值,因为可能会发生缓存未命中并增加广告加载时间。
重复使用 AdsLoader
实例
为了缩短 IMA 加载时间,我们建议您重复使用相同的 AdsLoader
实例。AdsLoader
接口可以处理多个广告或串流请求。避免为每个请求创建新的 AdsLoader
实例。创建新的 AdsLoader
实例需要加载时间,并且会使用额外的设备资源。
在播放前请求数据流
如果您在开始流式播放之前有权访问 AdsLoader
实例,还可以提前调用 AdsLoader.requestStream()
方法。执行此调用会开始加载 DAI 数据流。当用户准备好开始流式传输播放时,调用 StreamManager.init()
方法以初始化流式传输播放。
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-08-21。
[null,null,["最后更新时间 (UTC):2025-08-21。"],[],[],null,["# Improve IMA load time\n\nThe IMA SDK loads dependencies to request Dynamic Ad Insertion (DAI) streams,\nmeasure viewability and report ad impressions. By initializing IMA before you\nmake a stream request, you maximize the time to load IMA dependencies before\nstream playback. This page covers how to handling IMA load times in your app.\n\nCall `ImaSdkFactory.initialize()`\n---------------------------------\n\nThe\n[`ImaSdkFactory.initialize()`](/ad-manager/dynamic-ad-insertion/sdk/android/api/reference/com/google/ads/interactivemedia/v3/api/ImaSdkFactory#initialize(android.content.Context,com.google.ads.interactivemedia.v3.api.ImaSdkSettings))\nmethod starts to preload SDK resources in advance of the first stream request.\nTo use `initialize()`, you need IMA version 3.35.1 or higher.\n\nCall `ImaSdkFactory.initialize()` on application startup, or as early in the\napplication's lifecycle as your app structure allows. The `initialize()` call\ntakes the following parameters:\n\n- **`context`**: Uses the application context, which is always available during the lifecycle of the application.\n- **`settings`** : Pass the an [`ImaSdkSettings`](/ad-manager/dynamic-ad-insertion/sdk/android/api/reference/com/google/ads/interactivemedia/v3/api/ImaSdkSettings) object with its values set. The IMA settings used in the `initialize()` call must be the same as the settings values used in the `createAdsLoader()` call. The IMA SDK utilizes these settings, particularly the language setting, for cache key. We don't recommend using test or fake values for the settings used in the `initialize()` method call, as cache misses might occur and increase ad load time.\n\nReuse the `AdsLoader` instance\n------------------------------\n\nTo improve IMA load time, we recommend you reuse the same\n[`AdsLoader`](/interactive-media-ads/docs/sdks/android/client-side/api/reference/com/google/ads/interactivemedia/v3/api/AdsLoader)\ninstance. The `AdsLoader` interface can handle multiple ad or stream requests.\nAvoid creating a new `AdsLoader` instance for each request. Creating a new\n`AdsLoader` instance requires a load time, and uses additional device resources.\n\nRequest streams in advance of playback\n--------------------------------------\n\nIf you have access to the `AdsLoader` instance prior to starting stream\nplayback, you can also call the\n[`AdsLoader.requestStream()`](/ad-manager/dynamic-ad-insertion/sdk/android/api/reference/com/google/ads/interactivemedia/v3/api/AdsLoader#requestStream(com.google.ads.interactivemedia.v3.api.StreamRequest))\nmethod early. Making this call starts loading the DAI stream. When the user\nis ready to start stream playback, call the\n[`StreamManager.init()`](/ad-manager/dynamic-ad-insertion/sdk/android/api/reference/com/google/ads/interactivemedia/v3/api/BaseManager#init())\nmethod to initialize stream playback."]]