[英]After upgrading ExoPlayer from 2.11.8 to 2.12.1 and replacing deprecated components I can't play any video
I've recently upgraded com.google.android.exoplayer:exoplayer-core
from version 2.11.8 to version 2.12.1.我最近将
com.google.android.exoplayer:exoplayer-core
从 2.11.8 版升级到 2.12.1 版。
Some components are deprecated by version 2.12.1 in the following code snippet.以下代码片段中的某些组件已被版本 2.12.1 弃用。
cacheEvictor = LeastRecentlyUsedCacheEvictor(CACHE_SIZE_MAX)
databaseProvider = ExoDatabaseProvider(context)
cache = SimpleCache(File(context.cacheDir, CACHE_DIR),
cacheEvictor, databaseProvider)
upstreamFactory = DefaultDataSourceFactory(context, USER_AGENT)
// CacheDataSourceFactory class is deprecated
cacheFactory = CacheDataSourceFactory(cache, upstreamFactory,
CacheDataSource.FLAG_BLOCK_ON_CACHE or
CacheDataSource.FLAG_IGNORE_CACHE_ON_ERROR)
mediaSourceFactory = ProgressiveMediaSource.Factory(cacheFactory)
[...]
val url: Uri
val player: SimpleExoPlayer
[...]
// createMediaSource method is deprecated
val mediaSource = mediaSourceFactory.createMediaSource(uri)
// prepare method is deprecated
player.prepare(mediaSource, true, true)
Thus I replaced the deprecated components by the newest ones.因此,我用最新的组件替换了已弃用的组件。
cacheEvictor = LeastRecentlyUsedCacheEvictor(CACHE_SIZE_MAX)
databaseProvider = ExoDatabaseProvider(context)
cache = SimpleCache(File(context.cacheDir, CACHE_DIR),
cacheEvictor, databaseProvider)
upstreamFactory = DefaultDataSourceFactory(context, USER_AGENT)
cacheFactory = CacheDataSource.Factory().apply {
setCache(this@VideoPlayer.cache)
setUpstreamDataSourceFactory(upstreamFactory)
setFlags(CacheDataSource.FLAG_BLOCK_ON_CACHE or
CacheDataSource.FLAG_IGNORE_CACHE_ON_ERROR)
}
mediaSourceFactory = ProgressiveMediaSource.Factory(cacheFactory)
[...]
val url: Uri
val player: SimpleExoPlayer
[...]
val mediaItem = MediaItem.fromUri(uri)
val mediaSource = mediaSourceFactory.createMediaSource(mediaItem)
player.setMediaSource(mediaSource, true)
// The following missed
player.prepare(mediaSource)
After the updates, the player was black and I could not play any video (nothing usefull in Logcat).更新后,播放器是黑色的,我无法播放任何视频(在 Logcat 中没有任何用处)。 I've returned to the deprecated components (still with version 2.12.1) and now I can play every video.
我已经回到不推荐使用的组件(仍然使用版本 2.12.1),现在我可以播放每个视频。
I think I messed up something when in replacing the deprecated components.我想我在更换已弃用的组件时搞砸了一些事情。
Can anyone help me, please?任何人都可以帮助我吗?
I've just fixed the code in the answer: I had forgotten the last player.prepare()
.我刚刚修复了答案中的代码:我忘记了最后一个
player.prepare()
。
Now it works fine.现在它工作正常。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.