コード例 #1
0
        /// <summary>
        /// 更改封面和歌词
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ChangeCoverAndLyric()
        {
            DispatchService.Invoke(async() =>
            {
                if (mySongList.Count > 0 && listViewSongList.SelectedIndex >= 0)
                {
                    if (IsApiUsing)
                    {
                        var path = GetCover(mySongList[listViewSongList.SelectedIndex].apid);
                        if (!string.IsNullOrEmpty(path))
                        {
                            path += "?param=500x500";
                            bi    = await GetNewImageAsync(new Uri(path));
                        }
                        else
                        {
                            bi = new BitmapImage(new Uri("pack://application:,,,/images/default_cover.jpg", UriKind.Absolute));
                        }
                        //获取封面的同时更新歌手信息
                        playerInfo.Artist = mySongList[listViewSongList.SelectedIndex].artist;
                    }
                    else
                    {
                        string path    = mySongList[listViewSongList.SelectedIndex].path;
                        string lrcPath = path.Replace(".mp3", ".lrc");

                        bi = GetImageFromMp3(path);
                        //尝试获取本地歌词并显示
                        if (File.Exists(lrcPath))
                        {
                            haveLyric = true;
                            lrc       = Lrc.InitLrc(path.Replace(".mp3", ".lrc"));
                        }
                        else
                        {
                            haveLyric = false;
                        }

                        DoShowLrc("");
                    }

                    AlbumSlideEffect();
                }
            });
        }
コード例 #2
0
        public string GetCover(string songId)
        {
            try
            {
                string detailjson = MusicApi.Detail(songId);
                if (string.IsNullOrEmpty(detailjson))
                {
                    haveLyric = false; return(null);
                }
                var detailjsonObj = JsonConvert.DeserializeObject <dynamic>(detailjson);
                if (detailjsonObj == null)
                {
                    haveLyric = false; return(null);
                }
                var    artists  = detailjsonObj.songs[0].artists;
                var    album    = detailjsonObj.songs[0].album.name;
                string duration = detailjsonObj.songs[0].duration;
                if (duration != null)
                {
                    try
                    {
                        TimeSpan ts = TimeSpan.FromMilliseconds(double.Parse(duration));
                        MaxLength            = ts.TotalSeconds;
                        txtTotalSeconds.Text = TimeSpanToDateTime(ts);
                    }
                    catch { }
                }

                if (album != null)
                {
                    playerInfo.Album = album;
                }

                //获取歌词
                string lyricjson    = MusicApi.Lyric(songId);
                var    lyricjsonObj = JsonConvert.DeserializeObject <dynamic>(lyricjson);
                string lyric        = string.Empty;
                if (lyricjsonObj != null && lyricjsonObj.nolyric == null && lyricjsonObj.uncollected == null)
                {
                    lyric = lrcTemp = lyricjsonObj.lrc.lyric;
                }
                else
                {
                    lyric = lrcTemp = string.Empty;
                }

                List <string> listArtists = new List <string>();
                if (artists != null)
                {
                    foreach (var item in artists)
                    {
                        listArtists.Add((string)item.name);
                    }
                    string strArtists = string.Join("/", listArtists);
                    mySongList[listViewSongList.SelectedIndex].artist = strArtists;
                }
                else
                {
                    mySongList[listViewSongList.SelectedIndex].artist = "未知";
                }


                //mySongList[listViewSongList.SelectedIndex].lyric = lyric;

                if (!string.IsNullOrEmpty(lyric))
                {
                    //byte[] array = Lrc.ByteArrayCut(Encoding.Default.GetBytes(lyric), 0x3f);
                    //将默认编码转到Unicode
                    byte[] arraydefalut = Encoding.UTF8.GetBytes(lyric);
                    byte[] arrayunicode = Encoding.Convert(Encoding.UTF8, Encoding.Unicode, arraydefalut);

                    MemoryStream stream = new MemoryStream(arrayunicode);
                    lrc       = Lrc.InitLrc(stream);
                    haveLyric = true;
                    //有歌词
                    DoShowLrc("");
                }
                else
                {
                    haveLyric = false;
                    DoShowLrc("没有歌词");
                }

                return(detailjsonObj.songs[0].album.picUrl);
            }
            catch (Exception)
            {
                haveLyric = false;
                DoShowLrc("歌词获取失败");

                return(string.Empty);
            }
        }