在开始下一个

时间:2017-04-18 19:50:19

标签: c# youtube youtube-api

我有一个youtube上传器,我正在从一个音频文件中生成一个视频,该工作正常,但当我上传到Youtube时,程序仍在运行,当我试图等待它完成上传之后重复

我在这里制作视频:

    private void button2_Click(object sender, EventArgs e)
    {
        if (status.Text == "Stopped")
        {
            if (!generatearticle.IsBusy)
            {
                // started
                status.Text = "Started";
                status.ForeColor = System.Drawing.Color.Green;
                start.Text = "Stop Generating";
                generatearticle.RunWorkerAsync();
            }
        }
        else
        {
            if(generatearticle.IsBusy)
            {
                generatearticle.CancelAsync();
                // started
                status.Text = "Stopped";
                status.ForeColor = System.Drawing.Color.Red;
                start.Text = "Start Generating";
            }
        }
    }

    private void core()
    { 
        // generate audio
        int i = 0;
        for (int n = 1; n < co; n++)
        {
            // generate video and upload to
            // youtube, this generates, but
            // when uploading to youtube this for
            // loop carries on when I want it to
            // upload to youtube first before carrying on
            generatevideo(image, articlename);
        }
    }

    private void generateVideo(string images, String articlename)
    {
       //generate the video here, once done upload
       {code removed, this just generates a video, nothing important}

       // now upload (but I want it to finish before repeating the core() function
       try
            {
                new UploadVideo().Run(articlename, file);

            }
            catch (AggregateException ex)
            {
                foreach (var e in ex.InnerExceptions)
                {
                    ThreadSafe(() =>
                    {
                        this.Invoke((MethodInvoker)delegate
                        {
                            status.Text = e.Message;
                            status.ForeColor = System.Drawing.Color.Red;
                        });
                    });
                }
            }
    }

我如何上传到Youtube:

using System;
using System.IO;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
using Google.Apis.Auth.OAuth2;
using Google.Apis.Services;
using Google.Apis.Upload;
using Google.Apis.Util.Store;
using Google.Apis.YouTube.v3;
using Google.Apis.YouTube.v3.Data;

namespace articletoyoutube
{
    /// <summary>
    /// YouTube Data API v3 sample: upload a video.
    /// Relies on the Google APIs Client Library for .NET, v1.7.0 or higher.
    /// See https://code.google.com/p/google-api-dotnet-client/wiki/GettingStarted
    /// </summary>
    class UploadVideo
    {
        // to access form controlls
        Form1 core = new Form1();

        public async Task Run(string articlename, string filelocation)
        {
            UserCredential credential;
            using (var stream = new FileStream("client_secrets.json", FileMode.Open, FileAccess.Read))
            {
                credential = await GoogleWebAuthorizationBroker.AuthorizeAsync(
                    GoogleClientSecrets.Load(stream).Secrets,
                    // This OAuth 2.0 access scope allows an application to upload files to the
                    // authenticated user's YouTube channel, but doesn't allow other types of access.
                    new[] {
                        YouTubeService.Scope.YoutubeUpload
                    },
                    "user",
                    CancellationToken.None
                );
            }


            var youtubeService = new YouTubeService(new BaseClientService.Initializer()
            {
                HttpClientInitializer = credential,
                ApplicationName = Assembly.GetExecutingAssembly().GetName().Name
            });

            var video = new Video();
            video.Snippet = new VideoSnippet();
            video.Snippet.Title = articlename;
            video.Snippet.Description = "News story regarding" + articlename;
            video.Snippet.Tags = new string[] {
                "news",
                "breaking",
                "important"
            };
            video.Snippet.CategoryId = "25"; // See https://developers.google.com/youtube/v3/docs/videoCategories/list
            video.Status = new VideoStatus();
            video.Status.PrivacyStatus = "public"; // or "private" or "public"
            var filePath = filelocation; // Replace with path to actual movie file.

            using (var fileStream = new FileStream(filePath, FileMode.Open))
            {
                var videosInsertRequest = youtubeService.Videos.Insert(video, "snippet,status", fileStream, "video/*");
                videosInsertRequest.ProgressChanged += videosInsertRequest_ProgressChanged;
                videosInsertRequest.ResponseReceived += videosInsertRequest_ResponseReceived;

                await videosInsertRequest.UploadAsync();
            }
        }

