Youtube C# .NET API : Uploading video and getting events when finished

淺唱寂寞╮ 提交于 2019-12-05 01:36:05

问题


This is the code to upload a video to Youtube using the C# .NET API from a Windows Forms desktop application:

YouTubeRequestSettings settings = new YouTubeRequestSettings("whatwill come here ?",
                "my api key",
                "my youtube login email", "my youtube login password");
YouTubeRequest request = new YouTubeRequest(settings);

Video newVideo = new Video();

newVideo.Title = "test 1";
newVideo.Tags.Add(new MediaCategory("Gaming", YouTubeNameTable.CategorySchema));
newVideo.Keywords = "test 1 , test 2";
newVideo.Description = "test 3 test 4";
newVideo.YouTubeEntry.Private = false;
newVideo.Tags.Add(new MediaCategory("tag 1, tag 2",
              YouTubeNameTable.DeveloperTagSchema));
newVideo.YouTubeEntry.Location = new GeoRssWhere(37, -122);
newVideo.YouTubeEntry.MediaSource = new MediaFileSource("C:\\test.avi", "video/quicktime");         
Video createdVideo = request.Upload(newVideo);

This works. What I'm looking for is the events that get me back the upload progress, so I can show the progress in a progressbar. Ich can register the following events:

                            request.Service.AsyncOperationProgress +=
                            new AsyncOperationProgressEventHandler(Service_AsyncOperationProgress);
                        request.Service.AsyncOperationCompleted +=
                            new AsyncOperationCompletedEventHandler(Service_AsyncOperationCompleted);

... but they never get fired while uploading. Also, I cannot find any documentation about the .NET api that goes much further than the small video upload example above. So: Are those the wrong events to look for? Just for reference, I'm starting the seemingly synchonous upload in the following code in a background thread:

            ThreadPool.QueueUserWorkItem(
            delegate
                {
                    try
                    {
                        createdVideo = request.Upload(newVideo);
                    } catch (Exception ex){
                      Invoke((ThreadStart) delegate{uploadingFailedWithException(ex);});
                    }
                });
            Invoke((ThreadStart)readyUploading);

This way I know when the synchonous operation ended, but I'd like to have events for progress updates to the user. Any ideas?


回答1:


The Upload method you are using is synchronous and, as such, the execution of your program will stop on that line of code and only move on when the upload is complete.

What you are trying to do requires using asynchronous upload. A complete example showing how to use the ResumableUploader component and the AsyncOperationCompleted/AsyncOperationProgress events is included in the .NET client library and available at http://code.google.com/p/google-gdata/source/browse/#svn%2Ftrunk%2Fclients%2Fcs%2Fsamples%2FYouTubeUploader%2FYouTubeUploader



来源:https://stackoverflow.com/questions/6441553/youtube-c-sharp-net-api-uploading-video-and-getting-events-when-finished

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!