2011-06-16 102 views
0

我正在使用以下代码通过youtube API将视频上传到Youtube。我的问题是在上传视频后,我需要将视频的位置提供给用户。我如何找到这个? 如果有人能帮我解决这个问题,我会很感激。如何在YouTube上找到上传的视频的网址(通过Youtube API)

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

回答

2

我设法自己解决这个问题。我没有使用可恢复的文件上传,而是使用直接上传。我的代码:

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; 

    } 

我希望这会拯救别人的一天。

相关问题