コード例 #1
0
ファイル: PlayingService .cs プロジェクト: qedsd/MyNCMusic
        /// <summary>
        /// 开始播放前获取相关信息准备
        /// </summary>
        /// <param name="playingSongId"></param>
        /// <param name="songsItem"></param>
        /// <returns></returns>
        public static async Task <bool> PreparePlayingSong(long playingSongId, SongsItem songsItem = null)
        {
            if (songsItem == null)//需获取实例
            {
                MusicDetailRoot musicDetailRoot = await Task.Run(() => SongService.GetMusicDetail_Get(playingSongId.ToString()));

                if (musicDetailRoot == null || musicDetailRoot.songs == null || musicDetailRoot.songs.Count == 0)
                {
                    return(false);
                }
                songsItem = musicDetailRoot.songs.Last();
                //判断是否为喜欢歌曲
                if (MainPage.favoriteSongsRoot != null)
                {
                    if (MainPage.favoriteSongsRoot.ids.Find(p => p.Equals(songsItem.Id)) != 0)
                    {
                        songsItem.isFavorite = true;
                    }
                }
            }
            SongUrlRoot songUrlRoot = SongService.GetMusicUrl(songsItem.Id);

            if (songUrlRoot == null)
            {
                return(false);
            }
            PlayingSong        = songsItem;
            PlayingSongUrlRoot = songUrlRoot;
            var playingSong = PlayingService.PlayingSongList.FirstOrDefault(p => p.Id == PlayingService.PlayingSong.Id);

            if (playingSong == null)//将要播放的歌曲不在当前播放列表
            {
                PlayingSongList = new List <SongsItem>()
                {
                    PlayingSong
                };
                playingSong = PlayingSong;
            }
            if (PlayedSongId != null)
            {
                if (PlayingSongList != PlayedSongList)//不同一个播放列表需清空列表
                {
                    PlayedSongId.Clear();
                }
            }
            else
            {
                PlayedSongId = new List <long>();
            }
            PlayedSongId.Remove(playingSong.Id);   //删除重复的,避免死循环
            PlayedSongId.Add(playingSong.Id);
            if (PlayingService.PlayedSong != null) //听歌打卡
            {
                PlayingService.PlayDurationStopwatch.Stop();
                await Task.Run(() => SongService.MarkPlayDuration(PlayedSong.Id, PlayedListId, PlayDurationStopwatch.ElapsedMilliseconds / 1000));
            }

            //获取专辑
            PlayingAlbum = await Task.Run(() => AlbumService.GetAlbum(songsItem.al.id));

            if (PlayingAlbum == null)
            {
                return(false);
            }
            PlayingService.PlayingAlbumBitmapImage = await FileHelper.DownloadFile(new Uri(PlayingAlbum.album.picUrl + "?param=200y200"));

            if (PlayedSong != null && PlayedSongList != null)
            {
                PlayedSongList.FirstOrDefault(p => p.Id == PlayedSong.Id).IsPlaying = false;
            }
            PlayingSongList.FirstOrDefault(p => p.Id == PlayingSong.Id).IsPlaying = true;

            return(true);
        }