我已经为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);
Android视频缓存库可完全满足您的需求。请按照以下步骤缓存视频。
步骤1:编译'com.danikula:videocache:2.7.1'
步骤2:将共享代理存储在您的Application类中
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类放入清单文件中,如
<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);
快乐编码:)