Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/185.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
android中的视频下载问题_Android_Video - Fatal编程技术网

android中的视频下载问题

android中的视频下载问题,android,video,Android,Video,我创建了10秒的视频,并上传到亚马逊服务器上,然后我就开始下载了 在listview中,像图像懒散加载一样。第一次下载需要超过6-8分钟。视频文件大小为6.89-7 MB。但在ios中,相同的视频需要30秒 正在下载我的要求与android中的vine应用程序相同。请帮助。 提前谢谢。 这是我的密码:- public class VideoLoader { VideoMemoryCache memoryCache = new VideoMemoryCache(); VideoF

我创建了10秒的视频,并上传到亚马逊服务器上,然后我就开始下载了 在listview中,像图像懒散加载一样。第一次下载需要超过6-8分钟。视频文件大小为6.89-7 MB。但在ios中,相同的视频需要30秒 正在下载我的要求与android中的vine应用程序相同。请帮助。 提前谢谢。 这是我的密码:-

public class VideoLoader {

    VideoMemoryCache memoryCache = new VideoMemoryCache();
    VideoFileCache fileCache;
    private Context context;
    private Bitmap mLoadingbmp;
    private Map<VideoView, String> imageViews = Collections
            .synchronizedMap(new WeakHashMap<VideoView, String>());
    ExecutorService executorService;
    Handler handler = new Handler();

    public VideoLoader(Context context) {

        fileCache = new VideoFileCache(context);
        this.context = context;

        executorService = Executors.newFixedThreadPool(5);

        mLoadingbmp = BitmapFactory.decodeResource(context.getResources(),
                R.drawable.loading);

    }
    public void DisplayImage(String url, VideoView videoView,
            Button defaultIMage,ProgressBar progressbar) {

        imageViews.put(videoView, url);
        File bitmap = memoryCache.get(url);

        if (bitmap != null && bitmap.length()>0) {

            videoView.setVideoPath(bitmap.getAbsolutePath());
            defaultIMage.setVisibility(View.VISIBLE);
            progressbar.setVisibility(View.GONE);
        } else {
            queuePhoto(url, videoView,defaultIMage,progressbar);

            defaultIMage.setVisibility(View.GONE);
            progressbar.setVisibility(View.VISIBLE);
        }

    }

    private void queuePhoto(String url, VideoView imageView,Button btn,ProgressBar pb) {
        PhotoToLoad p = new PhotoToLoad(url, imageView,btn,pb);
        executorService.submit(new PhotosLoader(p));
    }

    private class PhotoToLoad {
        public String url;
        public VideoView imageView;
        public Button defaultImage;
        ProgressBar progressbar;
        public PhotoToLoad(String u, VideoView  i,Button btn,ProgressBar pb) {
            url = u;
            imageView = i;
            defaultImage=btn;
            progressbar=pb;
        }
    }

    class PhotosLoader implements Runnable {
        PhotoToLoad photoToLoad;

        PhotosLoader(PhotoToLoad photoToLoad) {
            this.photoToLoad = photoToLoad;
        }

        @Override
        public void run() {
            try {
                if (imageViewReused(photoToLoad))
                    return;
                File bmp = getFile(photoToLoad.url);

                memoryCache.put(photoToLoad.url, bmp);

                if (imageViewReused(photoToLoad))
                    return;
        //      BitmapDisplayer bd = new BitmapDisplayer(bmp, photoToLoad);
        //      handler.post(bd);   

            } catch (Throwable th) {
                th.printStackTrace();
            }
        }
    }

    private File getFile(String path) {
        File f = fileCache.getFile(path);

        if (f.exists())
            return f;

        try {
            if (!URLUtil.isNetworkUrl(path)) {
            } else {
                URL url = new URL(path);
                HttpURLConnection cn = (HttpURLConnection) url.openConnection();
                cn.setReadTimeout(5000);
                cn.setConnectTimeout(30000);
                cn.setInstanceFollowRedirects(true);
                cn.connect();
                InputStream stream = cn.getInputStream();
                if (stream == null)
                    throw new RuntimeException("stream is null");
                f.deleteOnExit();
            //  String tempPath = f.getAbsolutePath();
                FileOutputStream out = new FileOutputStream(f);
                byte buf[] = new byte[1024];
                do {
                    int numread = stream.read(buf);
                    if (numread <= 0)
                        break;
                    out.write(buf, 0, numread);
                } while (true);
                try {
                    out.flush();
                    out.close();
                    stream.close();
                    cn.disconnect();

                } catch (IOException ex) {
                    ex.printStackTrace();
                }
                }

            return f;

        } catch (Throwable ex) {
            ex.printStackTrace();

            return null;
        }
    }

    boolean imageViewReused(PhotoToLoad photoToLoad) {

        String tag = imageViews.get(photoToLoad.imageView);
        if (tag == null || !tag.equals(photoToLoad.url))
            return true;
        return false;
    }

    class BitmapDisplayer implements Runnable {
        File bitmap;
        PhotoToLoad photoToLoad;

        public BitmapDisplayer(File b, PhotoToLoad p) {
            bitmap = b;
            photoToLoad = p;
        }

        public void run() {
            if (imageViewReused(photoToLoad))
                return;

            try {
                if (bitmap!= null && bitmap.length()>0) {
                    photoToLoad.imageView.setVideoPath(bitmap.getAbsolutePath());
                    photoToLoad.defaultImage.setVisibility(View.VISIBLE);
                    photoToLoad.progressbar.setVisibility(View.GONE);

                } else {

                    photoToLoad.defaultImage.setVisibility(View.GONE);
                    photoToLoad.progressbar.setVisibility(View.VISIBLE);
                }
            }
            catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public void clearCache() {
        memoryCache.clear();
        fileCache.clear();
    }
公共类视频加载器{
VideoMemoryCache memoryCache=新的VideoMemoryCache();
视频文件缓存;
私人语境;
私有位图加载bmp;
私有地图图像视图=集合
.synchronizedMap(新WeakHashMap());
执行服务执行服务;
Handler=newhandler();
公共视频加载器(上下文){
fileCache=新的VideoFileCache(上下文);
this.context=上下文;
executorService=Executors.newFixedThreadPool(5);
mLoadingbmp=BitmapFactory.decodeResource(context.getResources(),
R.可牵引装载);
}
public void DisplayImage(字符串url、VideoView、VideoView、,
按钮默认图像,进度条(进度条){
imageViews.put(videoView,url);
文件位图=memoryCache.get(url);
if(bitmap!=null&&bitmap.length()>0){
videoView.setVideoPath(bitmap.getAbsolutePath());
defaultIMage.setVisibility(View.VISIBLE);
progressbar.setVisibility(View.GONE);
}否则{
queuePhoto(url、videoView、defaultIMage、progressbar);
defaultIMage.setVisibility(View.GONE);
progressbar.setVisibility(View.VISIBLE);
}
}
私人照片(字符串url、视频视图图像视图、按钮btn、进度条pb){
PhotoToLoad p=新的PhotoToLoad(url、imageView、btn、pb);
executorService.submit(新的PhotoLoader(p));
}
私有类光电负载{
公共字符串url;
公共视频视图;
公共按钮图像;
ProgressBar ProgressBar;
公共PhotoToLoad(字符串u、视频视图i、按钮btn、进度条pb){
url=u;
imageView=i;
defaultImage=btn;
progressbar=pb;
}
}
类photoloader实现可运行{
光电负载光电负载;
PhotoLoader(PhotoToLoad PhotoToLoad){
this.photoToLoad=photoToLoad;
}
@凌驾
公开募捐{
试一试{
if(图像视图重用(光电加载))
返回;
文件bmp=getFile(photoload.url);
memoryCache.put(photoload.url,bmp);
if(图像视图重用(光电加载))
返回;
//BitmapDisplayer bd=新的BitmapDisplayer(bmp,photoToLoad);
//邮政署署长(屋宇署);
}捕获(可丢弃){
th.printStackTrace();
}
}
}
私有文件getFile(字符串路径){
文件f=fileCache.getFile(路径);
如果(f.exists())
返回f;
试一试{
如果(!URLUtil.isNetworkUrl(路径)){
}否则{
URL=新URL(路径);
HttpURLConnection cn=(HttpURLConnection)url.openConnection();
cn.setReadTimeout(5000);
cn.setConnectTimeout(30000);
cn.SetInstanceFollow(真);
cn.connect();
InputStream=cn.getInputStream();
if(流==null)
抛出新的RuntimeException(“流为null”);
f、 deleteOnExit();
//字符串tempPath=f.getAbsolutePath();
FileOutputStream out=新的FileOutputStream(f);
字节buf[]=新字节[1024];
做{
int numread=stream.read(buf);
if(numread 0){
photoToLoad.imageView.setVideoPath(bitmap.getAbsolutePath());
photoToLoad.defaultImage.setVisibility(View.VISIBLE);
photoToLoad.progressbar.setVisibility(View.GONE);
}否则{
photoToLoad.defaultImage.setVisibility(View.GONE);
photoToLoad.progressbar.setVisibility(View.VISIBLE);
}
}
捕获(例外e){
e、 printStackTrace();
}
}
}
公共void clearCache(){
memoryCache.clear();
fileCache.clear();
}

下载速度取决于许多因素,包括您的网络带宽、设备/sd卡写入速度等。您正在使用单线程下载和小数据缓冲区,这会影响下载性能。我建议将数据缓冲区大小从1024增加到8192或更高,并实现多线程下载(如果您的服务器支持的话)。有关详细信息,请参阅HTTP“Range”标题。

我认为这个问题更适合您。