Ejemplo n.º 1
0
        // </SnippetDeclareHttpClient>

        async private void InitializeAdaptiveMediaSourceWithCustomHeaders(System.Uri uri)
        {
            System.Uri manifestUri = new Uri("http://amssamples.streaming.mediaservices.windows.net/49b57c87-f5f3-48b3-ba22-c55cfdffa9cb/Sintel.ism/manifest(format=m3u8-aapl)");

            // <SnippetInitializeAMSWithHttpClient>
            httpClient = new Windows.Web.Http.HttpClient();
            httpClient.DefaultRequestHeaders.TryAppendWithoutValidation("X-CustomHeader", "This is a custom header");
            AdaptiveMediaSourceCreationResult result = await AdaptiveMediaSource.CreateFromUriAsync(manifestUri, httpClient);

            // </SnippetInitializeAMSWithHttpClient>

            if (result.Status == AdaptiveMediaSourceCreationStatus.Success)
            {
                ams = result.MediaSource;
                mediaPlayerElement.SetMediaPlayer(new MediaPlayer());
                mediaPlayerElement.MediaPlayer.Source = MediaSource.CreateFromAdaptiveMediaSource(ams);
                mediaPlayerElement.MediaPlayer.Play();

                txtDownloadBitrate.Text = ams.InitialBitrate.ToString();
                txtPlaybackBitrate.Text = ams.InitialBitrate.ToString();

                //Register for download requests
                ams.DownloadRequested += DownloadRequested;

                //Register for bitrate change events
                ams.DownloadBitrateChanged += DownloadBitrateChanged;
                ams.PlaybackBitrateChanged += PlaybackBitrateChanged;
            }
            else
            {
                // Handle failure to create the adaptive media source
                MyLogMessageFunction($"Adaptive source creation failed: {manifestUri} - {result.ExtendedError}");
            }
        }
        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);

                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"] = "MainContent_ID";

            // In addition to logging, this scenario uses MediaSource events:
            RegisterForMediaSourceEvents(source);

            var mpItem = new MediaPlaybackItem(source);

            // We use a helper class that logs all the events for the MediaPlaybackItem:
            mediaPlaybackItemLogger = new MediaPlaybackItemLogger(LoggerControl, mpItem);

            // In addition to logging, this scenario uses MediaPlaybackItem events:
            RegisterForMediaPlaybackItemEvents(mpItem);

            // Now that we should have a MediaPlaybackItem, we will insert pre- mid- and post-roll ads
            // with the MediaBreak API.
            CreateMediaBreaksForItem(mpItem);

            HideDescriptionOnSmallScreen();

            return(mpItem);
        }
        async private void InitializeAdaptiveMediaSource(System.Uri uri, MediaElement m)
        {
            AdaptiveMediaSourceCreationResult result = await AdaptiveMediaSource.CreateFromUriAsync(uri);

            if (result.Status == AdaptiveMediaSourceCreationStatus.Success)
            {
                ams = result.MediaSource;
                m.SetMediaStreamSource(ams);

                outputBitrates(); //query for available bitrates and output to the log
                txtDownloadBitrate.Text = ams.InitialBitrate.ToString();
                txtPlaybackBitrate.Text = ams.InitialBitrate.ToString();

                //Register for download requests
                ams.DownloadRequested += DownloadRequested;

                //Register for bitrate change events
                ams.DownloadBitrateChanged += DownloadBitrateChanged;
                ams.PlaybackBitrateChanged += PlaybackBitrateChanged;
            }
            else
            {
                rootPage.NotifyUser("Error creating the AdaptiveMediaSource\n\t" + result.Status, NotifyType.ErrorMessage);
            }
        }
Ejemplo n.º 4
0
        // </SnippetDeclareAMS>

        // <SnippetInitializeAMS>
        async private void InitializeAdaptiveMediaSource(System.Uri uri)
        {
            AdaptiveMediaSourceCreationResult result = await AdaptiveMediaSource.CreateFromUriAsync(uri);

            if (result.Status == AdaptiveMediaSourceCreationStatus.Success)
            {
                ams = result.MediaSource;
                mediaPlayerElement.SetMediaPlayer(new MediaPlayer());
                mediaPlayerElement.MediaPlayer.Source = MediaSource.CreateFromAdaptiveMediaSource(ams);
                mediaPlayerElement.MediaPlayer.Play();


                ams.InitialBitrate = ams.AvailableBitrates.Max <uint>();

                //Register for download requests
                ams.DownloadRequested += DownloadRequested;

                //Register for download failure and completion events
                ams.DownloadCompleted += DownloadCompleted;
                ams.DownloadFailed    += DownloadFailed;

                //Register for bitrate change events
                ams.DownloadBitrateChanged += DownloadBitrateChanged;
                ams.PlaybackBitrateChanged += PlaybackBitrateChanged;

                //Register for diagnostic event
                ams.Diagnostics.DiagnosticAvailable += DiagnosticAvailable;
            }
            else
            {
                // Handle failure to create the adaptive media source
                MyLogMessageFunction($"Adaptive source creation failed: {uri} - {result.ExtendedError}");
            }
        }
