Share via


Azure Media Services: How to Create an Application

Introduction

Before you can get started developing your first AMS solution you need the following:

Create your Visual Studio Project

The AMS SDK can be installed using NuGet. So, open Visual Studio and create a new C# Console Application.


When the project is created, click Tools -> NuGet Package Manager -> Manage NuGet Packages for Solution.

In the search box, type windowsazure.mediaservices.extensions, select the package and click install.

Add a reference to the System.Configuration assembly to your project. This assembly is used to add the Media Service account name and the account key (which can be obtained from the Azure Portal, refer to this post: https://social.technet.microsoft.com/wiki/contents/articles/37560.create-a-azure-media-services-account.aspx) to the App.config.

Add the below code to the App.Config file:

<appSettings>
  <add key="MediaServicesAccountName" value="Account Name" />
  <add key="MediaServicesAccountKey" value="Account Key" />
</appSettings>

Replace the values with your Account Name and the Account Key.
Add the following using statements to the program.cs file:

using System.Configuration;
using System.Threading;
using System.IO;
using Microsoft.WindowsAzure.MediaServices.Client;

Inside your class, add the following code:

class Program
{
   // Read values from the App.config file.
   private static readonly string _mediaServicesAccountName =
   ConfigurationManager.AppSettings["MediaServicesAccountName"];
  
   private static readonly string _mediaServicesAccountKey =
   ConfigurationManager.AppSettings["MediaServicesAccountKey"];
  
   private static CloudMediaContext _context = null;
   private static MediaServicesCredentials _cachedCredentials = null;
  
   static void Main(string[] args)
   {
      try
      {
         _cachedCredentials = new MediaServicesCredentials(
         _mediaServicesAccountName,
         _mediaServicesAccountKey);
         _context = new CloudMediaContext(_cachedCredentials);
  
         IAsset inputAsset =
         UploadFile(@"C:\SampleVideo.mp4", AssetCreationOptions.None);
       }
       catch (Exception exception)
       {
          exception = MediaServicesExceptionParser.Parse(exception);
          Console.Error.WriteLine(exception.Message);
       }
       finally
       {
          Console.ReadLine();
       }
   }
  
   static public IAsset UploadFile(string fileName, AssetCreationOptions options)
   {
      IAsset inputAsset = _context.Assets.CreateFromFile(
         fileName,
         options,
         (af, p) =>
         {
               Console.WriteLine("Uploading '{0}' - Progress: {1:0.##}%", af.Name, p.Progress);
         });
      Console.WriteLine("Asset {0} created.", inputAsset.Id);
      return inputAsset;
   }
}

When you run this code, the video gets uploaded to the storage account of your Media Services. If you open the Media Services account in the Azure Portal you can see that it’s there.

See also

References