> ## Documentation Index
> Fetch the complete documentation index at: https://docs.kontext.so/llms.txt
> Use this file to discover all available pages before exploring further.

# 下一集

> 当前一集播放时创建下一集的 `VideoAds`，预取它的广告，观众离开时销毁当前的。

当前一集播放时创建下一集的 `VideoAds`，预取它的广告，观众离开时销毁当前的。任意数量的实例可以同时存在；SDK 让整个应用同一时间最多一个广告在屏幕上。

```kotlin theme={null}
val next = VideoAds(nextEpisode)
lifecycleScope.launch {
    nextLinear = try { next.format(AdFormat.LINEAR).apply { load() } } catch (e: VideoAdException) { null }
}

// 观众继续看下一集时：
ads.destroy()
ads = next
ads.track(player)     // 播放器现在播放下一集
```

在剧集真正开始在播放器中播放时调用 `track`，而不是创建时：在那之前播放器的时间属于上一集。

对于片尾广告，在这一集播完时展示它的线性广告，并且只在它的 `onAdEnded` 中切到下一集：`destroy()` 也会结束屏幕上的广告，线性广告也不例外。

```kotlin theme={null}
fun episodeFinished() {
    val ad = postRoll ?: return nextEpisode()
    try {
        ad.show(this)                        // 此时调用 destroy() 会结束它
    } catch (e: VideoAdException) {
        nextEpisode()                        // 没有展示
    }
}

override fun onAdEnded(ad: VideoAd, reason: VideoAd.EndReason) {
    if (ad === postRoll) nextEpisode()
}

private fun nextEpisode() {
    ads.destroy()                            // 在片尾广告的 onAdEnded 之后，绝不在之前
    ads = next
    ads.track(player)
}
```


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.