使用 .NET 進行媒體服務開發Media Services development with .NET

注意

媒體服務 v2 不會再新增任何新的特性或功能。No new features or functionality are being added to Media Services v2.
查看最新版本的媒體服務 v3Check out the latest version, Media Services v3. 此外,請參閱從 v2 至 v3 的移轉指導方針Also, see migration guidance from v2 to v3

本文章討論如何使用 .NET 開始開發媒體服務應用程式。This article discusses how to start developing Media Services applications using .NET.

Azure Media Services .NET SDK 程式庫 可讓您使用 .NET 對媒體服務進行程式設計。The Azure Media Services .NET SDK library enables you to program against Media Services using .NET. 為了讓使用 .NET 進行開發更為簡單,會提供 Azure Media Services .NET SDK 延伸模組 程式庫。To make it even easier to develop with .NET, the Azure Media Services .NET SDK Extensions library is provided. 此程式庫包含一組延伸方法和協助程式函數,以簡化 .NET 程式碼。This library contains a set of extension methods and helper functions that simplify your .NET code. 這兩個程式庫都是透過 NuGetGitHub 取得。Both libraries are available through NuGet and GitHub.

必要條件Prerequisites

  • 新的或現有 Azure 訂用帳戶中的媒體服務帳戶。A Media Services account in a new or existing Azure subscription. 請參閱文章如何建立媒體服務帳戶See the article How to Create a Media Services Account.
  • 作業系統:Windows 10、Windows 7、Windows 2008 R2 或 Windows 8。Operating Systems: Windows 10, Windows 7, Windows 2008 R2, or Windows 8.
  • .NET Framework 4.5 或更新版本。.NET Framework 4.5 or later.
  • Visual Studio。Visual Studio.

建立和設定 Visual Studio 專案Create and configure a Visual Studio project

本節說明如何在 Visual Studio 中建立專案並設定來進行媒體服務開發。This section shows you how to create a project in Visual Studio and set it up for Media Services development. 在此案例中,專案是 C# Windows 主控台應用程式,但這裡顯示的設定步驟也適用於可以為媒體服務應用程式建立的其他專案類型 (例如,Windows Forms 應用程式或 ASP.NET Web 應用程式)。In this case, the project is a C# Windows console application, but the same setup steps shown here apply to other types of projects you can create for Media Services applications (for example, a Windows Forms application or an ASP.NET Web application).

本節顯示如何使用 NuGet 新增 Media Services .NET SDK 延伸模組和其他相依程式庫。This section shows how to use NuGet to add Media Services .NET SDK extensions and other dependent libraries.

