【问题标题】:How to precache a list of videos before playing them in a recyclerview of exoplayer?如何在 exoplayer 的 recyclerview 播放视频之前预先缓存视频列表?
【发布时间】:2020-07-16 12:45:26
【问题描述】:

我在 recyclerview 中使用 exoplayer 来呈现视频列表。这个rec​​yclerview是在一个activity中渲染的,我在打开activity之前就知道了视频url列表。

我希望能够在进行该活动之前预加载或缓存视频。视频通常少于 1 分钟。所以我不是在寻找平滑流视频的解决方案。我只想在打开活动之前将视频放在缓存中,这样一旦 recyclerview 打开视频就可以开始播放而没有任何缓冲,就像在 tiktok 中一样。

我找到了一种使用LocalCacheDataSourceFactory in 缓存已播放视频的方法

MediaSource videoSource =
 new ExtractorMediaSource(Uri.parse(mediaUrl),
                            new LocalCacheDataSourceFactory(context, 100 * 1024 * 1024, 5 * 1024 * 1024), new DefaultExtractorsFactory(), null, null);

这仅允许我缓存已经播放但不能预加载或预缓存的视频。

从 exoplayer 团队找到这篇中型文章,但没有针对我的特定要求的其他示例集成。 article

【问题讨论】:

  • 我也在为同样的问题工作,你的问题解决了吗?有的话请分享一下?

标签: java android exoplayer


【解决方案1】:

我在自己的应用中实现了这一点。请按以下步骤操作:

  1. 添加此视频缓存依赖项:

      implementation 'com.danikula:videocache:2.7.1'
    
  2. 像这样设置依赖: 建议您在应用程序类中这样做:

    private HttpProxyCacheServer proxy;

    public static HttpProxyCacheServer getProxy(Context context) {
        AppController app = (AppController) context.getApplicationContext();
        return app.proxy == null ? (app.proxy = app.newProxy()) : app.proxy;
    }
    private HttpProxyCacheServer newProxy() {
        return new HttpProxyCacheServer(this);
    }

//Note that you need to have a single instance of HttpProxyCacheServer. 
//Note the library is tradinally meant to allow you to cache a video so exoplayer does not re-buffer it every single time you scroll to recycler item. 
  1. 然后像这样设置你的 exoplayer:

    一个。

    HttpProxyCacheServer proxyServer; // maintain a single instance of this. you can init this in oncreate method: 
    public ViewRecommendedShortVideosAdapter(Context context) {
    
        proxyServer = AppController.getProxy(context);
       }
    

    b.在您播放 exoplayer 文件的方法中:

    String streamingURL = shortVideo.getStreamingURL();
        String proxyURL = proxyServer.getProxyUrl(streamingURL); 
    
        MediaItem mediaItem = MediaItem.fromUri(proxyURL); // you are now crating the media item from the proxy url you get by calling getProxyUrl on your proxyServer instance. 
        exoPlayer.setMediaItem(mediaItem);
        exoPlayer.prepare();
        exoPlayer.setPlayWhenReady(true);
    

注意:上面仍然只给你缓存播放后的视频,而不是缓存尚未播放的视频。

  1. 要缓存尚未播放的视频,请执行以下操作:

    一个。您可以只缓存回收站数据数组列表中的下一个视频(这有助于节省用户的互联网成本),也可以缓存整个视频(我不推荐,因为用户的数据成本成本)。

    b.要缓存下一个视频,请创建一个方法,例如 prepareNextVideo(),这将包含缓存下一个视频的逻辑。您将在原始方法中调用此方法来播放 exoplayer 媒体项目。 (可能在设置第一个媒体项并设置 exoplayer.play() 之后)。

    c。 prepareNextVideo() 中的代码:

     private void prepareNextVideo(int currentPosition){
        URL url = null;
        try {
            ShortVideo shortVideo = videosArrayList.get(currentPosition+1); //you can put some if conditions here to check if the element exists. but a try cache should handle it to. 
    
            url = new URL(proxyServer.getProxyUrl(shortVideo.getStreamingURL())); // we are using our proxy url again. 
    
            InputStream inputStream = url.openStream();
            int bufferSize = 1024; 
            byte[] buffer = new byte[bufferSize];
            int length = 0;
            while ((length = inputStream.read(buffer)) != -1) {
                //nothing to do here. you are just reading the stream. 
            }
    
    
        } catch (Exception e) {
            e.printStackTrace();
    
            //can fetch
        }
    }
    

最后,只需调用此方法即可缓冲下一个视频。如果你想缓冲所有视频,你也可以把它放在一个循环中,异步运行。

【讨论】:

    【解决方案2】:

    查看这篇文章。我已经编写了使用 Exoplayer2 预加载/预缓存视频的分步指南。

    在您的情况下,您必须在后台线程内创建缓存方法的递归或循环。

    Video preloading/ precaching using Exoplayer 2 in Android

    【讨论】:

    • 我使用的是 exoplayer 2.7.3 版。我认为不支持 exodatabaseprovide。有没有其他选择?
    • prodev的解释很简短,很混乱,你真的需要用最简单的一步一步的方式重新编写,以便初学者理解..还包括github源代码。
    • 能否也提供一个Java教程
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-08-05
    • 1970-01-01
    • 2021-10-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-01
    相关资源
    最近更新 更多