Java Android将下载视频的部分内容与Play结合起来 [英] Java Android download video in parts and Play combined

查看:119
本文介绍了Java Android将下载视频的部分内容与Play结合起来的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道该怎么做。请建议如何下载大型视频文件并逐个播放所有部分。
其实我必须在Android VideoView 中传输FTP大文件。我搜索了很多,发现android不支持FTP流式传输。



所以,我尝试将这个文件分成多个部分下载并逐一播放。但问题是只有文件的第一部分起作用,而其他部分则不起作用。请推荐。



代码下载文件的部分内容。

  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 =新字节[5 * 1024];
int len;

int maxLenth = 10000; //一些随机值
int counter = 0;

//读取字节(并存储它们)直到没有更多内容要读取(-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);
}
}

当文件被下载时,我尝试更新下载至少1个文件时播放视频的列表。 listFiles包含所有dowloaded文件的列表。

  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() ;
}
}
});

我不知道为什么文件1下载的文件无法播放。即使在个人电脑中也不玩。我不知道我在做什么错。

解决方案

我尝试了很多东西,但都没有成功。 Android API目前不支持此功能。所以我必须依靠本地代码来解码视频然后进行编码。

要做到这一点,我必须学习一些东西,比如 JNI and NDK 。然后我将不得不应用本地代码。我听说过一个很好的图书馆,它支持 ffmpeg ,它提供了许多功能。但是现在我正在参与另一个项目,所以我无法尝试图书馆和所有这些东西。


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.

这篇关于Java Android将下载视频的部分内容与Play结合起来的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