我使用以下代码通过Youtube API将视频上传到Youtube。我的问题是上传视频后,我需要给用户视频的位置。我怎么找到这个?如果有人能帮我解决这个问题,我将不胜感激。
MediaFileSource ms = new MediaFileSource(videoFile, mimeType);
String videoTitle = title;
VideoEntry newEntry = new VideoEntry();
YouTubeMediaGroup mg = newEntry.getOrCreateMediaGroup();
mg.addCategory(new MediaCategory(YouTubeNamespace.CATEGORY_SCHEME, "Tech"));
mg.setTitle(new MediaTitle());
mg.getTitle().setPlainTextContent(videoTitle);
mg.setKeywords(new MediaKeywords());
mg.getKeywords().addKeyword("yt:crop=16:9");
mg.setDescription(new MediaDescription());
mg.getDescription().setHtmlContent(attributionDocument);
mg.setPrivate(true);
mg.setVideoId("Vid1");
ResumableGDataFileUploader uploader = null;
try {
uploader = new ResumableGDataFileUploader.Builder(
service, new URL(RESUMABLE_UPLOAD_URL), ms, newEntry)
.title(videoTitle)
.build();
uploader.start();
while (!uploader.isDone()) {
try {
Thread.sleep(PROGRESS_UPDATE_INTERVAL);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
switch(uploader.getUploadState()) {
case COMPLETE:
System.out.println("Uploaded successfully");
break;
case CLIENT_ERROR:
System.out.println("Upload Failed");
break;
default:
System.out.println("Unexpected upload status");
break;
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ServiceException e) {
e.printStackTrace();
}
我自己设法解决了这个问题。我没有使用可恢复的文件上传,而是使用了直接上传。我的代码:
String id = "";
File videoFile = new File(videoLocation);
if (!videoFile.exists()) {
System.out.println("Sorry, that video doesn't exist.");
}
String videoTitle = title;
VideoEntry newEntry = new VideoEntry();
YouTubeMediaGroup mg = newEntry.getOrCreateMediaGroup();
mg.setTitle(new MediaTitle());
mg.getTitle().setPlainTextContent(videoTitle);
mg.setKeywords(new MediaKeywords());
mg.getKeywords().addKeyword("yt:crop=16:9");
mg.setDescription(new MediaDescription());
mg.getDescription().setHtmlContent(attributionDocument);
mg.setPrivate(true);
mg.addCategory(new MediaCategory(YouTubeNamespace.CATEGORY_SCHEME, "Tech"));
MediaFileSource ms = new MediaFileSource(videoFile, "video/quicktime");
newEntry.setMediaSource(ms);
String uploadUrl =
"http://uploads.gdata.youtube.com/feeds/api/users/default/uploads";
VideoEntry createdEntry = service.insert(new URL(uploadUrl), newEntry);
id =createdEntry.getId();
return id;
}
我希望这能挽救别人的一天。