我正在使用ExoPlayer 2播放远程曲目。默认情况下,播放器逐个加载曲目(即大约20秒,然后在曲目播放时另外20秒)。
由于曲目是从远程服务器加载的,因此如果连接断开,播放器将无法再加载。有没有办法说ExoPlayer加载音频文件的更大部分(也是一次完整的轨道)?
我试图看看ExtractorMediaSource
,DataSource.Factory
和DefaultExtractorsFactory
,但我找不到解决我的问题。
val audioSource = ExtractorMediaSource(
Uri.parse(videoUrl),
mDataSourceFactory, // DataSource.Factory
mExtractor, // DefaultExtractorsFactory
null,
null
)
mExoPlayer.prepare(audioSource)
mExoPlayer.playWhenReady = true
(它是Kotlin,但Java程序员似乎也可以理解)
我找到了解决方案。由于我没有找到其他相关问题,我会回答我的问题(我希望将来有人会需要它):
要配置的正确对象是在创建LoadControl
对象时传递给ExoPlayerFactory
的ExoPlayer
:
val loadControl = DefaultLoadControl(
DefaultAllocator(true, C.DEFAULT_BUFFER_SEGMENT_SIZE),
5 * 60 * 1000, // this is it!
10 * 60 * 1000,
DefaultLoadControl.DEFAULT_BUFFER_FOR_PLAYBACK_MS.toLong(),
DefaultLoadControl.DEFAULT_BUFFER_FOR_PLAYBACK_AFTER_REBUFFER_MS.toLong()
)
mExoPlayer = ExoPlayerFactory.newSimpleInstance(this, trackSelector, loadControl)
The doc在哪里解释。
@Massimo的答案是正确的,但方法已被弃用
使用这样的东西
DefaultLoadControl.Builder()
.setBufferDurationsMs(
DefaultLoadControl.DEFAULT_MIN_BUFFER_MS,
30_000,
DefaultLoadControl.DEFAULT_BUFFER_FOR_PLAYBACK_MS,
DefaultLoadControl.DEFAULT_BUFFER_FOR_PLAYBACK_AFTER_REBUFFER_MS
)