private void UploadVideo(string FileName, string VideoTitle, string VideoDescription)
{
try
{
var youtubeService = new YouTubeService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = Assembly.GetExecutingAssembly().GetName().Name
});
video.Snippet = new VideoSnippet();
video.Snippet.Title = VideoTitle;
video.Snippet.Description = VideoDescription;
video.Snippet.Tags = new string[] { "tag1", "tag2" };
video.Status = new VideoStatus();
video.Status.PrivacyStatus = "public";
using (var fileStream = new FileStream(FileName, FileMode.Open))
{
const int KB = 0x400;
var minimumChunkSize = 256 * KB;
var videosInsertRequest = youtubeService.Videos.Insert(video,
"snippet,status", fileStream, "video/*");
videosInsertRequest.ProgressChanged +=
videosInsertRequest_ProgressChanged;
videosInsertRequest.ResponseReceived +=
videosInsertRequest_ResponseReceived;
// The default chunk size is 10MB, here will use 1MB.
videosInsertRequest.ChunkSize = minimumChunkSize * 3;
dt = DateTime.Now;
totalBytes = fileStream.Length;
videosInsertRequest.Upload();
videosInsertRequest.UploadAsync();
}
}
catch (Exception errors)
{
string errorss = errors.ToString();
}
}
到现在为止,我上传视频文件时只使用以下行:
videosInsertRequest.Upload();
在Upload的描述上写着:上传内容到服务器。此方法是同步的,并将阻塞,直到上传完成。
和UploadAsync的描述说:上传内容异步到服务器。
如果我有一个视频文件列表,我想并行上传它们,我的意思是同时上传一些视频,我该怎么做?
或者如果我使用UploadAsync,那么如果我点击一个按钮,每次它都会上传一个不同的文件名视频文件,它们将同时并行上传?
换句话说,我认为:我的问题应该是我如何能上传多个视频文件到youtube在同一时间?
var mResumableUploader = new ResumableUploader(chunkSize);
mResumableUploader.AsyncOperationCompleted += MResumableUploaderAsyncOperationCompleted;
mResumableUploader.AsyncOperationProgress += MResumableUploaderAsyncOperationProgress;
mResumableUploader.AsyncOperationCompleted += new AsyncOperationCompletedEventHandler(ru_AsyncOperationCompleted);
var youTubeAuthenticator = new ClientLoginAuthenticator(appName, ServiceNames.YouTube, uName, passWord);
youTubeAuthenticator.DeveloperKey = devKey;
newVideo = new Video();
newVideo.Title = "video";
newVideo.Tags.Add(new MediaCategory("Entertainment", YouTubeNameTable.CategorySchema));
newVideo.Keywords = "video";
newVideo.Description = "video";
newVideo.YouTubeEntry.Private = false;
newVideo.YouTubeEntry.MediaSource = new MediaFileSource(fileName, fileContType);
var link = new AtomLink("http://uploads.gdata.youtube.com/resumable/feeds/api/users/default/uploads");
link.Rel = ResumableUploader.CreateMediaRelation;
newVideo.YouTubeEntry.Links.Add(link);
Console.WriteLine("Starting upload: ");
mResumableUploader.InsertAsync(youTubeAuthenticator, newVideo.YouTubeEntry, "inserter");
有完成事件:
void ru_AsyncOperationCompleted(object sender, AsyncOperationCompletedEventArgs e)
{
//upload complete
YouTubeRequestSettings ytSettings = new YouTubeRequestSettings("myApp", googleDevKey, ytUsername, ytPassword);
Video v = ytRequest.ParseVideo(e.ResponseStream);
string videoId = v.VideoId;
string watchPage = v.WatchPage.ToString();
}