2014-02-18 115 views
0

我創建了10秒的視頻,並在亞馬遜服務器上傳,之後我在列表視圖中下載它 像圖像lazyloading.First第一次下載超過6-8分鐘。視頻文件大小是6.89-7 MB。但在ios中同樣的視頻在 下載30秒。我的需求是一樣的藤應用程序在android.Please幫助。 在此先感謝。 這裏是我的代碼: -在android中的視頻下載問題

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

我覺得這個問題更適合[CodeReview](http://codereview.stackexchange.com/)。 –

回答

0

下載速度取決於很多因素,包括您的網絡帶寬,設備/ SD卡的寫入速度,等您正在使用小數據緩衝區單線程下載,影響下載性能。我建議將您的數據緩衝區大小從1024增加到8192或更高,並實現多線程下載(以防您的服務器支持它)。有關詳細信息,請參閱HTTP「範圍」標題。