2013-12-16 79 views
0

我有這個doInBackground方法,它記錄來自互聯網的音頻流。主要工作是在while (len != -1) { }內完成。這可以正常工作,但buffer = new byte[]不能正常工作 - 當連接中斷時,IOException被立即調用並且緩衝區不再使用。我怎樣才能讓這個緩衝區「喂」我的代碼,直到它是空的?我想要像在音頻播放器中一樣的行爲;所以首先當你連接到流(它正在緩衝),然後播放,如果連接中斷,它仍然從緩衝區播放(直到它是空的)。如何實現互聯網音頻流緩衝區?

記錄:

protected Boolean doInBackground(String... StringUrls) { 

     boolean fdetermined = false; 
     Environment env = new Environment(); 
     Calendar c = Calendar.getInstance(); 
     BufferedOutputStream bufOutstream = null; 

     buffer = new byte[1024 * 10]; 
     int len=-1; 

     InputStream in = null; 

     URLConnection conn = null; 


     try{ 

      conn = new URL(StringUrls[0]).openConnection(); 
      conn.setConnectTimeout(5000); 
      in = conn.getInputStream(); 
      len = in.read(buffer); 

      File dir = new File(env.getExternalStorageDirectory() + "/somewhere"); 
      if (!dir.exists()) { 
       dir.mkdir(); 
      } 


      filename = env.getExternalStorageDirectory()+"/somewhere/file"; 
      bufOutstream = new BufferedOutputStream(new FileOutputStream(new File(filename))); 

     } catch (IOException e) { 
      System.err.println("Caught IOException: " + e.getMessage()); 


     } 



     while (len != -1) { 

      if(in != null && buffer != null && bufOutstream != null) { 
       try { 

       bufOutstream.write(buffer, 0, len); 

       len = in.read(buffer); 

       if (Recorder.this.isCancelled) { 

        Recorder.this.stopSelf(); 
        break; 
       } 

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

       conn = null; 
       in = null; 


      } 
     } 

      } 

      try{ 

       if(bufOutstream != null) { 
      bufOutstream.close(); 
       } 

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


     } 


     return true; 
     } 

IO例外:

12-16 14:37:16.999 31950-32021/com.app.example W/System.err﹕ java.net.SocketException: recvfrom failed: ETIMEDOUT (Connection timed out) 
12-16 14:37:17.059 31950-32021/com.app.example W/System.err﹕ at libcore.io.IoBridge.maybeThrowAfterRecvfrom(IoBridge.java:542) 
12-16 14:37:17.109 31950-32021/com.app.example W/System.err﹕ at libcore.io.IoBridge.recvfrom(IoBridge.java:506) 
12-16 14:37:17.109 31950-32021/com.app.example W/System.err﹕ at java.net.PlainSocketImpl.read(PlainSocketImpl.java:488) 
12-16 14:37:17.109 31950-32021/com.app.example W/System.err﹕ at java.net.PlainSocketImpl.access$000(PlainSocketImpl.java:46) 
12-16 14:37:17.119 31950-32021/com.app.example W/System.err﹕ at java.net.PlainSocketImpl$PlainSocketInputStream.read(PlainSocketImpl.java:240) 
12-16 14:37:17.119 31950-32021/com.app.example W/System.err﹕ at java.io.BufferedInputStream.read(BufferedInputStream.java:304) 
12-16 14:37:17.149 31950-32021/com.app.example W/System.err﹕ at libcore.net.http.UnknownLengthHttpInputStream.read(UnknownLengthHttpInputStream.java:41) 
12-16 14:37:17.149 31950-32021/com.app.example W/System.err﹕ at java.io.InputStream.read(InputStream.java:163) 
12-16 14:37:17.189 31950-32021/com.app.example W/System.err﹕ at com.app.example.Recorder$RecordTask.doInBackground(Recorder.java:376) 
12-16 14:37:17.189 31950-32021/com.app.example W/System.err﹕ at com.app.example.Recorder$RecordTask.doInBackground(Recorder.java:288) 
12-16 14:37:17.189 31950-32021/com.app.example W/System.err﹕ at android.os.AsyncTask$2.call(AsyncTask.java:264) 
12-16 14:37:17.189 31950-32021/com.app.example W/System.err﹕ at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305) 
12-16 14:37:17.199 31950-32021/com.app.example W/System.err﹕ at java.util.concurrent.FutureTask.run(FutureTask.java:137) 
12-16 14:37:17.199 31950-32021/com.app.example W/System.err﹕ at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208) 
12-16 14:37:17.199 31950-32021/com.app.example W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076) 
12-16 14:37:17.199 31950-32021/com.app.example W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569) 
12-16 14:37:17.199 31950-32021/com.app.example W/System.err﹕ at java.lang.Thread.run(Thread.java:856) 
12-16 14:37:17.199 31950-32021/com.app.example W/System.err﹕ Caused by: libcore.io.ErrnoException: recvfrom failed: ETIMEDOUT (Connection timed out) 
12-16 14:37:17.199 31950-32021/com.app.example W/System.err﹕ at libcore.io.Posix.recvfromBytes(Native Method) 
12-16 14:37:17.199 31950-32021/com.app.example W/System.err﹕ at libcore.io.Posix.recvfrom(Posix.java:131) 
12-16 14:37:17.209 31950-32021/com.app.example W/System.err﹕ at libcore.io.BlockGuardOs.recvfrom(BlockGuardOs.java:164) 
12-16 14:37:17.209 31950-32021/com.app.example W/System.err﹕ at libcore.io.IoBridge.recvfrom(IoBridge.java:503) 
+0

使用finally塊 – Sach

+0

好的,但是怎麼把這個'最後'塊? – Kristopher

回答

0

我已經加入終於下面的框:

while (len != -1) { 

       if(in != null && buffer != null && bufOutstream != null) { 
        try { 

         bufOutstream.write(buffer, 0, len); 

         len = in.read(buffer); 

         if (Recorder.this.isCancelled) { 

          Recorder.this.stopSelf(); 
          break; 
         } 

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

         conn = null; 
         in = null; 


        } 
        finally{ 

         //do your work here. This block will execute no matter of what exception is thrown 
         try{ 

          if(bufOutstream != null) { 
           bufOutstream.close(); 
          } 

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


         } 
        } 
       } 

      } 
+0

我不明白這與我的問題有何關係。 – Kristopher