Ejemplo n.º 5
0
        //</SnippetDeclareAMS>

        //<SnippetInitializeAMS>
        async private void InitializeAdaptiveMediaSource(System.Uri uri)
        {
            AdaptiveMediaSourceCreationResult result = await AdaptiveMediaSource.CreateFromUriAsync(uri);

            if (result.Status == AdaptiveMediaSourceCreationStatus.Success)
            {
                ams = result.MediaSource;
                mediaPlayerElement.SetMediaPlayer(new MediaPlayer());
                mediaPlayerElement.MediaPlayer.SetMediaSource(ams);
                mediaPlayerElement.MediaPlayer.Play();


                ams.InitialBitrate = ams.AvailableBitrates.Max <uint>();

                //Register for download requests
                ams.DownloadRequested += DownloadRequested;

                //Register for bitrate change events
                ams.DownloadBitrateChanged += DownloadBitrateChanged;
                ams.PlaybackBitrateChanged += PlaybackBitrateChanged;
            }
            else
            {
                // Handle failure to create the adaptive media source
            }
        }
Ejemplo n.º 6
0
        async private void InitializeAdaptiveMediaSource(System.Uri uri)
        {
            manifestUri = uri;
            httpClient.DefaultRequestHeaders.TryAppendWithoutValidation("X-CustomHeader", "This is a custom header");
            AdaptiveMediaSourceCreationResult result = await AdaptiveMediaSource.CreateFromUriAsync(manifestUri, httpClient);


            if (result.Status == AdaptiveMediaSourceCreationStatus.Success)
            {
                ams = result.MediaSource;
                mediaElement.SetMediaStreamSource(ams);


                ams.InitialBitrate = ams.AvailableBitrates.Max <uint>();

                //Register for download requests
                //     ams.DownloadRequested += DownloadRequested;

                //Register for bitrate change events
                //            ams.DownloadBitrateChanged += DownloadBitrateChanged;
                //              ams.PlaybackBitrateChanged += PlaybackBitrateChanged;
            }
            else
            {
                // Handle failure to create the adaptive media source
            }
        }
Ejemplo n.º 7
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);
        }
