private async Task LoadSourceFromUriAsync(Uri uri, HttpClient httpClient = null) { mediaPlayerElement.MediaPlayer?.DisposeSource(); AdaptiveMediaSourceCreationResult result = null; if (httpClient != null) { result = await AdaptiveMediaSource.CreateFromUriAsync(uri, httpClient); } else { result = await AdaptiveMediaSource.CreateFromUriAsync(uri); } if (result.Status == AdaptiveMediaSourceCreationStatus.Success) { adaptiveMS = result.MediaSource; // Register for events before setting the IMediaPlaybackSource RegisterForAdaptiveMediaSourceEvents(adaptiveMS); // Now that we have bitrates, attempt to cap them based on HdcpProtection. HdcpProtection?protection = hdcpSession.GetEffectiveProtection(); SetMaxBitrateForProtectionLevel(protection, adaptiveMS); MediaSource source = MediaSource.CreateFromAdaptiveMediaSource(adaptiveMS); // Note that in this sample with very few event handlers, we are creating neither // a MediaPlayer nor a MediaPlaybackItem. The MediaPlayer will be created implicitly // by the mpElement, which will also manage its lifetime. mediaPlayerElement.Source = source; // You can now access mpElement.MediaPlayer, but it is too late to register // to handle its MediaOpened event. } else { Log($"Error creating the AdaptiveMediaSource: {result.Status}"); } }
private async Task <MediaPlaybackItem> LoadSourceFromUriAsync(Uri uri, HttpClient httpClient = null) { UnregisterHandlers(mediaPlayerElement.MediaPlayer); if (tokenMethod == AzureKeyAcquisitionMethod.AuthorizationHeader) { if (ContentSelectorControl.SelectedModel == null) { return(null); } // Use an IHttpFilter to identify key request URIs and insert an Authorization header with the Bearer token. var baseProtocolFilter = new HttpBaseProtocolFilter(); baseProtocolFilter.CacheControl.WriteBehavior = HttpCacheWriteBehavior.NoCache; // Always set WriteBehavior = NoCache httpClientFilter = new AddAuthorizationHeaderFilter(baseProtocolFilter); httpClientFilter.AuthorizationHeader = new HttpCredentialsHeaderValue("Bearer", ContentSelectorControl.SelectedModel.AesToken); httpClient = new HttpClient(httpClientFilter); // Here is where you can add any required custom CDN headers. httpClient.DefaultRequestHeaders.Append("X-HeaderKey", "HeaderValue"); // NOTE: It is not recommended to set Authorization headers needed for key request on the // default headers of the HttpClient, as these will also be used on non-HTTPS calls // for media segments and manifests -- and thus will be easily visible. } AdaptiveMediaSourceCreationResult result = null; if (httpClient != null) { result = await AdaptiveMediaSource.CreateFromUriAsync(uri, httpClient); } else { result = await AdaptiveMediaSource.CreateFromUriAsync(uri); } MediaSource source; if (result.Status == AdaptiveMediaSourceCreationStatus.Success) { adaptiveMediaSource = result.MediaSource; // We use a helper class that logs all the events for the AdaptiveMediaSource: adaptiveMediaSourceLogger = new AdaptiveMediaSourceLogger(LoggerControl, adaptiveMediaSource); // In addition to logging, we use the callbacks to update some UI elements in this scenario: RegisterForAdaptiveMediaSourceEvents(adaptiveMediaSource); // At this point, we have read the manifest of the media source, and all bitrates are known. // Now that we have bitrates, attempt to cap them based on HdcpProtection. HdcpProtection?protection = hdcpSession.GetEffectiveProtection(); SetMaxBitrateForProtectionLevel(protection, adaptiveMediaSource); source = MediaSource.CreateFromAdaptiveMediaSource(adaptiveMediaSource); } else { Log($"Error creating the AdaptiveMediaSource. Status: {result.Status}, ExtendedError.Message: {result.ExtendedError.Message}, ExtendedError.HResult: {result.ExtendedError.HResult.ToString("X8")}"); return(null); } // We use a helper class that logs all the events for the MediaSource: mediaSourceLogger = new MediaSourceLogger(LoggerControl, source); // Save the original Uri. source.CustomProperties["uri"] = uri.ToString(); // You're likely to put a content tracking id into the CustomProperties. source.CustomProperties["contentId"] = Guid.NewGuid().ToString(); var mpItem = new MediaPlaybackItem(source); // We use a helper class that logs all the events for the MediaPlaybackItem: mediaPlaybackItemLogger = new MediaPlaybackItemLogger(LoggerControl, mpItem); HideDescriptionOnSmallScreen(); return(mpItem); }