YouTube C#API-如何恢复失败的上传



我正在尝试使用ResumeUploader 通过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事件获得当前位置。但是我该如何继续上传呢?

我知道这是一个老问题,但我是在尝试用YouTube API的V3做同样的事情时发现的。这看起来像是API早期版本的代码,所以我对API的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();  

相关内容

  • 没有找到相关文章