首页 文章

来自http url的在线视频流与Android应用程序中的缓存

提问于
浏览
4

在我的应用程序中,我以渐进的方式从在线源向用户显示视频(我为每个视频都有一个Http网址) . SurfaceView和MediaPlayer结合使用,也可以与VideoView一起使用 . 但是我想保留缓存中的流媒体视频以供将来使用[无需单独调用下载视频,因为MediaPlayer或VideoView在流式传输时已经下载了该视频)

有关从MediaPlayer缓冲的流中保存视频的想法吗?

3 回答

  • 0

    只需使用AndroidVideoCache

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ...
        try {
            Cache cache = new FileCache(new File(getExternalCacheDir(), VIDEO_CACHE_NAME));
            HttpUrlSource source = new HttpUrlSource(VIDEO_URL);
            proxyCache = new HttpProxyCache(source, cache);
            videoView.setVideoPath(proxyCache.getUrl());
            videoView.start();
        } catch (ProxyCacheException e) {
            Log.e(LOG_TAG, "Error playing video", e);
        }
    }
    
    @Override
    public void onDestroy() {
        super.onDestroy();
    
        if (proxyCache != null) {
            proxyCache.shutdown();
        }
    }
    
  • 0
    URLConnection cn = new URL(mediaUrl).openConnection();   
    cn.connect();   
    InputStream stream = cn.getInputStream();
    String cacheDir = context.getCacheDir();
    File downloadingMediaFile = new File(cacheDir, "downloadingMedia.dat");
    FileOutputStream out = new FileOutputStream(downloadingMediaFile);   
    byte buf[] = new byte[16384];
    do {
    int numread = stream.read(buf);   
    if (numread <= 0) break;   
    out.write(buf, 0, numread);
    } while (...);
    

    请参考此链接http://blog.pocketjourney.com/2009/12/27/android-streaming-mediaplayer-tutorial-updated-to-v1-5-cupcake/

  • 4

    我也需要这样的东西,我找到了一个很棒的图书馆,希望这个link有所帮助 . 它非常好用且易于使用 .

相关问题