Ejemplo n.º 8
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)
            {
                var adaptiveMediaSource = result.MediaSource;

                // We use a helper class that logs all the events for the AdaptiveMediaSource:
                adaptiveMediaSourceLogger = new AdaptiveMediaSourceLogger(LoggerControl, adaptiveMediaSource);

                source = MediaSource.CreateFromAdaptiveMediaSource(adaptiveMediaSource);

                if (adaptiveMediaSource.MaxSeekableWindowSize.HasValue)
                {
                    UpdateSeekableWindowControls(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);
        }
        async private void InitializeAdaptiveMediaSource(System.Uri uri, MediaElement m)
        {
            AdaptiveMediaSourceCreationResult result = await AdaptiveMediaSource.CreateFromUriAsync(uri);

            if (result.Status == AdaptiveMediaSourceCreationStatus.Success)
            {
                ams = result.MediaSource;
                SetUpProtectionManager(ref m);
                m.SetMediaStreamSource(ams);
            }
            else
            {
                rootPage.NotifyUser("Error creating the AdaptiveMediaSource\n\t" + result.Status, NotifyType.ErrorMessage);
            }
        }
Ejemplo n.º 10
0
        // <SnippetBinderBindingAMS>
        private async void Binder_Binding_AdaptiveMediaSource(MediaBinder sender, MediaBindingEventArgs args)
        {
            var deferral = args.GetDeferral();

            var contentUri = new Uri("http://contoso.com/media/" + args.MediaBinder.Token);
            AdaptiveMediaSourceCreationResult result = await AdaptiveMediaSource.CreateFromUriAsync(contentUri);

            if (result.MediaSource != null)
            {
                args.SetAdaptiveMediaSource(result.MediaSource);
            }
            args.SetUri(contentUri);

            deferral.Complete();
        }
Ejemplo n.º 11
0
        public async void InitEmsgCueScenario()
        {
            // <SnippetEmsgLoadContent>
            AdaptiveMediaSourceCreationResult result =
                await AdaptiveMediaSource.CreateFromUriAsync(new Uri("http://contoso.com/playlist.m3u"));

            if (result.Status != AdaptiveMediaSourceCreationStatus.Success)
            {
                // TODO: Handle adaptive media source creation errors.
                return;
            }
            var mediaSource       = MediaSource.CreateFromAdaptiveMediaSource(result.MediaSource);
            var mediaPlaybackItem = new MediaPlaybackItem(mediaSource);

            // </SnippetEmsgLoadContent>

            // <SnippetEmsgCueTracksChanged>
            mediaPlaybackItem.TimedMetadataTracksChanged += (MediaPlaybackItem sender, IVectorChangedEventArgs args) =>
            {
                if (args.CollectionChange == CollectionChange.ItemInserted)
                {
                    RegisterMetadataHandlerForEmsgCues(sender, (int)args.Index);
                }
                else if (args.CollectionChange == CollectionChange.Reset)
                {
                    for (int index = 0; index < sender.TimedMetadataTracks.Count; index++)
                    {
                        if (sender.TimedMetadataTracks[index].TimedMetadataKind == TimedMetadataKind.ImageSubtitle)
                        {
                            RegisterMetadataHandlerForEmsgCues(sender, index);
                        }
                    }
                }
            };

            for (int index = 0; index < mediaPlaybackItem.TimedMetadataTracks.Count; index++)
            {
                RegisterMetadataHandlerForEmsgCues(mediaPlaybackItem, index);
            }
            // </SnippetEmsgCueTracksChanged>

            // <SnippetEmsgCuePlay>
            _mediaPlayer = new MediaPlayer();
            mediaPlayerElement.SetMediaPlayer(_mediaPlayer);
            _mediaPlayer.Source = mediaPlaybackItem;
            _mediaPlayer.Play();
            // </SnippetEmsgCuePlay>
        }
        private async Task PlayHLS(Uri hsl)
        {
            AdaptiveMediaSourceCreationResult result = await AdaptiveMediaSource.CreateFromUriAsync(hsl);

            if (result.Status == AdaptiveMediaSourceCreationStatus.Success)
            {
                _source = result.MediaSource;
                _source.DownloadRequested      += _source_DownloadRequested;
                _source.DownloadCompleted      += _source_DownloadCompleted;
                _source.DownloadFailed         += _source_DownloadFailed;
                _source.DownloadBitrateChanged += _source_DownloadBitrateChanged;
                _source.PlaybackBitrateChanged += _source_PlaybackBitrateChanged;

                BackgroundMediaPlayer.Current.SetMediaSource(result.MediaSource);
            }
        }
        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)
            {
                var adaptiveMS = result.MediaSource;

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

                RegisterForMediaSourceEvents(source);

                // Register for events before resolving the MediaSource.
                mpItem = new MediaPlaybackItem(source);
                RegisterForMediaPlaybackItemEvents(mpItem);
            }
            else
            {
                Log($"Error creating the AdaptiveMediaSource: {result.Status}");
            }
        }
Ejemplo n.º 15
0
        protected override async void OnNavigatedTo(NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);
            string movieSource = e.Parameter as string;

            mediaPlayer.Source = new System.Uri(movieSource);
            AdaptiveMediaSourceCreationResult result = await AdaptiveMediaSource.CreateFromUriAsync(new System.Uri(movieSource, UriKind.Absolute));

            if (result.Status == AdaptiveMediaSourceCreationStatus.Success)
            {
                var astream     = result.MediaSource;
                var ttmSource   = TimedTextSource.CreateFromUri(new Uri(movieSource));
                var mediaSource = MediaSource.CreateFromAdaptiveMediaSource(astream);
                mediaSource.ExternalTimedTextSources.Add(ttmSource);
                var mediaElement = new MediaPlaybackItem(mediaSource);
                mediaPlayer.SetPlaybackSource(mediaElement);
                movieLoadingRing.IsActive = false;
            }
        }
Ejemplo n.º 16
0
        private async void OperateMediaStreamResult(AdaptiveMediaSourceCreationResult result)
        {
            switch (result.Status)
            {
            case AdaptiveMediaSourceCreationStatus.Success:
                CurrentSource = result.MediaSource;
                CurrentSource.DownloadBitrateChanged += (c, r) => { Debug.WriteLine("Download : {0}", CurrentSource.CurrentDownloadBitrate); };
                CurrentSource.PlaybackBitrateChanged += (c, r) => { Debug.WriteLine("Playback : {0}", CurrentSource.CurrentPlaybackBitrate); };
                break;

            default:
                var reportResult = await _dialogService.ShowConfirmationDialogAsync(string.Format("Show TV yüklenemedi."), ":(");

                if (reportResult)
                {
                    // TODO : Report channel
                }
                break;
            }
        }
Ejemplo n.º 17
0
        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}");
            }
        }
