我正在实施一项使用他们的 API (V2) 将视频上传到 YouTube 的服务,但是一旦上传了视频,我需要找到响应,以便我可以获得目标 URL。有人知道如何实现这一目标吗?
这是调用上传函数的调用:
YouTubeService service = new YouTubeService("My_Service", developerKey);
try {
service.setUserCredentials(user, password);
} catch (AuthenticationException e) {
System.out.println("Invalid login credentials.");
return;
}
youtubeURL = YouTubeUploadClient.uploadVideo(service);
这是我所说的上传功能代码:
public static String uploadVideo(YouTubeService service)
throws IOException, ServiceException, InterruptedException, JSONException {
String youtubeURL = null;
File videoFile = new File("c://video.mp4");
if (!videoFile.exists()) {
System.out.println("Sorry, that video doesn't exist.");
return null;
}
MediaFileSource ms = new MediaFileSource(videoFile, "video/mp4");
String videoTitle = "Title";
VideoEntry newEntry = new VideoEntry();
YouTubeMediaGroup mg = newEntry.getOrCreateMediaGroup();
mg.addCategory(new MediaCategory(YouTubeNamespace.CATEGORY_SCHEME, "Games"));
mg.setTitle(new MediaTitle());
mg.getTitle().setPlainTextContent("Title");
mg.setKeywords(new MediaKeywords());
mg.getKeywords().addKeyword("Test");
mg.setDescription(new MediaDescription());
mg.getDescription().setPlainTextContent("Description");
FileUploadProgressListener listener = new FileUploadProgressListener();
ResumableGDataFileUploader uploader = new ResumableGDataFileUploader.Builder(
service, new URL("http://uploads.gdata.youtube.com/resumable/feeds/api/users/default/uploads"), ms, newEntry)
.title(videoTitle)
.trackProgress(listener, 1000)
.chunkSize(10000000)
.build();
uploader.start();
while (!uploader.isDone()) { Thread.sleep(25); }
switch(uploader.getUploadState()) {
case COMPLETE: System.out.println("Uploaded successfully"); return null;
case CLIENT_ERROR: System.out.println("Upload Failed"); return null;
default: System.out.println("Unexpected upload status"); return null;
}
}
private static class FileUploadProgressListener implements ProgressListener {
public synchronized void progressChanged(ResumableHttpFileUploader uploader)
{
switch(uploader.getUploadState()) {
case COMPLETE: System.out.println("Upload Completed"); break;
case CLIENT_ERROR: System.out.println("Upload Failed"); break;
case IN_PROGRESS: System.out.println(String.format("Upload in progress: %3.0f", uploader.getProgress() * 100) + "%"); break;
case NOT_STARTED: System.out.println("Upload Not Started"); break;
}
}
}