如何从播放列表中预缓存下一个视频的前100kb,以便在exoplayer中顺利播放?

k7fdbhmy  于 2021-07-03  发布在  Java
关注(0)|答案(0)|浏览(448)

我已经用viewpager2实现了exoplayer。当用户向上或向下滑动时,播放器需要几秒钟来加载下一个项目。
为了消除两个视频之间的延迟,我想提前预切下一个视频。
我正在使用exoplayer 2.12.0
我已将exoplayer初始化为:

cacheDataSourceFactory = new CacheDataSource.Factory()
                    .setCache(CacheHelper.getCache(this))
                    .setUpstreamDataSourceFactory(httpDataSourceFactory);

            player = new SimpleExoPlayer.Builder(this)
                    .setMediaSourceFactory(
                            new DefaultMediaSourceFactory(cacheDataSourceFactory))
                    .setLoadControl(loadControlBuilder.build())
                    .build();

            videoView.setPlayer(player);
            currentWindow = 0;

            viewPager.setCurrentItem(currentWindow, false);

            player.addListener(playbackStateListener);
            player.addAnalyticsListener(new EventLogger(trackSelector));
            player.setRepeatMode(Player.REPEAT_MODE_OFF);

然后将媒体项添加到播放机中,如下所示:

final DownloadHelper downloadHelper =
                    DownloadHelper.forMediaItem(
                            PlayerActivity.this,
                            mediaItem,
                            new DefaultRenderersFactory(this),
                            httpDataSourceFactory);
            downloadHelper.prepare(new DownloadHelper.Callback() {
                @Override
                public void onPrepared(DownloadHelper helper) {
                    DownloadRequest downloadRequest = helper.getDownloadRequest(
                            Util.getUtf8Bytes(mediaItem.playbackProperties.uri.toString()));
                    if (!downloadedVideoItems.contains(shortVideo) && player != null) {
                        startDownload(downloadRequest);
                        player.addMediaItem(downloadRequest.toMediaItem());
                        downloadedVideoItems.add(shortVideo);
                        pagerAdapter.update(downloadedVideoItems);
                        // only play if there is first video.
                        if (videoDownloadIndex == 0) {
                            startPlayer();
                            dialog.hide();
                        }
                        videoDownloadIndex++;
                        Log.d(TAG, "setMediaItemToPlayer: video added : " + shortVideo.videoName);
                    }

                    // update video download index
                    Log.d(TAG, "setMediaItemToPlayer: videoDownloadIndex:" + videoDownloadIndex +
                            " currentWindow:" + currentWindow);

                    checkForDownload();
                }

                @Override
                public void onPrepareError(DownloadHelper helper, IOException e) {
                }
            });

cachehelper供参考:

/*************************************Cache helper****************************************/

public class CacheHelper {

    private static final long MAX_BYTES = 1024 * 1024 * 100;
    private static Cache cache;

    private static final String CACHE_DIR = "downloads";

    public static synchronized Cache getCache(Context context) {
        if (cache == null) {
            File cacheDir = new File(context.getExternalCacheDir(), CACHE_DIR);

            cache = new SimpleCache(cacheDir, new LeastRecentlyUsedCacheEvictor(MAX_BYTES),
                    ExoHelper.getExoDatabaseProvider(context));
        }
        return cache;
    }

    public static void deleteCache(Context context) {
        File cacheDir = new File(context.getExternalCacheDir(), CACHE_DIR);
        deleteDir(cacheDir);
    }

    private static boolean deleteDir(File dir) {
        if (dir != null && dir.isDirectory()) {
            String[] children = dir.list();
            for (String child : children != null ? children : new String[0]) {
                boolean success = deleteDir(new File(dir, child));
                if (!success) {
                    return false;
                }
            }
            return dir.delete();
        } else if (dir != null && dir.isFile()) {
            return dir.delete();
        } else {
            return false;
        }

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题