Youtube C# .NET API:上传视频并在完成后获取事件

发布于 2024-11-17 04:17:52 字数 1936 浏览 1 评论 0原文

这是使用 C# .NET API 从 Windows 窗体桌面应用程序将视频上​​传到 Youtube 的代码:

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);

这有效。我正在寻找的是让我返回上传进度的事件,这样我就可以在进度栏中显示进度。 Ich 可以注册以下事件:

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

...但它们在上传时永远不会被解雇。另外,我找不到任何有关 .NET api 的文档,这些文档比上面的小视频上传示例更进一步。那么:这些是需要寻找的错误事件吗?仅供参考,我在后台线程中的以下代码中开始看似同步的上传:

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

这样我就知道同步操作何时结束,但我希望向用户提供进度更新事件。有什么想法吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

云柯 2024-11-24 04:17:52

您使用的 Upload 方法是同步的,因此,程序的执行将停止在该行代码上,并且仅在上传完成后才继续执行。

您想要做的事情需要使用异步上传。展示如何使用 ResumableUploader 组件和 AsyncOperationCompleted/AsyncOperationProgress 事件的完整示例包含在 .NET 客户端库中,可从以下位置获取:http://code.google.com/p/google-gdata/source/browse/#svn%2Ftrunk%2Fclients%2Fcs%2Fsamples%2FYouTubeUploader%2FYouTubeUploader

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文