或者,您可以從 GitHub 取得最新 Media Services .NET SDK 位元 (github.com/Azure/azure-sdk-for-media-servicesgithub.com/Azure/azure-sdk-for-media-services-extensions)、建置方案,並新增至用戶端專案的參考。Alternatively, you can get the latest Media Services .NET SDK bits from GitHub (github.com/Azure/azure-sdk-for-media-services or github.com/Azure/azure-sdk-for-media-services-extensions), build the solution, and add the references to the client project. 所有必要相依性皆會自動下載並解壓縮。All the necessary dependencies get downloaded and extracted automatically.

  1. 在 Visual Studio 中,建立新的 C# 主控台應用程式。Create a new C# Console Application in Visual Studio. 輸入 [名稱] 、[位置] 和 [方案名稱] ,然後按一下 [確定]。Enter the Name, Location, and Solution name, and then click OK.

  2. 建置方案。Build the solution.

  3. 使用 NuGet 來安裝和新增 Azure 媒體服務 .NET SDK 延伸模組 (windowsazure.mediaservices.extensions)。Use NuGet to install and add Azure Media Services .NET SDK Extensions (windowsazure.mediaservices.extensions). 安裝這個封裝,也會安裝 Media Services .NET SDK ,並新增所有其他必要相依性。Installing this package, also installs Media Services .NET SDK and adds all other required dependencies.

    確定您已安裝 NuGet 的最新版本。Ensure that you have the newest version of NuGet installed. 如需詳細資訊和安裝指示,請參閱 NuGetFor more information and installation instructions, see NuGet.

    1. 在 [方案總管] 中,於專案名稱上按一下滑鼠右鍵,然後選擇 [管理 NuGet 套件] 。In Solution Explorer, right-click the name of the project and choose Manage NuGet Packages.

    2. [管理 NuGet 封裝] 對話方塊隨即出現。The Manage NuGet Packages dialog box appears.

    3. 在線上資源庫中,搜尋「Azure MediaServices 擴充功能」,選擇 [Azure Media Services .NET SDK 擴充功能] (windowsazure.mediaservices.extensions),然後按一下 [安裝] 按鈕。In the Online gallery, search for Azure MediaServices Extensions, choose Azure Media Services .NET SDK Extensions (windowsazure.mediaservices.extensions), and then click the Install button.

    4. 會修改專案,並新增 Media Services .NET SDK 延伸模組、Media Services .NET SDK 和其他相依組件的參考。The project is modified and references to the Media Services .NET SDK Extensions, Media Services .NET SDK, and other dependent assemblies are added.

  4. 若要提升更乾淨的開發環境,請考慮啟用 [NuGet 封裝還原]。To promote a cleaner development environment, consider enabling NuGet Package Restore. 如需詳細資訊,請參閱 NuGet 封裝還原For more information, see NuGet Package Restore".

  5. 加入 System.Configuration 組件的參考。Add a reference to System.Configuration assembly. 此組件包含用來存取組態檔 (例如 App.config) 的 System.Configuration.ConfigurationManager 類別。This assembly contains the System.Configuration.ConfigurationManager class that is used to access configuration files (for example, App.config).

    1. 若要使用 [管理參考] 對話方塊新增參考,請以滑鼠右鍵按一下 [方案總管] 中的專案名稱。To add references using the Manage References dialog, right-click the project name in the Solution Explorer. 接著,按一下 [新增] ,然後按一下 [參考] 。Then, click Add, then click Reference....

    2. [管理參考] 對話方塊隨即出現。The Manage References dialog appears.

    3. 在 .NET Framework 組件底下,尋找並選取 System.Configuration 組件,然後按 [確定] 。Under .NET framework assemblies, find and select the System.Configuration assembly and press OK.

  6. 開啟 App.config 檔案並將 appSettings 區段新增至檔案。Open the App.config file and add an appSettings section to the file. 設定連接媒體服務 API 時所需的值。Set the values that are needed to connect to the Media Services API. 如需詳細資訊,請參閱使用 Azure AD 驗證存取 Azure 媒體服務 APIFor more information, see Access the Azure Media Services API with Azure AD authentication.

    設定使用服務主體驗證方法連接時所需的值。Set the values that are needed to connect using the Service principal authentication method.

     ```csharp
             <configuration>
             ...
                 <appSettings>
                     <add key="AMSAADTenantDomain" value="tenant"/>
                     <add key="AMSRESTAPIEndpoint" value="endpoint"/>
                     <add key="AMSClientId" value="id"/>
                     <add key="AMSClientSecret" value="secret"/>
                 </appSettings>
             </configuration>
     ```
    
  7. System.Configuration 參考新增至專案。Add the System.Configuration reference to your project.

  8. 在 Program.cs 檔案的開頭,使用下列程式碼來覆寫現有的 using 陳述式:Overwrite the existing using statements at the beginning of the Program.cs file with the following code:

            using System;
            using System.Configuration;
            using System.IO;
            using Microsoft.WindowsAzure.MediaServices.Client;
            using System.Threading;
            using System.Collections.Generic;
            using System.Linq;
    

    現在,您可以開始開發媒體服務應用程式。At this point, you are ready to start developing a Media Services application.

範例Example

以下是一個小範例,它會連接到 AMS API,並列出所有可用的媒體處理器。Here is a small example that connects to the AMS API and lists all available Media Processors.

        class Program
        {
            // Read values from the App.config file.

            private static readonly string _AADTenantDomain =
                ConfigurationManager.AppSettings["AMSAADTenantDomain"];
            private static readonly string _RESTAPIEndpoint =
                ConfigurationManager.AppSettings["AMSRESTAPIEndpoint"];
            private static readonly string _AMSClientId =
                ConfigurationManager.AppSettings["AMSClientId"];
            private static readonly string _AMSClientSecret =
                ConfigurationManager.AppSettings["AMSClientSecret"];
        
            private static CloudMediaContext _context = null;
            static void Main(string[] args)
            {
                AzureAdTokenCredentials tokenCredentials = 
                    new AzureAdTokenCredentials(_AADTenantDomain,
                        new AzureAdClientSymmetricKey(_AMSClientId, _AMSClientSecret),
                        AzureEnvironments.AzureCloudEnvironment);

                var tokenProvider = new AzureAdTokenProvider(tokenCredentials);

                _context = new CloudMediaContext(new Uri(_RESTAPIEndpoint), tokenProvider);
        
                // List all available Media Processors
                foreach (var mp in _context.MediaProcessors)
                {
                    Console.WriteLine(mp.Name);
                }
        
            }

後續步驟Next steps

現在您可以連接到 AMS API並開始開發Now you can connect to the AMS API and start developing.

媒體服務學習路徑Media Services learning paths

媒體服務 v3 (最新版本)Media Services v3 (latest)

查看最新版本的 Azure 媒體服務!Check out the latest version of Azure Media Services!

媒體服務 v2 (舊版)Media Services v2 (legacy)

提供意見反應Provide feedback

利用 使用者心聲 論壇來提供意見並提出如何改善 Azure 媒體服務的建議。Use the User Voice forum to provide feedback and make suggestions on how to improve Azure Media Services. 您也可直接前往下列類別之一:You also can go directly to one of the following categories: