Get everything in from ExoPlayer Cache

泪湿孤枕 提交于 2019-12-05 04:09:28

问题


Using https://google.github.io/ExoPlayer/doc/reference/com/google/android/exoplayer2/upstream/cache/CacheDataSourceFactory.html is there a way to grab all the MediaSources that have been cached?


回答1:


The Cache offers no convenient API to get all completely cached URIs or similar.

If you create your own CacheEvictor (for instance by wrapping a LeastRecentlyUsedCacheEvictor) you can do your own book keeping when spans are added or removed and then delegate to the LRUCacheEvictor. This way you can maintain a list of cached URLs.

You can check what portions for a given uri is cached:

// create the data spec of a given media file
Uri uri = Uri.parse("http://cool.stuff.com/song-123.mp3")
DataSpec dataSpec = new DataSpec(uri);
// get information about what is cached for the given data spec
CacheUtil.CachingCounters counters = new CacheUtil.CachingCounters();
CacheUtil.getCached(dataSpec, cache, counters);
if (counters.contentLength == counters.totalCachedBytes()) {
  // all bytes cached
} else if (counters.totalCachedBytes() == 0){
  // not cached at all
} else {
  // partially cached
}

If the data for a given uri is only partially cached you can check what spans are available like this:

NavigableSet<CacheSpan> cachedSpans =
   cache.getCachedSpans(CacheUtil.generateKey(uri));


来源:https://stackoverflow.com/questions/48087752/get-everything-in-from-exoplayer-cache

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!