我正在编写后台视频播放的代码,为此我使用VideoView
。我从服务器获取视频 URL,然后将其解析为 videoView
。原因很明显,加载需要时间。
private String videoViewUrl = null;
private VideoView videoView;
private MediaPlayer mp;
networkManager.getRequest(JSON_URL, new NetworkCallback() {
@Override
public void onSuccess(String result) {
try {
JSONObject obj = new JSONObject(result);
JSONObject themeObj = obj.getJSONObject("Theme");
JSONObject videoObj = themeObj.getJSONObject("Video");
videoViewUrl = videoObj.getString("VideoUrl");
// parse to videoview
videoView.setVideoURI(Uri.parse(videoViewUrl));
videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mediaPlayer) {
mp = mediaPlayer;
mediaPlayer.setLooping(true);
}
});
videoView.start();
@Override
protected void onResume() {
if (videoView != null)
videoView.resume();
super.onResume();
@Override
protected void onPause() {
if (videoView != null)
videoView.suspend();
super.onPause();
因此,每当我开始新 Activity 并返回时,视频就会重新启动并需要一些时间重新加载。有什么办法可以最大限度地减少缓冲时间。
最佳答案
您可以使用缓存来使用此类:
public class CacheDataSourceFactory implements DataSource.Factory {
private final Context context;
private final DefaultDataSourceFactory defaultDatasourceFactory;
private final long maxFileSize, maxCacheSize;
private static SimpleCache simpleCache;
public static SimpleCache getInstance(Context c, long maxCacheSize) {
if (simpleCache == null) simpleCache = new SimpleCache(new File(c.getCacheDir(), "exoCache"), new LeastRecentlyUsedCacheEvictor(maxCacheSize));
return simpleCache;
}
public CacheDataSourceFactory(Context context, long maxCacheSize, long maxFileSize) {
super();
this.context = context;
this.maxCacheSize = maxCacheSize;
this.maxFileSize = maxFileSize;
String userAgent = Util.getUserAgent(context, context.getString(R.string.app_name));
DefaultBandwidthMeter bandwidthMeter = new DefaultBandwidthMeter();
defaultDatasourceFactory = new DefaultDataSourceFactory(this.context,
bandwidthMeter,
new DefaultHttpDataSourceFactory(userAgent, bandwidthMeter));
}
@Override
public DataSource createDataSource() {
return new CacheDataSource(CacheDataSourceFactory.getInstance(context, maxCacheSize), defaultDatasourceFactory.createDataSource(),
new FileDataSource(), new CacheDataSink(simpleCache, maxFileSize),
CacheDataSource.FLAG_BLOCK_ON_CACHE | CacheDataSource.FLAG_IGNORE_CACHE_ON_ERROR, null);
}}
SimpleCache should be singleton other wise it will override the cache everytime.
现在像这样在您的 Activity 中使用它
BandwidthMeter bandwidthMeter = new DefaultBandwidthMeter();
TrackSelection.Factory videoTrackSelectionFactory =
new AdaptiveTrackSelection.Factory(bandwidthMeter);
TrackSelector trackSelector = new DefaultTrackSelector(videoTrackSelectionFactory);
exoPlayer = ExoPlayerFactory.newSimpleInstance(this, trackSelector);
MediaSource mSource = new ExtractorMediaSource(Uri.parse(videoViewUrl),
new CacheDataSourceFactory(context, 100 * 1024 * 1024, 5 * 1024 * 1024), new DefaultExtractorsFactory(), null, null);
exoPlayerView.setResizeMode(AspectRatioFrameLayout.RESIZE_MODE_FILL);
exoPlayerView.setPlayer(exoPlayer);
exoPlayer.setRepeatMode(Player.REPEAT_MODE_ALL); // if you want
exoPlayer.prepare(mSource);
exoPlayer.setPlayWhenReady(true);
这就像一个魅力。
关于android - 如何从第二个 Activity 返回导航后不缓冲地播放视频?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56644540/