        void videosInsertRequest_ProgressChanged(Google.Apis.Upload.IUploadProgress progress)
        {
            switch (progress.Status)
            {
                case UploadStatus.Uploading:
                    core.prog_up.Text = "{0} bytes sent." + progress.BytesSent;
                    break;

                case UploadStatus.Failed:
                    core.status.Text = "An error prevented the upload from completing.\n{0}" + progress.Exception;
                    core.status.ForeColor = System.Drawing.Color.Red;
                    break;
            }
        }

        void videosInsertRequest_ResponseReceived(Video video)
        {
            core.prog_up.Text = "Video id '{0}' was successfully uploaded." + video.Id;
        }
    }
}

后台工作人员只运行core();

当它到达功能时

 new UploadVideo().Run(articlename, file);

它开始上传但重新开始重复核心功能,从而在视频上传之前生成另一个视频....如果我使用

new UploadVideo().Run(articlename, file).Wait();

然后程序停止并等待,直到我关闭程序,如何在核心方法中继续前循环之前等待Upload类/方法完成?

对于回答的人,当我在新的上传之前添加等待时......它给了我:

  

严重级代码描述项目文件行抑制状态   错误CS4033'await'运算符只能在异步中使用   方法。考虑使用'async'修饰符标记此方法   将其返回类型更改为   '任务'。 articletoyoutube C:\ Users \ Laptop \ Documents \ Visual Studio   2017 \ Projects \ articletoyoutube \ articletoyoutube \ Form1.cs 254 Active

2 个答案:

答案 0 :(得分:1)

确保在方法上使用async关键字,并为任务使​​用await关键字。

例如:

private async Task core()
{ 
    // generate audio
    int i = 0;
    for (int n = 1; n < co; n++)
    {
        await generatevideo(image, articlename);
    }
}

private async Task generateVideo(string images, String articlename)
    {
       //generate the video here,
       try
            {
                var uploadVideo = new UploadVideo();
                await uploadVideo.Run(articlename, file);

            }
            catch (AggregateException ex)
            {
                foreach (var e in ex.InnerExceptions)
                {
                    ThreadSafe(() =>
                    {
                        this.Invoke((MethodInvoker)delegate
                        {
                            status.Text = e.Message;
                            status.ForeColor = System.Drawing.Color.Red;
                        });
                    });
                }
            }
    }

答案 1 :(得分:0)

您需要在调用堆栈中一直使用await到事件处理程序所在的位置,这需要更改许多方法。

private async Task core()
{ 
    // generate audio
    int i = 0;
    for (int n = 1; n < co; n++)
    {
        // generate video and upload to
        // youtube, this generates, but
        // when uploading to youtube this for
        // loop carries on when I want it to
        // upload to youtube first before carrying on
        await generatevideo(image, articlename);
    }
}

private async Task generateVideo(string images, String articlename)
{
   //generate the video here, once done upload
   {code removed, this just generates a video, nothing important}

   // now upload (but I want it to finish before repeating the core() function
   try
        {
            await new UploadVideo().Run(articlename, file);

        }
        catch (AggregateException ex)
        {
            foreach (var e in ex.InnerExceptions)
            {
                ThreadSafe(() =>
                {
                    this.Invoke((MethodInvoker)delegate
                    {
                        status.Text = e.Message;
                        status.ForeColor = System.Drawing.Color.Red;
                    });
                });
            }
        }
}

注意,使用async / await不能与BackgroundWorker一起使用,您需要切换到使用Task.RunCancellationToken来表示取消。

Task _backgroundWork;
CancellationTokenSource _cts;

private void button2_Click(object sender, EventArgs e)
{
    if (status.Text == "Stopped")
    {
        if (!generatearticle.IsBusy)
        {
            // started
            status.Text = "Started";
            status.ForeColor = System.Drawing.Color.Green;
            start.Text = "Stop Generating";
            _cts = new CancellationTokenSource();
            _backgroundWork = Task.Run(() => core(_cts.Token), _cts.Token);
        }
    }
    else
    {
        if(!_backgroundWork.IsCompleted)
        {
            _cts.Cancel();
            // started
            status.Text = "Stopped";
            status.ForeColor = System.Drawing.Color.Red;
            start.Text = "Start Generating";
        }
    }
}