4
votes

In my Application, I am showing videos to user from an online source in progressive manner (I have an Http url for every video). It is quite possible with SurfaceView and MediaPlayer in combination and also with VideoView as an alternative. But I want to keep the streamed video available in the cache for the future uses [without making any separate call for downloading video as its already being downloaded by MediaPlayer or VideoView while streaming]

Any Idea about saving video from stream buffered by MediaPlayer ??

3
those who are down-voting the question should comment reason for down-voting. - Bharat

3 Answers

5
votes

Just use 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
votes
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 (...);

Please refer this link also http://blog.pocketjourney.com/2009/12/27/android-streaming-mediaplayer-tutorial-updated-to-v1-5-cupcake/

0
votes

i also needed something like this and i found a great library, hope this link to be helpful. its very great and easy to use.