2013-11-27 93 views
0

我試圖通過使用ResumableUploaderYouTube的C#API - 如何恢復上傳失敗

的代碼看起來像這樣的C#API大的視頻文件上傳到YouTube:

  var settings = new YouTubeRequestSettings(Configuration.YouTubeApplicationName, Configuration.YouTubeApplicationKey, Configuration.YouTubeUsername, Configuration.YouTubePassword); 
      settings.Timeout = int.MaxValue; 

      var newVideo = new Video(); 

      newVideo.Title = title.Trim(); 
      newVideo.Tags.Add(new MediaCategory("Games", YouTubeNameTable.CategorySchema)); 
      newVideo.Keywords = keywords; 
      newVideo.Description = description; 
      newVideo.YouTubeEntry.Private = false; 
      newVideo.Tags.Add(new MediaCategory("StarCraft2, Replay", YouTubeNameTable.DeveloperTagSchema)); 
      var contentType = MediaFileSource.GetContentTypeForFileName(filePathToUpload); 
      newVideo.YouTubeEntry.MediaSource = new MediaFileSource(filePathToUpload, contentType); 

      var link = new AtomLink("http://uploads.gdata.youtube.com/resumable/feeds/api/users/default/uploads"); 
      link.Rel = ResumableUploader.CreateMediaRelation; 
      newVideo.YouTubeEntry.Links.Add(link); 

      var resumableUploader = new ResumableUploader(256); //chunksize 256 kilobyte 
      resumableUploader.AsyncOperationCompleted += resumableUploader_AsyncOperationCompleted; 
      resumableUploader.AsyncOperationProgress += resumableUploader_AsyncOperationProgress; 

      var youTubeAuthenticator = new ClientLoginAuthenticator(Configuration.YouTubeApplicationName, ServiceNames.YouTube, Configuration.YouTubeUsername, Configuration.YouTubePassword); 
      youTubeAuthenticator.DeveloperKey = Configuration.YouTubeApplicationKey; 

      resumableUploader.InsertAsync(youTubeAuthenticator, newVideo.YouTubeEntry, new object()); 

我嘗試獲取異步操作完成後的視頻ID,如下所示:

private static void resumableUploader_AsyncOperationCompleted(object sender, AsyncOperationCompletedEventArgs e) 
{ 
    try 
    { 
     var settings = new YouTubeRequestSettings(Configuration.YouTubeApplicationName, Configuration.YouTubeApplicationKey, Configuration.YouTubeUsername, Configuration.YouTubePassword); 
     var request = new YouTubeRequest(settings); 
     Video v = request.ParseVideo(e.ResponseStream);   

    } 
    catch (Exception ex) 
    { 
     //Upload has been disturbed. 
    } 

} 

問題是。有些時候,上傳,連接不穩定或類似的問題。當發生錯誤時AsyncOperationCompleted被調用。

現在的問題是。我如何恢復上傳?我可以通過AsyncOperationProgress事件獲取當前位置。但是,如何繼續上傳?

回答

0

我知道這是一個老問題,但我在嘗試使用YouTube API的V3做同樣的事情時發現它。這看起來像來自早期版本的API的代碼,所以my question要求對V3的相同內容可能有助於升級。

我已經粘貼了一些舊的代碼,如果有人正在爲此苦苦掙扎,但我正在切換到新版本並替換所有代碼。 youtube_upload對象是我自己的滾動類,而不是api的一部分。我現在也在使用ReSharper並遵循他們的命名約定。

//Grab the rendered YouTube video ready for upload 
string content_type = MediaFileSource.GetContentTypeForFileName(youtube_upload.YouTubeFileName); 
MediaFileSource media_file_source = new MediaFileSource(youtube_upload.YouTubeFileName, content_type); 

//Generate a media stream and move to the currently uploaded position within it 
media_stream_resume = media_file_source.GetDataStream(); 
media_stream_resume.Seek(youtube_upload.CurrentPosition, SeekOrigin.Begin); 

youtube_uploading_id = youtube_upload.YouTubeID;   

//Attempt to resume the upload 
resumable_uploader.ResumeAsync(youtube_authenticator, youtube_upload.ResumeUri, 
    youtube_upload.httpVerb, media_stream_resume, content_type, youtube_upload); 

youtube_upload.ErrorText = Constants.YouTubeStatusResuming; 
youtube_upload.SaveYouTube();