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

问题描述 投票:2回答:2

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

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

Azure Media Services文档提供了用于实现此目的的代码框架:

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中运行异步,并且能够处理任务的结束(在编码结束时运行某个代码)。

你能帮帮我吗?如果您需要更多信息,请大声呼喊!

c# asynchronous azure-media-services
2个回答
0
投票

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


0
投票

怎么样:

await job.GetExecutionProgressTask(CancellationToken.None);
© www.soinside.com 2019 - 2024. All rights reserved.