[null,null,["最后更新时间 (UTC):2025-09-05。"],[],[],null,["The preload manager helps you give users a better experience by serving content\nto them faster, with less waiting when they switch from one item to another. It\nalso lets you customize the duration and ranking of preloading per item.\n\nA common situation in social media is, an app shows a list or carousel of media\nchoices to the user. For example, an app might show a carousel of short videos.\nWhen one video finishes, the app switches to the next one. And if the user\ndoesn't like the video they're watching, they might swipe away to the next\nvideo, or the previous one.\n\nIf you don't preload video content, this can result in a frustrating user\nexperience. The user finishes with some media and then has to wait for the next\nmedia to load.\n\nOn the other hand, if you preload the content too aggressively,\nthat wastes power and network bandwidth loading content that the user may never\nactually play.\n\n[`DefaultPreloadManager`](/reference/androidx/media3/exoplayer/source/preload/DefaultPreloadManager) helps your app balance these concerns. The preload\nmanager works with your app to decide how important each media item is, and\nloads the appropriate amount in advance.\n\nDivision of labor\n\nIf you use `DefaultPreloadManager`, some work is done by your code, and some by\nthe preload manager.\n\nYour app has to do the following:\n\n- Create the app's `ExoPlayer` objects by using the same `DefaultPreloadManager.Builder` object you use to create the preload manager. Call `DefaultPreloadManager.Builder.buildExoPlayer()` to create an `ExoPlayer`.\n- [Tell the preload manager about each media item it should be tracking](/media/media3/exoplayer/preloading-media/preloadmanager/manage-play#add-items). This might not be all the content in the carousel; instead, you can just tell it about the first few items to be played. As the user navigates through the carousel, you can add [and remove](/media/media3/exoplayer/preloading-media/preloadmanager/manage-play#remove) media items from the preload manager's pool.\n- [Invalidate the priorities in the preload manager](/media/media3/exoplayer/preloading-media/preloadmanager/manage-play#invalidate) when the content in the carousel changes, or the user changes which item they're playing. This tells the preload manager to redetermine the priority of each media item, and load content if necessary. You'll invalidate the preload manager after you first add media items, and also when the user moves from one item to another, or when you add or remove items to the carousel.\n- [Respond to queries from the preload manager](/media/media3/exoplayer/preloading-media/preloadmanager/create#create-tpsc), telling the manager *how\n much* content to preload for each item.\n- [Fetch media from the preload manager](/media/media3/exoplayer/preloading-media/preloadmanager/manage-play#fetch-play) when the user starts playing an\n item. The preload manager gives your app a `MediaSource` for that content.\n\n | **Important:** The `MediaSource` returned by the preload manager must be played on an `ExoPlayer` created by that preload manager's builder.\n- [Release the preload manager](/media/media3/exoplayer/preloading-media/preloadmanager/manage-play#release) when you're done with it, freeing its\n resources.\n\nThe preload manager does the following:\n\n- It keeps track of all the media items your app has added to it.\n- Each time its priorities are invalidated, it queries your app by calling a [`TargetPreloadStatusControl`](/reference/androidx/media3/exoplayer/source/preload/TargetPreloadStatusControl) implemented by your app. It calls this to find out *how much* of each media item to load.\n- After it queries the app, it preloads the appropriate amount of each media item. The preload manager decides what order to load the item. It prioritizes items that are closest to the item the user's playing.\n- When the app requests content, the preload manager provides a `MediaSource` with whatever content has already been loaded.\n\n| **Note:** Your app decides *how much* of each media item should be preloaded, and tells the preload manager where each media item is in the carousel, as well as which item is currently playing. The preload manager decides what order to load the items, based on how close each item is to the item that's currently playing.\n\nPreload manager workflow\n\nThis section describes a typical workflow for an app that uses the preload\nmanager. In this example, we assume the app displays a carousel of short videos.\nThe selected video plays automatically, but the user can scroll the carousel in\neither direction, which stops the video that was playing and starts the video\nthey scroll to.\n\nAll these steps are discussed in detail in the following pages.\n\n1. [The app creates a](/media/media3/exoplayer/preloading-media/preloadmanager/create#create-tpsc) [*target preload status control*](/media/media3/exoplayer/preloading-media/preloadmanager/create#create-tpsc). The preload manager queries this control to find out how much of each media item to load.\n2. The app creates a `DefaultPreloadManager.Builder`, and passes the target preload status control. The app then [uses the builder to create the preload\n manager](/media/media3/exoplayer/preloading-media/preloadmanager/create#create-dpm).\n3. The app [adds media items to the preload manager](/media/media3/exoplayer/preloading-media/preloadmanager/manage-play#add-items). The app provides an *index* for each item, specifying the item's position in the carousel.\n4. After all the media is added, [the app calls](/media/media3/exoplayer/preloading-media/preloadmanager/manage-play#invalidate) [`invalidate()`](/media/media3/exoplayer/preloading-media/preloadmanager/manage-play#invalidate) to tell the preload manager to set the priorities for each item then preload them.\n5. For each media item, the preload manager calls the target preload control to query how much of the item should be loaded. The target preload control might say to load a certain duration of content, just fetch the item's metadata, or not fetch any of that item at this time. After the preload manager has gotten this information, it starts loading the media content.\n6. When the user starts playing content, [the app calls the preload manager](/media/media3/exoplayer/preloading-media/preloadmanager/manage-play#fetch-play) to request a `MediaSource` for that media item. The app also calls `setCurrentPlayingIndex()` to tell the preload manager which media item is being played.\n7. If the user moves to a different media item, the app requests that item from the preload manager, and also updates the current playing index. It then calls `invalidate()` again to tell the preload manager to update its priorities based on what's now being played.\n8. If the app adds or removes media items to the carousel, it also adds or removes those items to the preload manager, and calls `invalidate()` when it's done doing that.\n9. Whenever the preload manager's priorities are invalidated, it calls the target preload control once again to find out how much of each item to load.\n10. When the app closes the carousel, it [releases the preload manager](/media/media3/exoplayer/preloading-media/preloadmanager/manage-play#release) to free its resources."]]