如何将ExoPlayer的MediaSource转换为普通的Java InputStream?

时间:2019-06-23 07:00:07

标签: caching android-webview hybrid-mobile-app exoplayer

我想使用ExoPlayer的缓存库,而无需实际在ExoPlayer上播放媒体。 (视频将通过HTML元素在WebView中呈现。)目标是实现持久性缓存,以便应用程序可以在互联网连接失败时正常处理。问题是我正在覆盖的WebViewClient.shouldInterceptRequest(...)钩子需要一个InputStream作为响应的一部分,而MediaSource是这样的高级抽象,我还没有想出要得到的底层字节。

Exoplayer实际上是我第四次尝试将强大的缓存库添加到系统中。

  1. 第一个是我用于测试的简单内存中类。它 显示系统正在运行,但不是持久的。

  2. 接下来,我构建了自己的简单缓存库,该库对于 小文件,但不是大视频文件(这里我只是在做 渐进式下载/ mp4文件)。

  3. 我尝试了https://github.com/danikula/AndroidVideoCache,这是 有趣的是,它如何启动代理缓存服务器来完成其工作, 但是随着套接字关闭,它抛出了很多IOExceptions。同样,它只是一个直写式缓存。我需要选择性地缓存和刷新内容... Internet连接期间播放的内容与故障转移所需存储的内容不同。

  4. 最后,我嫁接到了exoplayer的缓存子系统中。好像有 我需要的一切...直到我意识到MediaSource不会 轻松允许从中传输数据。

这是我想要将持久性缓存嫁接到WebViewClient代码中的示意图。请注意我确实需要建议的INSERT_MAGIC_HERE()通话。

public class MyWebviewFragment extends Fragment { 

    private WebView webView

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // ... 
        webView = view.findViewById(R.id.web_view);
        webView.setWebViewClient(new WebViewClient() { 

            // blocking call from a non-UI thread.
            // returning null means call is not intercepted and Chromium engine handles it like normal.
            @Override
            public WebResourceResponse shouldInterceptRequest(final WebView view, WebResourceRequest request) {
               // ... 
               // here I inspect the request and internet connectivity to 
               // update the persistent cache

               // If I want to intercept the call and used cached data, I 
               // need to return a WebResourceResponse packed with an inputStream 
               MediaSource mediaSource = ProgressiveMediaSource.Factory(...).createMediaSorce(request.uri);

               InputStream istream = INSERT_MAGIC_HERE(mediaSource)

               return new WebResourceResponse(..., iStream, ...)
            }
        });
    }

}

或者,如果这超出了ExoPlayer的功能,我也欢迎提出其他建议。

0 个答案:

没有答案