I don't know how to do. Please suggest how to download a large video file in parts and play all parts one by one.
Actually I have to stream FTP large file in Android VideoView
. I have searched a lot and found that android do not support FTP streaming.
So, I tried to download the file in multiple parts and play them one by one. But the problem is that only first part of file plays, others don't. Please suggest.
Code to download file in parts.
URL url = new URL(fileUrl);
URLConnection ucon = url.openConnection();
InputStream is = ucon.getInputStream();
BufferedInputStream inStream = new BufferedInputStream(is, 1024 * 5);
FileOutputStream outStream = new FileOutputStream(fileName);
byte[] buff = new byte[5 * 1024];
int len;
int maxLenth = 10000; //Some random value
int counter = 0;
//Read bytes (and store them) until there is nothing more to read(-1)
while ((len = inStream.read(buff)) != -1) {
outStream.write(buff, 0, len);
counter ++;
Log.d(TAG, "Counter: " + counter);
if(counter == maxLenth) {
counter = 0;
outStream.flush();
outStream.close();
fileName = new File(directory.getAbsolutePath() + "/"
+ context.getResources().getString(R.string.app_name) + "_"
+ System.currentTimeMillis() + "." + format);
fileName.createNewFile();
outStream = new FileOutputStream(fileName);
}
}
And when the files are downloaded, I tried to update the list to play videos when at least 1 file is downloaded. listFiles contains list of all the dowloaded files.
Uri uri = Uri.parse(listFiles.get(0));
videoView.setVideoURI(uri);
videoView.start();
videoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mediaPlayer) {
counter ++;
if(counter < listFiles.size()) {
Uri uri = Uri.parse(listFiles.get(counter));
videoView.setVideoURI(uri);
videoView.start();
}
}
});
I don't know why the downloaded files after file 1 don't play. They don't play even in the PC. I don't know what wrong am I doing.
I tried a lot of things and none of them worked. Android APIs currently doesn't support this feature. So I have to rely on native code to decode the video and then encode.
To do that I will have to learn few things before such as JNI and NDK. Then I will have to apply native code. I have heard of a good library that supports these features ffmpeg which provides many features. But currently I am engaged in another project so I could not try that library and all these things.