|
| 1 | +--- |
| 2 | +title: Create a streaming locator and build URLs - Azure Media Services |
| 3 | +description: This article demonstrates how to create a streaming locator and build URLs. |
| 4 | +services: media-services |
| 5 | +documentationcenter: '' |
| 6 | +author: Juliako |
| 7 | +manager: femila |
| 8 | +editor: '' |
| 9 | + |
| 10 | +ms.service: media-services |
| 11 | +ms.workload: |
| 12 | +ms.topic: article |
| 13 | +ms.date: 03/04/2020 |
| 14 | +ms.author: juliako |
| 15 | +--- |
| 16 | + |
| 17 | +# Create a streaming locator and build URLs |
| 18 | + |
| 19 | +In Azure Media Services, to build a streaming URL, you need to first create a [Streaming Locator](streaming-locators-concept.md). You then concatenate the [Streaming Endpoint](https://docs.microsoft.com/rest/api/media/streamingendpoints) host name and the **Streaming Locator** path. In this sample, the *default* **Streaming Endpoint** is used. When you first create a Media Service account, this *default* **Streaming Endpoint** will be in a stopped state, so you need to call **Start** to start streaming. |
| 20 | + |
| 21 | +This article demonstrates how to create a streaming locator and build a streaming URL using Java and .NET SDKs. |
| 22 | + |
| 23 | +## Prerequisite |
| 24 | + |
| 25 | +Preview [Dynamic packaging](dynamic-packaging-overview.md) |
| 26 | + |
| 27 | +## Java |
| 28 | + |
| 29 | +```java |
| 30 | +/** |
| 31 | +* Creates a StreamingLocator for the specified asset and with the specified streaming policy name. |
| 32 | +* Once the StreamingLocator is created the output asset is available to clients for playback. |
| 33 | +* @param manager The entry point of Azure Media resource management |
| 34 | +* @param resourceGroup The name of the resource group within the Azure subscription |
| 35 | +* @param accountName The Media Services account name |
| 36 | +* @param assetName The name of the output asset |
| 37 | +* @param locatorName The StreamingLocator name (unique in this case) |
| 38 | +* @return The locator created |
| 39 | +*/ |
| 40 | +private static StreamingLocator getStreamingLocator(MediaManager manager, String resourceGroup, String accountName, |
| 41 | + String assetName, String locatorName) { |
| 42 | + // Note that we are using one of the PredefinedStreamingPolicies which tell the Origin component |
| 43 | + // of Azure Media Services how to publish the content for streaming. |
| 44 | + System.out.println("Creating a streaming locator..."); |
| 45 | + StreamingLocator locator = manager |
| 46 | + .streamingLocators().define(locatorName) |
| 47 | + .withExistingMediaservice(resourceGroup, accountName) |
| 48 | + .withAssetName(assetName) |
| 49 | + .withStreamingPolicyName("Predefined_ClearStreamingOnly") |
| 50 | + .create(); |
| 51 | + |
| 52 | + return locator; |
| 53 | +} |
| 54 | + |
| 55 | +/** |
| 56 | +* Checks if the streaming endpoint is in the running state, if not, starts it. |
| 57 | +* @param manager The entry point of Azure Media resource management |
| 58 | +* @param resourceGroup The name of the resource group within the Azure subscription |
| 59 | +* @param accountName The Media Services account name |
| 60 | +* @param locatorName The name of the StreamingLocator that was created |
| 61 | +* @param streamingEndpoint The streaming endpoint. |
| 62 | +* @return List of streaming urls |
| 63 | +*/ |
| 64 | +private static List<String> getStreamingUrls(MediaManager manager, String resourceGroup, String accountName, |
| 65 | + String locatorName, StreamingEndpoint streamingEndpoint) { |
| 66 | + List<String> streamingUrls = new ArrayList<>(); |
| 67 | + |
| 68 | + ListPathsResponse paths = manager.streamingLocators().listPathsAsync(resourceGroup, accountName, locatorName) |
| 69 | + .toBlocking().first(); |
| 70 | + |
| 71 | + for (StreamingPath path: paths.streamingPaths()) { |
| 72 | + StringBuilder uriBuilder = new StringBuilder(); |
| 73 | + uriBuilder.append("https://") |
| 74 | + .append(streamingEndpoint.hostName()) |
| 75 | + .append("/") |
| 76 | + .append(path.paths().get(0)); |
| 77 | + |
| 78 | + streamingUrls.add(uriBuilder.toString()); |
| 79 | + } |
| 80 | + return streamingUrls; |
| 81 | +} |
| 82 | +``` |
| 83 | + |
| 84 | +See the full code sample: [EncodingWithMESPredefinedPreset](https://github.com/Azure-Samples/media-services-v3-java/blob/master/VideoEncoding/EncodingWithMESPredefinedPreset/src/main/java/sample/EncodingWithMESPredefinedPreset.java) |
| 85 | + |
| 86 | +## .NET |
| 87 | + |
| 88 | +```csharp |
| 89 | +/// <summary> |
| 90 | +/// Creates a StreamingLocator for the specified asset and with the specified streaming policy name. |
| 91 | +/// Once the StreamingLocator is created the output asset is available to clients for playback. |
| 92 | +/// </summary> |
| 93 | +/// <param name="client">The Media Services client.</param> |
| 94 | +/// <param name="resourceGroupName">The name of the resource group within the Azure subscription.</param> |
| 95 | +/// <param name="accountName"> The Media Services account name.</param> |
| 96 | +/// <param name="assetName">The name of the output asset.</param> |
| 97 | +/// <param name="locatorName">The StreamingLocator name (unique in this case).</param> |
| 98 | +/// <returns>A task.</returns> |
| 99 | +private static async Task<StreamingLocator> CreateStreamingLocatorAsync( |
| 100 | + IAzureMediaServicesClient client, |
| 101 | + string resourceGroup, |
| 102 | + string accountName, |
| 103 | + string assetName, |
| 104 | + string locatorName) |
| 105 | +{ |
| 106 | + Console.WriteLine("Creating a streaming locator..."); |
| 107 | + StreamingLocator locator = await client.StreamingLocators.CreateAsync( |
| 108 | + resourceGroup, |
| 109 | + accountName, |
| 110 | + locatorName, |
| 111 | + new StreamingLocator |
| 112 | + { |
| 113 | + AssetName = assetName, |
| 114 | + StreamingPolicyName = PredefinedStreamingPolicy.ClearStreamingOnly |
| 115 | + }); |
| 116 | + |
| 117 | + return locator; |
| 118 | +} |
| 119 | + |
| 120 | +/// <summary> |
| 121 | +/// Checks if the streaming endpoint is in the running state, |
| 122 | +/// if not, starts it. Then, builds the streaming URLs. |
| 123 | +/// </summary> |
| 124 | +/// <param name="client">The Media Services client.</param> |
| 125 | +/// <param name="resourceGroupName">The name of the resource group within the Azure subscription.</param> |
| 126 | +/// <param name="accountName"> The Media Services account name.</param> |
| 127 | +/// <param name="locatorName">The name of the StreamingLocator that was created.</param> |
| 128 | +/// <param name="streamingEndpoint">The streaming endpoint.</param> |
| 129 | +/// <returns>A task.</returns> |
| 130 | +private static async Task<IList<string>> GetStreamingUrlsAsync( |
| 131 | + IAzureMediaServicesClient client, |
| 132 | + string resourceGroupName, |
| 133 | + string accountName, |
| 134 | + String locatorName, |
| 135 | + StreamingEndpoint streamingEndpoint) |
| 136 | +{ |
| 137 | + IList<string> streamingUrls = new List<string>(); |
| 138 | + |
| 139 | + ListPathsResponse paths = await client.StreamingLocators.ListPathsAsync(resourceGroupName, accountName, locatorName); |
| 140 | + |
| 141 | + foreach (StreamingPath path in paths.StreamingPaths) |
| 142 | + { |
| 143 | + UriBuilder uriBuilder = new UriBuilder |
| 144 | + { |
| 145 | + Scheme = "https", |
| 146 | + Host = streamingEndpoint.HostName, |
| 147 | + |
| 148 | + Path = path.Paths[0] |
| 149 | + }; |
| 150 | + streamingUrls.Add(uriBuilder.ToString()); |
| 151 | + } |
| 152 | + |
| 153 | + return streamingUrls; |
| 154 | +} |
| 155 | +``` |
| 156 | + |
| 157 | +See the full code sample: [EncodingWithMESPredefinedPreset](https://github.com/Azure-Samples/media-services-v3-dotnet/blob/master/VideoEncoding/EncodingWithMESPredefinedPreset/Program.cs) |
| 158 | + |
| 159 | +## See also |
| 160 | + |
| 161 | +* [Create filters with .NET](filters-dynamic-manifest-dotnet-howto.md) |
| 162 | +* [Create filters with REST](filters-dynamic-manifest-rest-howto.md) |
| 163 | +* [Create filters with CLI](filters-dynamic-manifest-cli-howto.md) |
| 164 | + |
| 165 | +## Next steps |
| 166 | + |
| 167 | +[Protect your content with DRM](protect-with-drm.md). |
0 commit comments