Example #1
0
        private async Task <MediaPlaybackItem> LoadSourceFromUriAsync(Uri uri, HttpClient httpClient = null)
        {
            UnregisterHandlers(mediaPlayerElement.MediaPlayer);
            mediaPlayerElement.MediaPlayer?.DisposeSource();

            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.
                bitrateHelper = new BitrateHelper(adaptiveMediaSource.AvailableBitrates);
                InitializeBitrateLists(adaptiveMediaSource);
                await UpdatePlaybackBitrateAsync(adaptiveMediaSource.CurrentPlaybackBitrate);
                await UpdateDownloadBitrateAsync(adaptiveMediaSource.CurrentDownloadBitrate);

                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);
        }
        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;

                bitrateHelper = new BitrateHelper(adaptiveMS.AvailableBitrates);
                InitializeBitrateLists(adaptiveMS);

                // At this point, we have read the manifest of the media source, and all bitrates are known.
                await UpdatePlaybackBitrate(adaptiveMS.CurrentPlaybackBitrate);
                await UpdateDownloadBitrate(adaptiveMS.CurrentDownloadBitrate);

                // Register for events before resolving the MediaSource.
                RegisterForAdaptiveMediaSourceEvents(adaptiveMS);

                MediaSource source = MediaSource.CreateFromAdaptiveMediaSource(adaptiveMS);

                // You can save additional information in the CustomPropertySet for future retrieval.
                // Note: MediaSource.CustomProperties is a ValueSet and therefore can store
                // only serializable types.

                // Save the original Uri.
                source.CustomProperties.Add("uri", uri.ToString());

                // You're likely to put a content tracking id into the CustomProperties.
                source.CustomProperties.Add("contentId", Guid.NewGuid());

                mpItem = new MediaPlaybackItem(source);
            }
            else
            {
                Log("Error creating the AdaptiveMediaSource: " + result.Status);
            }
        }
        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;

                bitrateHelper = new BitrateHelper(adaptiveMS.AvailableBitrates);
                InitializeBitrateLists(adaptiveMS);

                // At this point, we have read the manifest of the media source, and all bitrates are known.
                await UpdatePlaybackBitrate(adaptiveMS.CurrentPlaybackBitrate);
                await UpdateDownloadBitrate(adaptiveMS.CurrentDownloadBitrate);

                // Register for events before resolving the MediaSource.
                RegisterForAdaptiveMediaSourceEvents(adaptiveMS);

                MediaSource source = MediaSource.CreateFromAdaptiveMediaSource(adaptiveMS);

                // You can save additional information in the CustomPropertySet for future retrieval.
                // Note: MediaSource.CustomProperties is a ValueSet and therefore can store
                // only serializable types.

                // Save the original Uri.
                source.CustomProperties.Add("uri", uri.ToString());

                // You're likely to put a content tracking id into the CustomProperties.
                source.CustomProperties.Add("contentId", Guid.NewGuid());

                mpItem = new MediaPlaybackItem(source);
            }
            else
            {
                Log("Error creating the AdaptiveMediaSource: " + result.Status);
            }
        }
        private async Task LoadSourceFromUriAsync(Uri uri, HttpClient httpClient = null)
        {
            if (mediaPlayerElement.MediaPlayer?.Source != null)
            {
                UnregisterHandlers(mediaPlayerElement.MediaPlayer);
                mediaPlayerElement.MediaPlayer.DisposeSource();
            }

            AdaptiveMediaSourceCreationResult result = null;
            if (httpClient != null)
            {
                result = await AdaptiveMediaSource.CreateFromUriAsync(uri, httpClient);
            }
            else
            {
                result = await AdaptiveMediaSource.CreateFromUriAsync(uri);
            }

            // We don't need to save a reference to the MediaSource,
            // because we can obtain it from the MediaPlaybackItem.Source in event handlers.
            MediaSource source;

            if (result.Status == AdaptiveMediaSourceCreationStatus.Success)
            {
                adaptiveMediaSource = result.MediaSource;

                // At this point, we have read the manifest of the media source, and all bitrates are known.
                bitrateHelper = new BitrateHelper(adaptiveMediaSource.AvailableBitrates);

                // The AdaptiveMediaSource chooses initial playback and download bitrates.
                // See the Tuning scenario for examples of customizing these bitrates.
                await UpdatePlaybackBitrate(adaptiveMediaSource.CurrentPlaybackBitrate);
                await UpdateDownloadBitrateAsync(adaptiveMediaSource.CurrentDownloadBitrate);

                // Register for events before resolving the MediaSource.
                RegisterForAdaptiveMediaSourceEvents(adaptiveMediaSource);

                source = MediaSource.CreateFromAdaptiveMediaSource(adaptiveMediaSource);
            }
            else
            {
                Log($"Error creating the AdaptiveMediaSource: {result.Status}");

                // Try to load the URI as a progressive download URI.
                Log($"Attempting to create a MediaSource from uri: {uri}");
                source = MediaSource.CreateFromUri(uri);
            }

            // You can save additional information in the CustomPropertySet for future retrieval.
            // Note: MediaSource.CustomProperties is a ValueSet and therefore can store
            // only serializable types.

            // Save the original Uri.
            source.CustomProperties.Add("uri", uri.ToString());

            // You're likely to put a content tracking id into the CustomProperties.
            source.CustomProperties.Add("contentId", Guid.NewGuid());

            RegisterForMediaSourceEvents(source);

            // Register for events before resolving the MediaSource.
            var mpItem = new MediaPlaybackItem(source);
            RegisterForMediaPlaybackItemEvents(mpItem);

            // It is at this point that the MediaSource (within a MediaPlaybackItem) will be fully resolved.
            // It gets opened, and events start being raised.

            // Since we are in an async function, the user may have user navigated away, which will null the MediaPlayer.
            if (mediaPlayerElement.MediaPlayer != null)
            {
                mediaPlayerElement.MediaPlayer.Source = mpItem;
            }
        }
        private async Task LoadSourceFromUriAsync(Uri uri, HttpClient httpClient = null)
        {
            if (mediaPlayerElement.MediaPlayer?.Source != null)
            {
                UnregisterHandlers(mediaPlayerElement.MediaPlayer);
                mediaPlayerElement.MediaPlayer.DisposeSource();
            }

            AdaptiveMediaSourceCreationResult result = null;

            if (httpClient != null)
            {
                result = await AdaptiveMediaSource.CreateFromUriAsync(uri, httpClient);
            }
            else
            {
                result = await AdaptiveMediaSource.CreateFromUriAsync(uri);
            }

            // We don't need to save a reference to the MediaSource,
            // because we can obtain it from the MediaPlaybackItem.Source in event handlers.
            MediaSource source;

            if (result.Status == AdaptiveMediaSourceCreationStatus.Success)
            {
                adaptiveMediaSource = result.MediaSource;

                // At this point, we have read the manifest of the media source, and all bitrates are known.
                bitrateHelper = new BitrateHelper(adaptiveMediaSource.AvailableBitrates);

                // The AdaptiveMediaSource chooses initial playback and download bitrates.
                // See the Tuning scenario for examples of customizing these bitrates.
                await UpdatePlaybackBitrate(adaptiveMediaSource.CurrentPlaybackBitrate);
                await UpdateDownloadBitrateAsync(adaptiveMediaSource.CurrentDownloadBitrate);

                // Register for events before resolving the MediaSource.
                RegisterForAdaptiveMediaSourceEvents(adaptiveMediaSource);

                source = MediaSource.CreateFromAdaptiveMediaSource(adaptiveMediaSource);
            }
            else
            {
                Log($"Error creating the AdaptiveMediaSource: {result.Status}");

                // Try to load the URI as a progressive download URI.
                Log($"Attempting to create a MediaSource from uri: {uri}");
                source = MediaSource.CreateFromUri(uri);
            }

            // You can save additional information in the CustomPropertySet for future retrieval.
            // Note: MediaSource.CustomProperties is a ValueSet and therefore can store
            // only serializable types.

            // Save the original Uri.
            source.CustomProperties.Add("uri", uri.ToString());

            // You're likely to put a content tracking id into the CustomProperties.
            source.CustomProperties.Add("contentId", Guid.NewGuid());

            RegisterForMediaSourceEvents(source);

            // Register for events before resolving the MediaSource.
            var mpItem = new MediaPlaybackItem(source);

            RegisterForMediaPlaybackItemEvents(mpItem);

            // It is at this point that the MediaSource (within a MediaPlaybackItem) will be fully resolved.
            // It gets opened, and events start being raised.

            // Since we are in an async function, the user may have user navigated away, which will null the MediaPlayer.
            if (mediaPlayerElement.MediaPlayer != null)
            {
                mediaPlayerElement.MediaPlayer.Source = mpItem;
            }
        }
        private async Task <MediaPlaybackItem> LoadSourceFromUriAsync(Uri uri, HttpClient httpClient = null)
        {
            UnregisterHandlers(mediaPlayerElement.MediaPlayer);

            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)
            {
                var 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.
                bitrateHelper = new BitrateHelper(adaptiveMediaSource.AvailableBitrates);

                // The AdaptiveMediaSource chooses initial playback and download bitrates.
                // See the Tuning scenario for examples of customizing these bitrates.
                await UpdatePlaybackBitrateAsync(adaptiveMediaSource.CurrentPlaybackBitrate);
                await UpdateDownloadBitrateAsync(adaptiveMediaSource.CurrentDownloadBitrate);

                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")}");

                // In this scenario, we make a second attempt to load any URI that is
                // not adaptive streaming as a progressive download URI:
                Log($"Attempting to create a MediaSource from uri: {uri}");
                source = MediaSource.CreateFromUri(uri);
            }

            // We use a helper class that logs all the events for the MediaSource:
            mediaSourceLogger = new MediaSourceLogger(LoggerControl, source);

            // You can save additional information in the CustomPropertySet for future retrieval.
            // Note: MediaSource.CustomProperties is a ValueSet and therefore can store
            // only serializable types.

            // 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);
        }