Exemplo n.º 1
0
        public async void Play(PlaylistItem item)
        {
            // プレイリストアイテムが不正
            var playlist = item.Owner;

            if (item.Type == PlaylistItemType.Video)
            {
                if (playlist != null && !playlist.Contains(item.ContentId))
                {
                    throw new Exception();
                }
            }


            if (!NiconicoSession.IsPremiumAccount && !VideoCacheManager.CanAddDownloadLine)
            {
                // 一般ユーザーまたは未登録ユーザーの場合
                // 視聴セッションを1つに制限するため、キャッシュダウンロードを止める必要がある
                // キャッシュ済みのアイテムを再生中の場合はダウンロード可能なので確認をスキップする
                bool playingVideoIsCached = false;
                var  currentItem          = item;
                if (currentItem != null && currentItem.Type == PlaylistItemType.Video)
                {
                    var cachedItems = await VideoCacheManager.GetCacheRequest(currentItem.ContentId);

                    if (cachedItems.FirstOrDefault(x => x.ToCacheState() == NicoVideoCacheState.Cached) != null)
                    {
                        playingVideoIsCached = true;
                    }
                }

                if (!playingVideoIsCached)
                {
                    var currentDownloadingItems = await VideoCacheManager.GetDownloadProgressVideosAsync();

                    var downloadingItem          = currentDownloadingItems.FirstOrDefault();
                    var downloadingItemVideoInfo = Database.NicoVideoDb.Get(downloadingItem.RawVideoId);

                    var dialogService = App.Current.Container.Resolve <Services.DialogService>();
                    var totalSize     = downloadingItem.DownloadOperation.Progress.TotalBytesToReceive;
                    var receivedSize  = downloadingItem.DownloadOperation.Progress.BytesReceived;
                    var megaBytes     = (totalSize - receivedSize) / 1000_000.0;
                    var downloadProgressDescription = $"ダウンロード中\n{downloadingItemVideoInfo.Title}\n残り {megaBytes:0.0} MB ( {receivedSize / 1000_000.0:0.0} MB / {totalSize / 1000_000.0:0.0} MB)";
                    var isCancelCacheAndPlay        = await dialogService.ShowMessageDialog("ニコニコのプレミアム会員以外は 視聴とダウンロードは一つしか同時に行えません。\n視聴を開始する場合、キャッシュは中止されます。またキャッシュを再開する場合はダウンロードは最初からやり直しになります。\n\n" + downloadProgressDescription, "キャッシュを中止して視聴を開始しますか?", "キャッシュを中止して視聴する", "何もしない");

                    if (isCancelCacheAndPlay)
                    {
                        await VideoCacheManager.SuspendCacheDownload();
                    }
                    else
                    {
                        return;
                    }
                }
            }
            else if (!NiconicoSession.IsPremiumAccount)
            {
                // キャッシュ済みの場合はサスペンドを掛けない
                if (item.Type == PlaylistItemType.Video && false == await VideoCacheManager.CheckCachedAsync(item.ContentId))
                {
                    await VideoCacheManager.SuspendCacheDownload();
                }
            }

            Player.PlayStarted(item);

            _ = PlayerViewManager.PlayWithCurrentPlayerView(item);
        }
Exemplo n.º 2
0
        /// <summary>
        /// 再生処理
        /// </summary>
        /// <returns></returns>
        public async Task <PlayingOrchestrateResult> CreatePlayingOrchestrateResultAsync(string videoId)
        {
            var cacheVideoResult = await _videoCacheManager.TryCreateCachedVideoSessionProvider(videoId);

            if (cacheVideoResult.IsSuccess)
            {
                INiconicoCommentSessionProvider commentSessionProvider = null;
                INicoVideoDetails nicoVideoDetails = null;
                if (!Models.Helpers.InternetConnection.IsInternet())
                {
                    var cachedComment = _commentRepository.GetCached(videoId);
                    if (cachedComment != null)
                    {
                        commentSessionProvider = new CachedCommentsProvider(videoId, cachedComment.Comments);
                    }

                    var videoInfo = Database.NicoVideoDb.Get(videoId);
                    if (videoInfo != null)
                    {
                        nicoVideoDetails = new CachedVideoDetails()
                        {
                            VideoTitle      = videoInfo.Title,
                            ViewCount       = videoInfo.ViewCount,
                            CommentCount    = videoInfo.CommentCount,
                            MylistCount     = videoInfo.MylistCount,
                            VideoLength     = videoInfo.Length,
                            SubmitDate      = videoInfo.PostedAt,
                            Tags            = videoInfo.Tags.Select(x => new NicoVideoTag(x.Id)).ToArray(),
                            ProviderId      = videoInfo.Owner?.OwnerId,
                            ProviderName    = videoInfo.Owner?.ScreenName,
                            OwnerIconUrl    = videoInfo.Owner.IconUrl,
                            DescriptionHtml = videoInfo.DescriptionWithHtml,
                            ThumbnailUrl    = videoInfo.ThumbnailUrl
                        };
                    }
                }
                else
                {
                    var preparePlayVideo = await _nicoVideoSessionProvider.PreparePlayVideoAsync(videoId);

                    commentSessionProvider = preparePlayVideo;
                    nicoVideoDetails       = preparePlayVideo?.GetVideoDetails();
                }

                // キャッシュからコメントを取得する方法が必要
                return(new PlayingOrchestrateResult(
                           cacheVideoResult.VideoSessionProvider,
                           commentSessionProvider,
                           nicoVideoDetails
                           ));
            }
            else
            {
                /*
                 * bool canPlay = true;
                 * var downloadLineOwnership = await _videoCacheManager.TryRentDownloadLineAsync();
                 * if (downloadLineOwnership == null)
                 * {
                 *  canPlay = false;
                 *  if (await ShowSuspendCacheDownloadingDialog())
                 *  {
                 *      await _videoCacheManager.SuspendCacheDownload();
                 *      canPlay = true;
                 *  }
                 * }
                 *
                 * if (!canPlay)
                 * {
                 *  return new PlayingOrchestrateResult();
                 * }
                 */

                var preparePlayVideo = await _nicoVideoSessionProvider.PreparePlayVideoAsync(videoId);

                if (!preparePlayVideo.IsSuccess)
                {
                    var progress = await _videoCacheManager.GetDownloadProgressVideosAsync();

                    if (!_niconicoSession.IsPremiumAccount && progress.Any())
                    {
                        var result = await ShowSuspendCacheDownloadingDialog();

                        if (result)
                        {
                            preparePlayVideo = await _nicoVideoSessionProvider.PreparePlayVideoAsync(videoId);
                        }
                    }
                }

                if (preparePlayVideo == null || !preparePlayVideo.IsSuccess)
                {
                    throw new NotSupportedException("不明なエラーにより再生できません");
                }

                return(new PlayingOrchestrateResult(
                           preparePlayVideo,
                           preparePlayVideo,
                           preparePlayVideo.GetVideoDetails()
                           ));
            }
        }