How to play video without buffering after back-navigation from second activity?

Viewed 1678

I am writing a code for background video play, for that I'm using VideoView. I'm getting the video URL from server and then parsing it to videoView. Obvious reason, it will take time to load.

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

So when whenever I start my new activity and navigate back, video restarts and takes time to reload. Is there any way to minimize the buffer-time.

1 Answers

you can use cache for that use this class:

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.

now use it in your activity like this

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

This will work like a charm.

Related