2011-06-16 199 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; 

    } 

我希望這會拯救別人的一天。