【问题标题】:ExoPlayer cacheExoPlayer 缓存
【发布时间】:2014-11-23 13:43:59
【问题描述】:

我正在准备使用 ExoPlayer 通过 http 播放视频。我想在视频加载后保存视频并从缓存中播放。如何从缓存中实现缓存和回放?可以给我任何样品。

【问题讨论】:

  • 我认为没有默认行为可以轻松做到这一点。它是开源的。你可以 fork 并更改缓存的下载位置。
  • 我也有同样的问题。你有办法实现这个吗?
  • @fisher3421 你有什么我也在寻找的解决方案吗?

标签: android exoplayer


【解决方案1】:

这个库很容易使用:https://github.com/danikula/AndroidVideoCache。您只需要在 appcontroller 的 repo 中找到初始化代码即可。

对于那些使用 mediaitem 的人,您可以这样做:

exoPlayer = new SimpleExoPlayer.Builder(context).build();
    holder.exoPlayerView.setPlayer(exoPlayer);
    HttpProxyCacheServer proxyServer = AppController.getProxy(context);
    String streamingURL = shortVideosRecommendationsArrayList.get(holder.getAbsoluteAdapterPosition()).getStreamingURL();
    String proxyURL = proxyServer.getProxyUrl(streamingURL);
  
    MediaItem mediaItem = MediaItem.fromUri(proxyURL);
    exoPlayer.setMediaItem(mediaItem);
    exoPlayer.setRepeatMode(exoPlayer.REPEAT_MODE_ALL);
    exoPlayer.prepare();
    exoPlayer.setPlayWhenReady(true);

【讨论】:

    【解决方案2】:

    我在这个库中使用 exoplayer: https://github.com/danikula/AndroidVideoCache 它可以帮助您缓存来自资源(URL、文件)的视频...

    这是我的示例代码:

    String mediaURL = "https://my_cool_vid.com/vi.mp4";
    SimpleExoPlayer exoPlayer = ExoPlayerFactory.newSimpleInstance(getContext());
    HttpProxyCacheServer proxyServer = HttpProxyCacheServer.Builder(getContext()).maxCacheSize(1024 * 1024 * 1024).build();
    
    String proxyURL = proxyServer.getProxyUrl(mediaURL);
    
    
    DataSource.Factory dataSourceFactory = new DefaultDataSourceFactory(getContext(),
                    Util.getUserAgent(getContext(), getActivity().getApplicationContext().getPackageName()));
    
    
    exoPlayer.prepare(new ProgressiveMediaSource.Factory(dataSourceFactory)
                    .createMediaSource(Uri.parse(proxyURL)););
    

    【讨论】:

      【解决方案3】:

      您使用使用缓存和数据源创建的 cacheDataSource。这个 cacheDataSource 然后被 ExtractorSampleSource 使用。下面是 audioRenderer 的代码,videoRender 也可以这样做;传递给 exoplayerInstance.prepare(renderers)。

      Cache cache = new SimpleCache(mCtx.getCacheDir(), new LeastRecentlyUsedCacheEvictor(1024 * 1024 * 10));
      DataSource dataSource = new DefaultUriDataSource(mCtx, "My Player");
      CacheDataSource cacheDataSource = new CacheDataSource(cache, dataSource, false, false);
      Allocator allocator = new DefaultAllocator(BUFFER_SEGMENT_SIZE);
      ExtractorSampleSource extractorSampleSource = new ExtractorSampleSource(trackURI, cacheDataSource, allocator, BUFFER_SEGMENT_COUNT*BUFFER_SEGMENT_SIZE, new Mp3Extractor());
      MediaCodecAudioTrackRenderer audioTrackRenderer = new MediaCodecAudioTrackRenderer(extractorSampleSource);
      

      【讨论】:

      • 这是让缓存工作的整个实现吗?我已经实现了这一点,但无论我做什么,打印cache.getCacheSpace() 都会显示 0。填充缓存的任何想法或提示?
      【解决方案4】:

      你使用的是什么协议 mpeg-dash 或普通 http。

      您可以覆盖 HttpDataSource 并将传入字节写入文件,并在再次播放时检查文件是否存在于所需位置并更改从您的文件而不是 HttpDataSource 馈送到播放器的 InputStream。

      【讨论】:

      • 覆盖 HttpDataSource 是错误的方法,因为 ExoPlayer 具有用于缓存实现的目标类。例如缓存数据源。我想正确地使用这些类。
      • 我为此目的编写了数据源,但下载的文件比预期的要大一些 KB。我不知道为什么。
      猜你喜欢
      • 2019-03-18
      • 1970-01-01
      • 2015-04-26
      • 1970-01-01
      • 1970-01-01
      • 2015-03-16
      • 1970-01-01
      • 2018-06-13
      • 1970-01-01
      相关资源
      最近更新 更多