Ejemplo n.º 1
0
        /// <summary>
        /// 获得歌词信息
        /// </summary>
        /// <param name="LrcPath">歌词路径</param>
        /// <returns>返回歌词信息(Lrc实例)</returns>
        /// <summary>
        /// 获得歌词信息
        /// </summary>
        /// <param name="LrcPath">歌词路径</param>
        /// <returns>返回歌词信息(Lrc实例)</returns>
        public static Lrc InitLrc(string LrcPath)
        {
            Lrc lrc = new Lrc();
            SortedDictionary <long, string> dicword = new SortedDictionary <long, 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)
                                {
                                    long time = (long)TimeSpan.Parse("00:" + item.Groups[1].Value).TotalMilliseconds;
                                    dicword.Add(time, word);
                                }
                            }
                            catch
                            {
                                continue;
                            }
                        }
                    }
                }
            }
            lrc.LrcWord = dicword;
            return(lrc);
        }
Ejemplo n.º 2
0
        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (0 > e.RowIndex)
            {
                return;
            }
            Hzexe.QQMusic.Model.SongItem si = (dataGridView1.Rows[e.RowIndex].DataBoundItem as TableModel).SongItem;
            var col = dataGridView1.Columns[e.ColumnIndex];

            Action <EnumFileType, SongItem> fun = new Action <EnumFileType, SongItem>((ext, obj) =>
            {
                var att = ext.GetFileType();

                string musicfilename            = $"{obj.title}-{obj.singer[0].name}.{att.Suffix}";
                saveFileDialog1.CheckPathExists = true;
                saveFileDialog1.CheckFileExists = false;
                saveFileDialog1.DefaultExt      = att.Suffix;
                saveFileDialog1.OverwritePrompt = true;
                saveFileDialog1.ValidateNames   = true;
                saveFileDialog1.Tag             = new { si = obj, ft = ext };
                saveFileDialog1.Filter          = $"{att.Suffix}(*.{att.Suffix})|*.{att.Suffix}";
                saveFileDialog1.FileName        = musicfilename;
                saveFileDialog1.ShowDialog();
            });

            if ("Ape" == col.DataPropertyName)
            {
                fun(EnumFileType.Ape, si);
            }
            else if ("Flac" == col.DataPropertyName)
            {
                fun(EnumFileType.Flac, si);
            }
            else if ("Mp3_320k" == col.DataPropertyName)
            {
                fun(EnumFileType.Mp3_320k, si);
            }
            else if ("Mp3_128k" == col.DataPropertyName)
            {
                fun(EnumFileType.Mp3_128k, si);
            }
            else if ("M4a" == col.DataPropertyName)
            {
                fun(EnumFileType.M4a, si);
            }
            else if ("Play" == col.DataPropertyName)
            {
                EnumFileType type         = si.file.GetAvailableFileType();//取当前歌曲可用的类型
                EnumFileType downloadType = 0;
                downloadType |= (type & EnumFileType.Ape);
                if (downloadType == 0)
                {
                    downloadType |= (type & EnumFileType.Flac);
                }
                if (downloadType == 0)
                {
                    downloadType |= (type & EnumFileType.Mp3_320k);
                }
                if (downloadType == 0)
                {
                    downloadType |= (type & EnumFileType.Mp3_128k);
                }
                if (downloadType == 0)
                {
                    downloadType |= (type & EnumFileType.M4a);
                }



                string url = api.GetDownloadSongUrl(si, downloadType);
                mediaPlayer.SetMedia(new Uri(url));

                Task.Run(async() =>
                {
                    //尝试载歌词
                    var ms = new System.IO.MemoryStream();
                    if (await api.downloadLyricAsync(si, ms))
                    {
                        ms.Position = 0;
                        lrc         = Lrc.InitLrc(ms);
                    }
                    else
                    {
                        lrc = null;
                    }
                    ms.Dispose();
                    mediaPlayer.Play();
                });
            }
        }