首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ExoPlayer中的视频缓存

ExoPlayer中的视频缓存
EN

Stack Overflow用户
提问于 2022-07-06 04:54:51
回答 1查看 539关注 0票数 0

我在/of ExoPlayer中搜索了缓存,但我既没有放弃它们,也无法正确地实现它们。

在这个被问及的问题中 for 在Exoplayer中使用缓存有一些答案和示例代码。我无法实现的第一个答案在示例代码的第2部分中,它说使用类DefaultHttpDataSourceFactory,我想它是用类DefaultHttpDataSource.Factory()修改的,这个类看起来并不是什么问题,但我面临的主要问题是这一行代码

代码语言:javascript
复制
BandwidthMeter bandwidthMeter = new DefaultBandwidthMeter();
TrackSelection.Factory videoTrackSelectionFactory =
    new AdaptiveTrackSelection.Factory(bandwidthMeter);
TrackSelector trackSelector = new DefaultTrackSelector(videoTrackSelectionFactory);

SimpleExoPlayer exoPlayer = ExoPlayerFactory.newSimpleInstance(this, trackSelector);
MediaSource audioSource = new ExtractorMediaSource(Uri.parse(url),
        new CacheDataSourceFactory(context, 100 * 1024 * 1024, 5 * 1024 * 1024), new DefaultExtractorsFactory(), null, null);
exoPlayer.setPlayWhenReady(true); 
exoPlayer.prepare(audioSource);

也就是说,使用类ExtractorMediaSource,它没有这个名称,我甚至在depricated类的系外播放器文档中都找不到。在2.18.0版本的系外播放器中,几乎每件事都发生了变化。例如,我们应该为系外玩家设置MediaSource的部分现在被分为两部分:

代码语言:javascript
复制
final DataSource.Factory dataSourceFactory = new DefaultDataSourceFactory(context, "agent name"); //part one
final ProgressiveMediaSource.Factory mediaSourceFactory = new ProgressiveMediaSource.Factory(dataSourceFactory); //part two

我希望你已经看到了这个解决方案的分歧和我的问题。

现在,下一个解决方案及其问题:这个亲爱的朋友建议使用这个图书馆,这听起来很好,以及如何解决我的问题,但是这个依赖compile 'com.danikula:videocache:2.7.1'实际上所做的是下载视频并将其保存在app文件夹中。由于android Pie(9)以及谷歌和android手机生产商的新隐私,他们不允许访问应用程序文件夹。

在Kotlin的其他解决方案中,也有同样的问题,系外玩家的类也没有了。

我想要的东西:我想在exoplayer2 (或exoplayer2的缓存)中使用缓存,比如在应用缓存中保存图像的Glide,我想为我的exoplayer2实现类似的功能,我几乎在任何地方都搜索过。

注意:我使用ExoPlayer的这些依赖项

代码语言:javascript
复制
implementation 'com.google.android.exoplayer:exoplayer-core:2.18.0'
implementation 'com.google.android.exoplayer:exoplayer-dash:2.18.0'
implementation 'com.google.android.exoplayer:exoplayer-ui:2.18.0'
implementation 'com.google.android.exoplayer:exoplayer:2.18.0'

我已经完成了我的应用程序,只是需要弄清楚它的这一部分。

EN

回答 1

Stack Overflow用户

发布于 2022-07-06 05:12:46

使用Builder类创建player对象,并将MediaSourceFactory设置为如下所示的缓存:

代码语言:javascript
复制
            val databaseProvider = StandaloneDatabaseProvider(context)
        context.getExternalFilesDir("voice/$artistId/$albumNumber")?.let {
            simpleCache = SimpleCache(
                it,
                NoOpCacheEvictor(),
                databaseProvider
            )
            val defaultDataSourceFactory = DefaultDataSource.Factory(context)
            val cacheDataSourceFactory = CacheDataSource.Factory()
                .setCache(simpleCache!!)
                .setUpstreamDataSourceFactory(defaultDataSourceFactory)
            player = ExoPlayer
                .Builder(context)
                .setMediaSourceFactory(DefaultMediaSourceFactory(cacheDataSourceFactory))
                .build()
        } ?: kotlin.run {
            player = ExoPlayer.Builder(context).build()
        }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72878109

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档