title | description | author | manager | editor | services | documentationcenter | ms.assetid | ms.service | ms.workload | ms.tgt_pltfrm | ms.devlang | ms.topic | ms.date | ms.author |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Publish Azure Media Services content using .NET | Microsoft Docs |
Learn how to create a locator that is used to build a streaming URL. Code samples are written in C# and use the Media Services SDK for .NET. |
juliako |
cfowler |
media-services |
c53b1f83-4cb1-4b09-840f-9c145b7d6f8d |
media-services |
media |
na |
na |
article |
08/09/2017 |
juliako |
[!div class="op_single_selector"]
You can stream an adaptive bitrate MP4 set by creating an OnDemand streaming locator and building a streaming URL. The encoding an asset topic shows how to encode into an adaptive bitrate MP4 set.
Note
If your content is encrypted, configure asset delivery policy (as described in this topic) before creating a locator.
You can also use an OnDemand streaming locator to build URLs that point to MP4 files that can be progressively downloaded.
This topic shows how to create an OnDemand streaming locator to publish your asset and build a Smooth, MPEG DASH, and HLS streaming URLs. It also shows hot to build progressive download URLs.
To create the OnDemand streaming locator and get URLs, you need to do the following things:
-
If the content is encrypted, define an access policy.
-
Create an OnDemand streaming locator.
-
If you plan to stream, get the streaming manifest file (.ism) in the asset.
If you plan to progressively download, get the names of MP4 files in the asset.
-
Build URLs to the manifest file or MP4 files.
Note
There is a limit of 1,000,000 policies for different AMS policies (for example, for Locator policy or ContentKeyAuthorizationPolicy). Use the same policy ID if you are always using the same days / access permissions. For example, policies for locators that are intended to remain in place for a long time (non-upload policies). For more information, see this topic.
Build Streaming URLs
private static void BuildStreamingURLs(IAsset asset)
{
// Create a 30-day readonly access policy.
// You cannot create a streaming locator using an AccessPolicy that includes write or delete permissions.
IAccessPolicy policy = _context.AccessPolicies.Create("Streaming policy",
TimeSpan.FromDays(30),
AccessPermissions.Read);
// Create a locator to the streaming content on an origin.
ILocator originLocator = _context.Locators.CreateLocator(LocatorType.OnDemandOrigin, asset,
policy,
DateTime.UtcNow.AddMinutes(-5));
// Display some useful values based on the locator.
Console.WriteLine("Streaming asset base path on origin: ");
Console.WriteLine(originLocator.Path);
Console.WriteLine();
// Get a reference to the streaming manifest file from the
// collection of files in the asset.
var manifestFile = asset.AssetFiles.Where(f => f.Name.ToLower().
EndsWith(".ism")).
FirstOrDefault();
// Create a full URL to the manifest file. Use this for playback
// in streaming media clients.
string urlForClientStreaming = originLocator.Path + manifestFile.Name + "/manifest";
Console.WriteLine("URL to manifest for client streaming using Smooth Streaming protocol: ");
Console.WriteLine(urlForClientStreaming);
Console.WriteLine("URL to manifest for client streaming using HLS protocol: ");
Console.WriteLine(urlForClientStreaming + "(format=m3u8-aapl)");
Console.WriteLine("URL to manifest for client streaming using MPEG DASH protocol: ");
Console.WriteLine(urlForClientStreaming + "(format=mpd-time-csf)");
Console.WriteLine();
}
The outputs:
URL to manifest for client streaming using Smooth Streaming protocol:
http://amstest1.streaming.mediaservices.windows.net/3c5fe676-199c-4620-9b03-ba014900f214/BigBuckBunny.ism/manifest
URL to manifest for client streaming using HLS protocol:
http://amstest1.streaming.mediaservices.windows.net/3c5fe676-199c-4620-9b03-ba014900f214/BigBuckBunny.ism/manifest(format=m3u8-aapl)
URL to manifest for client streaming using MPEG DASH protocol:
http://amstest1.streaming.mediaservices.windows.net/3c5fe676-199c-4620-9b03-ba014900f214/BigBuckBunny.ism/manifest(format=mpd-time-csf)
Note
You can also stream your content over an SSL connection. To do this approach, make sure your streaming URLs start with HTTPS. Currently, AMS doesn’t support SSL with custom domains.
Build progressive download URLs
private static void BuildProgressiveDownloadURLs(IAsset asset)
{
// Create a 30-day readonly access policy.
IAccessPolicy policy = _context.AccessPolicies.Create("Streaming policy",
TimeSpan.FromDays(30),
AccessPermissions.Read);
// Create an OnDemandOrigin locator to the asset.
ILocator originLocator = _context.Locators.CreateLocator(LocatorType.OnDemandOrigin, asset,
policy,
DateTime.UtcNow.AddMinutes(-5));
// Display some useful values based on the locator.
Console.WriteLine("Streaming asset base path on origin: ");
Console.WriteLine(originLocator.Path);
Console.WriteLine();
// Get MP4 files.
IEnumerable<IAssetFile> mp4AssetFiles = asset
.AssetFiles
.ToList()
.Where(af => af.Name.EndsWith(".mp4", StringComparison.OrdinalIgnoreCase));
// Create a full URL to the MP4 files. Use this to progressively download files.
foreach (var pd in mp4AssetFiles)
Console.WriteLine(originLocator.Path + pd.Name);
}
The outputs:
http://amstest1.streaming.mediaservices.windows.net/3c5fe676-199c-4620-9b03-ba014900f214/BigBuckBunny_H264_650kbps_AAC_und_ch2_96kbps.mp4
http://amstest1.streaming.mediaservices.windows.net/3c5fe676-199c-4620-9b03-ba014900f214/BigBuckBunny_H264_400kbps_AAC_und_ch2_96kbps.mp4
http://amstest1.streaming.mediaservices.windows.net/3c5fe676-199c-4620-9b03-ba014900f214/BigBuckBunny_H264_3400kbps_AAC_und_ch2_96kbps.mp4
http://amstest1.streaming.mediaservices.windows.net/3c5fe676-199c-4620-9b03-ba014900f214/BigBuckBunny_H264_2250kbps_AAC_und_ch2_96kbps.mp4
. . .
The following code calls .NET SDK extensions methods that create a locator and generate the Smooth Streaming, HLS, and MPEG-DASH URLs for adaptive streaming.
// Create a loctor.
_context.Locators.Create(
LocatorType.OnDemandOrigin,
inputAsset,
AccessPermissions.Read,
TimeSpan.FromDays(30));
// Get the streaming URLs.
Uri smoothStreamingUri = inputAsset.GetSmoothStreamingUri();
Uri hlsUri = inputAsset.GetHlsUri();
Uri mpegDashUri = inputAsset.GetMpegDashUri();
Console.WriteLine(smoothStreamingUri);
Console.WriteLine(hlsUri);
Console.WriteLine(mpegDashUri);
[!INCLUDE media-services-learning-paths-include]
[!INCLUDE media-services-user-voice-include]