2013-11-14 77 views
1

我正在實施一項服務,使用他們的API(V2)將視頻上傳到YouTube,但是一旦視頻上傳,我需要找到響應,以便我可以獲取目標網址。任何人都知道如何實現這一目標?如何使用YouTube API V2獲得可恢復上傳的響應?

下面是調用上傳功能調用:

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

回答

0

發現在YouTube的API論壇這個響應(https://groups.google.com/forum/embed/?place=forum/youtube-api-gdata&showsearch=true&showpopout=true&parenturl=https%3A%2F%2Fdevelopers.google.com%2Fyoutube%2Fforum%2Fdiscussion#!searchin/youtube-api-gdata/resumable$20response/youtube-api-gdata/ox_c40kvZWQ/ADy-DWo16HkJ):

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

     Future<ResumableHttpFileUploader.ResponseMessage> futureResponse = uploader.start(); 

     while (!uploader.isDone()) { Thread.sleep(1000); } 

     switch(uploader.getUploadState()) { 
      case COMPLETE: 
       ResumableHttpFileUploader.ResponseMessage responseMessage = futureResponse.get(); 
       VideoEntry videoEntry = new VideoEntry(); 
       videoEntry.parseAtom(new ExtensionProfile(), responseMessage.getInputStream()); 
       youtubeURL = videoEntry.getHtmlLink().getHref(); 
       logger.info("Uploaded successfully"); 
       return youtubeURL; 
      case CLIENT_ERROR: logger.error("Upload Failed"); return null; 
      default: logger.warn("Unexpected upload status"); return null; 
     } 
相關問題