C# - 异步编码Azure Media Services视频并处理任务完成

时间:2018-02-20 18:20:07

标签: c# asynchronous azure-media-services

我有一个ASP.NET MVC Web应用程序,我想使用Azure Media Services对视频进行编码。

由于此过程持续时间过长,我需要异步运行,因此我不会让用户等待处理此操作。此外,我需要以某种方式处理此任务的执行,只要它结束

Azure媒体服务文档提供了实现此目的的代码框架:

static public IAsset EncodeToAdaptiveBitrateMP4Set(IAsset asset)
{
    // Declare a new job.
    IJob job = _context.Jobs.Create("Media Encoder Standard Job");
    // Get a media processor reference, and pass to it the name of the 
    // processor to use for the specific task.
    IMediaProcessor processor = GetLatestMediaProcessorByName("Media Encoder Standard");

    // Create a task with the encoding details, using a string preset.
    // In this case "Adaptive Streaming" preset is used.
    ITask task = job.Tasks.AddNew("My encoding task",
        processor,
        "Adaptive Streaming",
        TaskOptions.None);

    // Specify the input asset to be encoded.
    task.InputAssets.Add(asset);
    // Add an output asset to contain the results of the job. 
    // This output is specified as AssetCreationOptions.None, which 
    // means the output asset is not encrypted. 
    task.OutputAssets.AddNew("Output asset",
        AssetCreationOptions.None);

    job.StateChanged += new EventHandler<JobStateChangedEventArgs>(JobStateChanged);
    job.Submit();
    job.GetExecutionProgressTask(CancellationToken.None).Wait();

    return job.OutputMediaAssets[0];
}

private static void JobStateChanged(object sender, JobStateChangedEventArgs e)
{
    // do something when job state changes 
}

此代码负责在Azure Media Services中对视频进行编码,但处理是同步完成的,因此用户将被阻止,直到此操作结束。

而不是使程序等待任务结束的指令:

job.GetExecutionProgressTask(CancellationToken.None).Wait();

,我需要一些东西让作业在Azure Media Services中运行异步,并且能够处理任务的结束(在编码结束时运行某个代码)。

你能帮我解决这个问题吗?如果您需要更多信息,请大声呼喊!

2 个答案:

答案 0 :(得分:0)

我们建议您注册通知 - 有关详细信息,请参阅this

答案 1 :(得分:0)

仅仅是:

await job.GetExecutionProgressTask(CancellationToken.None);