> ## 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.

# The next episode

> Create the next episode's `VideoAds` while the current one plays, prefetch its ads, and destroy the current one when the viewer leaves.

Create the next episode's `VideoAds` while the current one plays, prefetch its ads, and destroy the current one when the viewer leaves. Any number can be alive at once; the SDK keeps at most one ad on screen in the whole app.

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

// When the viewer moves on:
ads.destroy()
ads = next
ads.track(player)     // the player now plays the next episode
```

Call `track` when the episode actually starts in the player, not at creation: before that the player's time belongs to the previous episode.

For a post-roll, show this episode's linear ad when it finishes, and move on only from its `onAdEnded`: `destroy()` ends the ad on screen too, a linear ad included.

```kotlin theme={null}
fun episodeFinished() {
    val ad = postRoll ?: return nextEpisode()
    try {
        ad.show(this)                        // destroy() now would end it
    } catch (e: VideoAdException) {
        nextEpisode()                        // not shown
    }
}

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

private fun nextEpisode() {
    ads.destroy()                            // after the post-roll's onAdEnded, never before
    ads = next
    ads.track(player)
}
```


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