java - 如何使用Java+Google Data API测量上传码率

标签 java youtube google-data-api

我正在编写一个 Java 客户端应用程序,它使用 Google 数据 API 将内容上传到 youtube。我想知道如何使用 Google 数据 API 库来跟踪上传进度,我只需调用 service.insert 来插入一个新视频,该视频会一直阻塞直到完成。

有没有其他人想出一个解决方案来监控上传状态并在发送时计算字节数?

感谢任何想法

链接:

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

最佳答案

扩展 com.google.gdata.data.media.MediaSource writeTo() 以包含 bytesRead 计数器:

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

关于java - 如何使用Java+Google Data API测量上传码率,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5163266/

相关文章:

c# - Google 数据登录到 YouTube?

youtube - 如何在YouTube视频上切换字幕?

c# - 尽管使用 "Namespace Name ",但仍收到错误 "Google.GData.Contacts"联系人“不存在”?

objective-c - 为什么在从 Google Data Objective-C Client Library 子类化 GDataEntryBase 时会出现 "unrecognized selector"错误?

youtube - 从网络应用程序到youtube的Google API访问

JavaFX 屏幕分辨率缩放

java - 在演示者之间传递控件/值,GWT 模型 View 演示者?

Java正则表达式匹配至少出现两次的单词

java - 记录拆分是否需要为hadoop中的每条记录生成唯一键?

java - YouTube 的响应数组,以编程方式排序