/// <summary> /// This method disables the current page and shows a progress indicator until the youtube movie url has been loaded and starts /// </summary> /// <param name="youTubeId"></param> /// <param name="manualActivatePage">if true add YouTube.CancelPlay() in OnNavigatedTo() of the page (better)</param> /// <param name="maxQuality"></param> /// <param name="completed"></param> public static void Play(string youTubeId, bool manualActivatePage, YouTubeQuality maxQuality = YouTubeQuality.Quality480P, Action<Exception> completed = null) { lock (typeof(YouTube)) { if (oldState != null) return; if (SystemTray.ProgressIndicator == null) SystemTray.ProgressIndicator = new ProgressIndicator(); SystemTray.ProgressIndicator.IsVisible = true; SystemTray.ProgressIndicator.IsIndeterminate = true; var page = PhonePage.CurrentPage; oldState = PageDeactivator.Inactivate(); httpResponse = Play(youTubeId, YouTubeQuality.Quality480P, ex => Deployment.Current.Dispatcher.BeginInvoke( delegate { if (page == PhonePage.CurrentPage) // !user navigated away { if (ex == null) CancelPlay(manualActivatePage); else CancelPlay(false); } if (completed != null) completed(ex); })); } }
/// <summary>Plays the YouTube video in the Windows Phone's external player. /// Disables the current page and shows a progress indicator until /// the YouTube movie URI has been loaded and the video playback starts. </summary> /// <param name="youTubeId">The YouTube ID</param> /// <param name="manualActivatePage">if true add YouTube.CancelPlay() in OnNavigatedTo() of the page (better)</param> /// <param name="maxQuality">The maximum allowed video quality. </param> /// <returns>Awaitable task. </returns> public static async Task PlayWithPageDeactivationAsync(string youTubeId, bool manualActivatePage, YouTubeQuality maxQuality) { PhoneApplicationPage page; lock (typeof(YouTube)) { if (_oldState != null) { return; } if (SystemTray.ProgressIndicator == null) { SystemTray.ProgressIndicator = new ProgressIndicator(); } SystemTray.ProgressIndicator.IsVisible = true; SystemTray.ProgressIndicator.IsIndeterminate = true; page = Environment.PhoneApplication.CurrentPage; _oldState = PageDeactivator.Inactivate(); _cancellationToken = new CancellationTokenSource(); } try { await PlayAsync(youTubeId, maxQuality, _cancellationToken.Token); if (page == Environment.PhoneApplication.CurrentPage) { CancelPlay(manualActivatePage); } } catch (Exception) { if (page == Environment.PhoneApplication.CurrentPage) { CancelPlay(false); } throw; } }