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

时间:2014-04-17 13:37:45

标签: android video-streaming android-mediaplayer progressive-download

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

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

3 个答案:

答案 0 :(得分:4)

只需使用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();
    }
}

答案 1 :(得分: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/

答案 2 :(得分:0)

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