Ejemplo n.º 18
0
        private async Task <MediaSource> GetMediaSource(Uri streamUri, string contentType)
        {
            // Using AdaptiveMediaSource for HLS since MediaSource can fails to stream HLS on some versions of Windows.
            // Root cause: User-Agent header mismatch between HLS manifest & fragment request
            if (contentType == "application/vnd.apple.mpegurl")
            {
                AdaptiveMediaSourceCreationResult adaptiveMediaSourceCreation =
                    await AdaptiveMediaSource.CreateFromUriAsync(streamUri);

                if (adaptiveMediaSourceCreation.Status != AdaptiveMediaSourceCreationStatus.Success)
                {
                    Debug.WriteLine($"Error creating the AdaptiveMediaSource: {adaptiveMediaSourceCreation.Status}");

                    return(null);
                }

                return(MediaSource.CreateFromAdaptiveMediaSource(adaptiveMediaSourceCreation.MediaSource));
            }

            return(MediaSource.CreateFromUri(streamUri));
        }
Ejemplo n.º 19
0
        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)
            {
                var adaptiveMS = result.MediaSource;
                isLiveMediaSource = adaptiveMS.IsLive;
                RegisterForAdaptiveMediaSourceEvents(adaptiveMS);

                MediaSource source = MediaSource.CreateFromAdaptiveMediaSource(adaptiveMS);
                source.CustomProperties.Add("uri", uri.ToString());
                source.CustomProperties.Add("contentId", "MainContent_ID");
                RegisterForMediaSourceEvents(source);

                mpItem = new MediaPlaybackItem(source);
                RegisterForMediaPlaybackItemEvents(mpItem);
            }
            else
            {
                Log($"Error creating the AdaptiveMediaSource: {result.Status}");
            }

            // Now that we should have a MediaPlaybackItem, we will insert pre- mid- and post-roll ads
            // with the MediaBreak API.
            if (mpItem != null)
            {
                CreateMediaBreaksForItem(mpItem);
            }
        }
Ejemplo n.º 20
0
        async private void InitializeAdaptiveMediaSource(System.Uri uri, MediaElement m)
        {
            httpClient = new Windows.Web.Http.HttpClient();
            httpClient.DefaultRequestHeaders.TryAppendWithoutValidation("X-CustomHeader", "This is a custom header");

            AdaptiveMediaSourceCreationResult result = await AdaptiveMediaSource.CreateFromUriAsync(uri, httpClient);

            if (result.Status == AdaptiveMediaSourceCreationStatus.Success)
            {
                ams = result.MediaSource;
                m.SetMediaStreamSource(ams);

                //Register for download requested event
                ams.DownloadRequested += DownloadRequested;

                //Register for download success and failure events
                ams.DownloadCompleted += DownloadCompleted;
                ams.DownloadFailed    += DownloadFailed;
            }
            else
            {
                rootPage.NotifyUser("Error creating the AdaptiveMediaSource\n\t" + result.Status, NotifyType.ErrorMessage);
            }
        }
Ejemplo n.º 21
0
        private async void Button_Click(object sender, RoutedEventArgs e)
        {
            AdaptiveMediaSourceCreationResult result = null;

            try
            {
                result = await AdaptiveMediaSource.CreateFromUriAsync(new Uri(UrlBox.Text));

                VideoElement.SetMediaStreamSource(result.MediaSource);
            }
            catch
            {
                try
                {
                    VideoElement.Source = new Uri(UrlBox.Text);
                }
                catch
                {
                    await Message();

                    return;
                }
            }
        }
Ejemplo n.º 22
0
        private async void OnPlayClick(object sender, RoutedEventArgs e)
        {
            String url = txtURL.Text;
            String tag = "ra000001";
            Uri    hsl = await GetHitChannelHSL(url, tag);

            if (hsl != null)
            {
                // check is phone
                bool isHardwareButtonsAPIPresent =
                    Windows.Foundation.Metadata.ApiInformation.IsTypePresent("Windows.Phone.UI.Input.HardwareButtons");

                if (isHardwareButtonsAPIPresent)
                {
                    //Windows.Phone.UI.Input.HardwareButtons.CameraPressed +=
                    //    HardwareButtons_CameraPressed;
                    ValueSet msg = new ValueSet();
                    msg.Add("Play", hsl.OriginalString);
                    BackgroundMediaPlayer.SendMessageToBackground(msg);
                }

                AdaptiveMediaSourceCreationResult result = await AdaptiveMediaSource.CreateFromUriAsync(hsl);

                if (result.Status == AdaptiveMediaSourceCreationStatus.Success)
                {
                    _source = result.MediaSource;
                    _source.DownloadRequested      += _source_DownloadRequested;
                    _source.DownloadCompleted      += _source_DownloadCompleted;
                    _source.DownloadFailed         += _source_DownloadFailed;
                    _source.DownloadBitrateChanged += _source_DownloadBitrateChanged;
                    _source.PlaybackBitrateChanged += _source_PlaybackBitrateChanged;

                    mediaPlayer.SetMediaStreamSource(result.MediaSource);
                }
            }
        }
        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);
        }
        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);
        }
        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;
            }
        }