/// <summary> /// Load movie's trailer asynchronously /// </summary> /// <param name="movie">The movie</param> /// <param name="ct">Cancellation token</param> public async Task LoadTrailerAsync(MovieFull movie, CancellationToken ct) { try { var trailer = await _movieService.GetMovieTrailerAsync(movie, ct); var video = await GetVideoInfoForStreamingAsync( Constants.YoutubePath + trailer.Results.FirstOrDefault()?.Key, Constants.YoutubeStreamingQuality.High); if (video != null && video.RequiresDecryption) { Logger.Info( $"Decrypting Youtube trailer url: {video.Title}"); await Task.Run(() => DownloadUrlResolver.DecryptDownloadUrl(video), ct); } if (video == null) { Logger.Error( $"Failed loading movie's trailer: {movie.Title}"); Messenger.Default.Send( new ManageExceptionMessage( new Exception( LocalizationProviderHelper.GetLocalizedValue<string>("TrailerNotAvailable")))); Messenger.Default.Send(new StopPlayingTrailerMessage()); return; } if (!ct.IsCancellationRequested) { Logger.Debug( $"Movie's trailer loaded: {movie.Title}"); TrailerPlayer = new TrailerPlayerViewModel(_applicationState, _movieService, _movieHistoryService); TrailerPlayer.LoadTrailer(new Models.Trailer.Trailer(new Uri(video.DownloadUrl))); } } catch (Exception exception) when (exception is TaskCanceledException) { Logger.Debug( "GetMovieTrailerAsync cancelled."); Messenger.Default.Send(new StopPlayingTrailerMessage()); } catch (Exception exception) when (exception is SocketException || exception is WebException) { Logger.Error( $"GetMovieTrailerAsync: {exception.Message}"); Messenger.Default.Send(new StopPlayingTrailerMessage()); Messenger.Default.Send(new ManageExceptionMessage(exception)); } catch (Exception exception) when (exception is VideoNotAvailableException || exception is YoutubeParseException) { Logger.Error( $"GetMovieTrailerAsync: {exception.Message}"); Messenger.Default.Send( new ManageExceptionMessage( new Exception( LocalizationProviderHelper.GetLocalizedValue<string>( "TrailerNotAvailable")))); Messenger.Default.Send(new StopPlayingTrailerMessage()); } catch (Exception exception) { Logger.Error( $"GetMovieTrailerAsync: {exception.Message}"); Messenger.Default.Send(new StopPlayingTrailerMessage()); } }
/// <summary> /// Cleanup resources /// </summary> public override void Cleanup() { TrailerPlayer?.Cleanup(); TrailerPlayer = null; base.Cleanup(); }
/// <summary> /// Unload the trailer /// </summary> public void UnLoadTrailer() { TrailerPlayer?.Cleanup(); TrailerPlayer = null; }
/// <summary> /// Get trailer of a movie /// </summary> /// <param name="movie">The movie</param> /// <param name="ct">Cancellation token</param> public async Task LoadTrailerAsync(MovieFull movie, CancellationToken ct) { try { var trailer = await _movieService.GetMovieTrailerAsync(movie, ct); var video = await GetVideoInfoForStreamingAsync( Constants.YoutubePath + trailer.Results.FirstOrDefault()?.Key, Constants.YoutubeStreamingQuality.High); if (video != null && video.RequiresDecryption) { Logger.Info( $"Decrypting Youtube trailer url: {video.Title}"); await Task.Run(() => DownloadUrlResolver.DecryptDownloadUrl(video), ct); } if (video == null) { Logger.Error( $"Failed loading movie's trailer: {movie.Title}"); Messenger.Default.Send( new ManageExceptionMessage( new Exception( LocalizationProviderHelper.GetLocalizedValue <string>("TrailerNotAvailable")))); Messenger.Default.Send(new StopPlayingTrailerMessage()); return; } if (!ct.IsCancellationRequested) { Logger.Debug( $"Movie's trailer loaded: {movie.Title}"); TrailerPlayer = new TrailerPlayerViewModel(_applicationState, _movieService, _movieHistoryService); TrailerPlayer.LoadTrailer(new Models.Trailer.Trailer(new Uri(video.DownloadUrl))); } } catch (Exception exception) when(exception is TaskCanceledException) { Logger.Debug( "GetMovieTrailerAsync cancelled."); Messenger.Default.Send(new StopPlayingTrailerMessage()); } catch (Exception exception) when(exception is SocketException || exception is WebException) { Logger.Error( $"GetMovieTrailerAsync: {exception.Message}"); Messenger.Default.Send(new StopPlayingTrailerMessage()); Messenger.Default.Send(new ManageExceptionMessage(exception)); } catch (Exception exception) when(exception is VideoNotAvailableException || exception is YoutubeParseException) { Logger.Error( $"GetMovieTrailerAsync: {exception.Message}"); Messenger.Default.Send( new ManageExceptionMessage( new Exception( LocalizationProviderHelper.GetLocalizedValue <string>( "TrailerNotAvailable")))); Messenger.Default.Send(new StopPlayingTrailerMessage()); } catch (Exception exception) { Logger.Error( $"GetMovieTrailerAsync: {exception.Message}"); Messenger.Default.Send(new StopPlayingTrailerMessage()); } }