gpt4 book ai didi

android - 为 exoplayer 启用缓存

转载 作者:行者123 更新时间:2023-12-04 23:54:22 27 4
gpt4 key购买 nike

我已经为 hls 视频链接实现了 exoplayer,一旦视频再次播放,它就会加载要播放的视频,任何人都可以建议如何在视频完全流式传输后停止再次加载并在不缓冲的情况下播放。如何为hls流媒体视频存储缓存。如有解决方案,请提供解决方案。在此先感谢:)

 TrackSelector trackSelector = new DefaultTrackSelector(this);

DefaultLoadControl loadControl = new DefaultLoadControl.Builder()
.setBufferDurationsMs(1024, 64 * 1024, 1024, 1024)
.createDefaultLoadControl();


videoView = findViewById(R.id.video_view);
player = ExoPlayerFactory.newSimpleInstance(this, trackSelector,loadControl);

//player = ExoPlayerFactory.newSimpleInstance(this);

    player.setPlayWhenReady(true);
videoView.setPlayer(player);

DataSource.Factory dataSourceFactory = new DefaultDataSourceFactory(this,
Util.getUserAgent(this, "ExoPlayer"));

// Produces Extractor instances for parsing the media data.
ExtractorsFactory extractorsFactory = new DefaultExtractorsFactory();


MediaSource mediaSource = new HlsMediaSource.Factory(dataSourceFactory).createMediaSource(Uri.parse(videoUrl));

player.prepare(mediaSource);
player.setPlayWhenReady(true);

最佳答案

方法一:使用Exoplayer缓存策略

第 1 步:实现 Exoplayer

implementation 'com.google.android.exoplayer:exoplayer-core:2.15.0'
implementation 'com.google.android.exoplayer:exoplayer-ui:2.15.0'

第 2 步:在您的应用程序类中创建缓存策略

public SimpleCache simpleCache;
@Override
public void onCreate() {
super.onCreate();

LeastRecentlyUsedCacheEvictor leastRecentlyUsedCacheEvictor = new LeastRecentlyUsedCacheEvictor(100 * 1024 * 1024);
if (simpleCache == null) {
simpleCache = new SimpleCache(getCacheDir(), leastRecentlyUsedCacheEvictor, new ExoDatabaseProvider(this));
}
}

第 3 步:像下面的方法一样缓存内容

Uri videoUri = Uri.parse("YOUR URL");
MediaItem mediaItem = MediaItem.fromUri(videoUri);
DefaultHttpDataSource.Factory httpDataSourceFactory = new DefaultHttpDataSource.Factory().setAllowCrossProtocolRedirects(true);
DefaultDataSource.Factory defaultDataSourceFactory = new DefaultDataSourceFactory(requireContext(), httpDataSourceFactory);
CacheDataSource.Factory cacheDataSourceFactory = new CacheDataSource.Factory()
.setCache(MyApplication.getAppInstance().simpleCache)
.setUpstreamDataSourceFactory(defaultDataSourceFactory)
.setFlags(CacheDataSource.FLAG_IGNORE_CACHE_ON_ERROR);

MediaSource mediaSource = new ProgressiveMediaSource.Factory(cacheDataSourceFactory)
.createMediaSource(mediaItem);
player.setMediaSource(mediaSource, true);

方法二:Android Video Cache Library 做你想做的。按照以下步骤缓存您的视频。

第 1 步:实现 'com.danikula:videocache:2.7.1'

第 2 步:在您的应用程序类中存储共享代理

public class MyApplication extends Application {
private HttpProxyCacheServer proxy;
public static HttpProxyCacheServer getProxy(Context context) {
MyApplication app = (MyApplication) context.getApplicationContext();
return app.proxy == null ? (app.proxy = app.newProxy()) : app.proxy;
}

private HttpProxyCacheServer newProxy() {
return new HttpProxyCacheServer.Builder(this)
.maxCacheSize(1024 * 1024 * 1024)
.build();
//return new HttpProxyCacheServer(this);

}
}

第 3 步:将 MyApplication 类放入 list 文件中,例如

<application
android:name=". MyApplication">
.
.
.
</application>

第 4 步:使用来自代理的 url 而不是原始 url 来添加缓存

HttpProxyCacheServer proxy = MyApplication.getProxy(activity);
String proxyUrl = proxy.getProxyUrl(VIDEO_URL);
videoView.setVideoPath(proxyUrl);

如果你正在使用exoplayer

HttpProxyCacheServer proxy = getProxy(activity);
String proxyUrl = proxy.getProxyUrl(VIDEO_URL);
PlayerView playerView = findViewById(R.id.video_view);
ExoPlayer player = ExoPlayerFactory.newSimpleInstance(VideoActivity.this,
new DefaultRenderersFactory(this),
new DefaultTrackSelector());
MediaSource mediaSource = buildMediaSource(proxyUrl);
player.prepare(mediaSource, true, false);
playerView.setPlayer(player);

快乐编码:)

关于android - 为 exoplayer 启用缓存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59944403/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com