在第二项活动后退导航后如何在不缓冲的情况下播放视频?

时间:2019-06-18 08:09:37

标签: android android-videoview

我正在编写用于背景视频播放的代码,因为我正在使用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();

因此,每当我开始新活动并向后浏览时,视频都会重新启动,并且需要一些时间来重新加载。有什么办法可以减少缓冲时间。

1 个答案:

答案 0 :(得分:0)

您可以将缓存用于该类:

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应该是单例的,否则它将覆盖缓存   每次。

现在在您的活动中使用它

 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);

这将像护身符一样工作。