classMyTargetPreloadStatusControl(currentPlayingIndex:Int=C.INDEX_UNSET):TargetPreloadStatusControl<Int,DefaultPreloadManager.PreloadStatus>{overridefungetTargetPreloadStatus(index:Int):DefaultPreloadManager.PreloadStatus?{if(index-currentPlayingIndex==1){// next track// return a PreloadStatus that is labelled by STAGE_SPECIFIED_RANGE_LOADED and// suggest loading 3000ms from the default start positionreturnDefaultPreloadManager.PreloadStatus.specifiedRangeLoaded(3000L)}elseif(index-currentPlayingIndex==-1){// previous track// return a PreloadStatus that is labelled by STAGE_SPECIFIED_RANGE_LOADED and// suggest loading 3000ms from the default start positionreturnDefaultPreloadManager.PreloadStatus.specifiedRangeLoaded(3000L)}elseif(abs(index-currentPlayingIndex)==2){// return a PreloadStatus that is labelled by STAGE_TRACKS_SELECTEDreturnDefaultPreloadManager.PreloadStatus.TRACKS_SELECTED}elseif(abs(index-currentPlayingIndex)<=4){// return a PreloadStatus that is labelled by STAGE_SOURCE_PREPAREDreturnDefaultPreloadManager.PreloadStatus.SOURCE_PREPARED}returnnull}}
[null,null,["最后更新时间 (UTC):2025-09-11。"],[],[],null,["This page describes how to create a [`DefaultPreloadManager`](/reference/androidx/media3/exoplayer/source/preload/DefaultPreloadManager), which preloads\nmedia content for your app based on the strategy you choose.\n\nPreload managers based on the [`BasePreloadManager`](/reference/androidx/media3/exoplayer/source/preload/BasePreloadManager) abstract class let you\nrank content by the criteria you choose. This document explains how to use the\nderived class `DefaultPreloadManager`, in which each media item is ranked with\nan integer representing its location in a list (for example, its position in a\nvideo carousel). The preload manager prioritizes loading the items based on how\nclose it is to the item the user is currently playing. That way, if a user moves\nto another item, the new item can start playing right away.\n\nThere are three steps to creating an instance of `DefaultPreloadManager`:\n\n- [Define a `TargetPreloadStatusControl`](#create-tpsc) which the preload manager can query to find out if the media item is ready to be loaded and how much to load.\n- [Create the builder](#create-dpm) which you'll use to create the preload manager, and to create your app's `ExoPlayer` objects.\n- Use the builder to create the preload manager by calling the builder's [`build()`](/reference/androidx/media3/exoplayer/source/preload/DefaultPreloadManager.Builder#build()) method.\n\nCreate a target preload status control\n\nWhen you create the [`DefaultPreloadManager.Builder`](/reference/androidx/media3/exoplayer/source/preload/DefaultPreloadManager.Builder), you'll pass it a\n*target preload status control* object that you define. This object implements\nthe [`TargetPreloadStatusControl`](/reference/androidx/media3/exoplayer/source/preload/TargetPreloadStatusControl) interface. When the preload manager is\ngetting ready to preload media, it calls your status control's\n[`getTargetPreloadStatus()`](/reference/androidx/media3/exoplayer/source/preload/TargetPreloadStatusControl#getTargetPreloadStatus(T)) method to find out how much content to load. The\nstatus control can reply with one of these status codes:\n\n- [`STAGE_SPECIFIED_RANGE_LOADED`](/reference/androidx/media3/exoplayer/source/preload/DefaultPreloadManager.PreloadStatus#STAGE_SPECIFIED_RANGE_LOADED()): The preload manager should load the content from the specified start position and for the specified duration (given in milliseconds).\n- [`STAGE_TRACKS_SELECTED`](/reference/androidx/media3/exoplayer/source/preload/DefaultPreloadManager.PreloadStatus#STAGE_TRACKS_SELECTED()): The preload manager should load and process the content track's information, and select the tracks. The preload manager shouldn't start loading the content yet.\n- [`STAGE_SOURCE_PREPARED`](/reference/androidx/media3/exoplayer/source/preload/DefaultPreloadManager.PreloadStatus#STAGE_SOURCE_PREPARED()): The preload manager should prepare the content source. For example, if the content's metadata is in a separate manifest file, the preload manager might fetch and parse that manifest.\n- `null`: The preload manager shouldn't load any content or metadata for that media item.\n\nYou'll need to have a strategy for deciding how much content to load for each\nmedia item. In this example, more content is loaded for items closest to the\nitem that's currently playing. If the user is playing content with index **n**,\nthe controller returns the following codes:\n\n- Index **n+1** (the next media item): Load 3000 ms (3 seconds) from the default start position\n- Index **n-1** (the previous media item): Load 1000 ms (1 second) from the default start position\n- Other media items in the range **n-2** to **n+2** : Return `PreloadStatus.TRACKS_SELECTED`\n- Other media items in the range **n-4** to **n+4** : Return `PreloadStatus.SOURCE_PREPARED`\n- For all other media items, return `null`\n\n\n```kotlin\nclass MyTargetPreloadStatusControl(\n currentPlayingIndex: Int = C.INDEX_UNSET\n) : TargetPreloadStatusControl\u003cInt, DefaultPreloadManager.PreloadStatus\u003e {\n\n override fun getTargetPreloadStatus(index: Int): DefaultPreloadManager.PreloadStatus? {\n if (index - currentPlayingIndex == 1) { // next track\n // return a PreloadStatus that is labelled by STAGE_SPECIFIED_RANGE_LOADED and\n // suggest loading 3000ms from the default start position\n return DefaultPreloadManager.PreloadStatus.specifiedRangeLoaded(3000L)\n } else if (index - currentPlayingIndex == -1) { // previous track\n // return a PreloadStatus that is labelled by STAGE_SPECIFIED_RANGE_LOADED and\n // suggest loading 3000ms from the default start position\n return DefaultPreloadManager.PreloadStatus.specifiedRangeLoaded(3000L)\n } else if (abs(index - currentPlayingIndex) == 2) {\n // return a PreloadStatus that is labelled by STAGE_TRACKS_SELECTED\n return DefaultPreloadManager.PreloadStatus.TRACKS_SELECTED\n } else if (abs(index - currentPlayingIndex) \u003c= 4) {\n // return a PreloadStatus that is labelled by STAGE_SOURCE_PREPARED\n return DefaultPreloadManager.PreloadStatus.SOURCE_PREPARED\n }\n return null\n }\n}https://github.com/android/snippets/blob/9efabddf7d28b059560ab8e3d3e584fabe481501/misc/src/main/java/com/example/snippets/PreloadManagerKotlinSnippets.kt#L36-L58\n```\n\n\u003cbr /\u003e\n\nKey points about the code\n\n- You'll pass an instance of `MyTargetPreloadStatusControl` to the preload manager builder when you create it.\n- `currentPlayingIndex` holds the index of whatever media item is currently playing. It's the app's job to keep that value up to date.\n- When the preload manager is ready to load content, it calls `getTargetPreloadStatus` and passes the ranking information you specified for that corresponding media item. In the case of `DefaultPreloadManager`, that ranking information is an integer, specifying the item's position in a carousel. The method chooses what code to return by comparing that index with the index of the item that's currently selected.\n\nCreate the preload manager\n\nTo create your preload manager, you need a [`DefaultPreloadManager.Builder`](/reference/androidx/media3/exoplayer/source/preload/DefaultPreloadManager.Builder).\nThat builder is configured with the current context and the app's target preload\nstatus control. The builder also provides setter methods which you can use to\nset the preload manager's custom components.\n\nBesides using the builder to create the preload manager, you'll also use it to\ncreate the [`ExoPlayer`](/media/media3/exoplayer) objects your app uses to play the content.\n| **Note:** You can only use a `DefaultPreloadManager.Builder` to create a single `DefaultPreloadManager`. If you try to create a second preload manager with the same builder, it throws an exception, even if you've [released the first preload\n| manager](/media/media3/exoplayer/preloading-media/preloadmanager/manage-play#release).\n\n\n```kotlin\nval targetPreloadStatusControl = MyTargetPreloadStatusControl()\nval preloadManagerBuilder =\n DefaultPreloadManager.Builder(context, targetPreloadStatusControl)\nval preloadManager = preloadManagerBuilder.build()https://github.com/android/snippets/blob/9efabddf7d28b059560ab8e3d3e584fabe481501/misc/src/main/java/com/example/snippets/PreloadManagerKotlinSnippets.kt#L72-L75\n```\n\n\u003cbr /\u003e\n\nKey points about the code\n\n- `MyTargetPreloadStatusControl` is the class you defined in [Create a target\n preload status control](#create-tpsc).\n- You'll use the same `DefaultPreloadManager.Builder` to create the `ExoPlayer` objects that will play content managed by that preload manager."]]