4

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

关于从 MediaPlayer 缓冲的流中保存视频的任何想法?

4

3 回答 3

5

只需使用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();
    }
}
于 2015-05-11T20:11:43.360 回答
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/

于 2014-04-28T08:49:56.737 回答
0

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

于 2016-01-18T19:27:07.663 回答