2011-03-02 86 views
3

我正在編寫一個Java客戶端應用程序,它使用Google Data API將內容上傳到YouTube。我想知道如何去跟蹤上傳的進度,使用Google Data API庫,我只需調用service.insert插入一個新視頻,該視頻會阻止直到完成。如何使用Java + Google Data API來測量上傳比特率

是否有其他人想出一個解決方案來監控上傳狀態並計算髮送的字節數?

感謝您的任何想法

鏈接:

http://code.google.com/apis/youtube/2.0/developers_guide_java.html#Direct_Upload

+2

如何使用自己的InputStrea實現自己的[MediaFileSource](http://code.google.com/apis/gdata/javadoc/com/google/gdata/data/media/MediaFileSource.html) m'表示有多少數據被讀取? – 2011-03-02 04:15:37

回答

1

擴展com.google.gdata.data.media.MediaSource的writeTo(),包括讀取動作計數器:

 public static void writeTo(MediaSource source, OutputStream outputStream) 
     throws IOException { 

     InputStream sourceStream = source.getInputStream(); 
     BufferedOutputStream bos = new BufferedOutputStream(outputStream); 
     BufferedInputStream bis = new BufferedInputStream(sourceStream); 
     long byteCounter = 0L; 

     try { 
     byte [] buf = new byte[2048]; // Transfer in 2k chunks 
     int bytesRead = 0; 
     while ((bytesRead = bis.read(buf, 0, buf.length)) >= 0) { 
      // byte counter 
      byteCounter += bytesRead; 
      bos.write(buf, 0, bytesRead); 
     } 
     bos.flush(); 
     } finally { 
     bis.close(); 
     } 
    } 
    } 
相關問題