Advertisement
kinhoSilva

bufferVideoUrl

Jul 7th, 2019
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.89 KB | None | 0 0
  1.     private void bufferVideoUrl(){
  2.         new Thread(){
  3.  
  4.             @Override
  5.             public void run(){
  6.  
  7.                 try {
  8.                     URL url = new URL("http://media-br-am.crackle.com/1/h/96/m21lf_480p.mp4");
  9.  
  10.                     HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
  11.  
  12.                     httpURLConnection.connect();
  13.  
  14.                     InputStream inputStream = httpURLConnection.getInputStream();
  15.  
  16.                     //inputStream.read();
  17.  
  18.                     int line;
  19.                     byte[] bytes = new byte[32 * 1024];
  20.  
  21.                     File file = new File("Filme2.mp4");
  22.  
  23.                     if(!file.exists())
  24.                         file.createNewFile();
  25.  
  26.                     BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(file));
  27.  
  28.                     int loopCount = 0;
  29.                     boolean playVideo = false;
  30.  
  31.                     while ((line = inputStream.read(bytes)) != -1){
  32.                         System.out.println("Bufferizando");
  33.                         bufferedOutputStream.write(bytes,0, line);
  34.  
  35.                         bufferedOutputStream.flush();
  36.  
  37.                         loopCount += line;
  38.  
  39.                         if(loopCount >= 12000 && !playVideo) {
  40.                             //System.out.println("Exibindo Video"+file.toString());
  41.  
  42.                             showVideo(file.toString());
  43.                             playVideo = true;
  44.                         }
  45.  
  46.  
  47.                     }
  48.  
  49.                     bufferedOutputStream.close();
  50.                     showVideo(file.toString());
  51.  
  52.                 } catch (MalformedURLException e) {
  53.                     e.printStackTrace();
  54.                 } catch (IOException e) {
  55.                     e.printStackTrace();
  56.  
  57.                 }
  58.  
  59.             }
  60.         }.start();
  61.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement