示例#1
0
        /// <summary>
        /// 从文件获得歌词信息
        /// </summary>
        /// <param name="LrcPath">歌词路径</param>
        /// <returns>返回歌词信息(Lrc实例)</returns>
        public static Lrc InitLrc(string LrcPath)
        {
            Lrc lrc = new Lrc();
            Dictionary <double, string> dicword = new Dictionary <double, string>();

            using (FileStream fs = new FileStream(LrcPath, FileMode.Open, FileAccess.Read, FileShare.Read))
            {
                string line;
                using (StreamReader sr = new StreamReader(fs, Encoding.UTF8))
                {
                    while ((line = sr.ReadLine()) != null)
                    {
                        if (line.StartsWith("[ti:"))
                        {
                            lrc.Title = SplitInfo(line);
                        }
                        else if (line.StartsWith("[ar:"))
                        {
                            lrc.Artist = SplitInfo(line);
                        }
                        else if (line.StartsWith("[al:"))
                        {
                            lrc.Album = SplitInfo(line);
                        }
                        else if (line.StartsWith("[by:"))
                        {
                            lrc.LrcBy = SplitInfo(line);
                        }
                        else if (line.StartsWith("[offset:"))
                        {
                            lrc.Offset = SplitInfo(line);
                        }
                        else
                        {
                            try
                            {
                                Regex           regexword = new Regex(@".*\](.*)");
                                Match           mcw       = regexword.Match(line);
                                string          word      = mcw.Groups[1].Value;
                                Regex           regextime = new Regex(@"\[([0-9.:]*)\]", RegexOptions.Compiled);
                                MatchCollection mct       = regextime.Matches(line);
                                foreach (Match item in mct)
                                {
                                    double time = TimeSpan.Parse("00:" + item.Groups[1].Value).TotalMilliseconds;
                                    dicword.Add(time, word);
                                }
                            }
                            catch
                            {
                                continue;
                            }
                        }
                    }
                }
            }
            lrc.LrcWord = dicword.OrderBy(t => t.Key).ToDictionary(t => t.Key, p => p.Value);
            return(lrc);
        }
示例#2
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();
                }
            });
        }
示例#3
0
        /// <summary>
        /// 刷新歌词
        /// </summary>
        /// <param name="ts"></param>
        public void FreshLyric(TimeSpan ts, Lrc lrc)
        {
            var lrcs = from n in lrc.LrcWord
                       where n.Key <= ts.TotalMilliseconds
                       select n.Value;

            if (lrcs.Count() > 0)
            {
                if (currentLrc == lrcs.Last())
                {
                    return;
                }
                else
                {
                    currentLrc = lrcs.Last();
                }

                if (DoShowLrc != null)
                {
                    this.Dispatcher.Invoke(() => DoShowLrc(currentLrc));
                }
            }
        }
示例#4
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);
            }
        }