4

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 ??

Bharat
  • 904
  • 6
  • 19

3 Answers3

5

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();
    }
}
danik
  • 796
  • 9
  • 17
  • great library by the way , i was just wondering if you can guide me on how i can apply encryption , i read your comment on [this](https://github.com/danikula/AndroidVideoCache/issues/71#issuecomment-301775416) issue but it wasn't clear for me how to implement it , thanks in advance. – Mohammed Elrashied Nov 16 '17 at 09:59
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 (...);

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

sam
  • 56
  • 2
0

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.

Dmila Ram
  • 1,054
  • 1
  • 13
  • 19