public LyricFile(string fileName) { FileName = Path.GetFileNameWithoutExtension(fileName); using (StreamReader reader = new StreamReader(fileName, Encoding.UTF8)) { SpectialTags = new Dictionary<string, string> { {"ar", String.Empty}, {"ti", String.Empty}, {"al", String.Empty}, {"by", String.Empty}, {"offset", "0"} }; List<Lyric> lines = new List<Lyric>(); String readin; while ((readin = reader.ReadLine()) != null) { string content = readin.Substring(readin.LastIndexOf(']') + 1).Trim(); foreach (Match match in specTagReg.Matches(readin)) SpectialTags[match.Groups["key"].Value] = match.Groups["value"].Value; foreach (Match match in timeTagReg.Matches(readin)) { long timeTag = Int64.Parse(match.Groups["min"].Value) * 60000 + Convert.ToInt64(Single.Parse(match.Groups["sec"].Value) * 1000); Lyric item = new Lyric(timeTag, content); lines.Add(item); } } Offset = Int32.Parse(SpectialTags["offset"]); Lyrics = new Queue<Lyric>(lines.OrderBy(l => l.TimeTag)); } }
/// <summary> /// 从LRC文件中读取所有lyric,并按时间顺序排列。 /// </summary> /// <param name="fileName">待读取的文件路径。</param> /// <returns>一个队列,包含待读取文件中的所有lyric。</returns> public static Queue<Lyric> LoadFromFile(string fileName) { StreamReader reader = null; try { reader = new StreamReader(fileName, Encoding.UTF8); Queue<Lyric> results = new Queue<Lyric>(); ArrayList lines = new ArrayList(); String readin = null; Regex time_tag = new Regex(@"\[(\d+):(\d+)\.(\d+)\]"); Regex lyric_tag = new Regex(@"\[\d\d:\d\d\.\d\d\]+"); Match time; string[] lrcOut = null; string tmp_tag = null; while ((readin = reader.ReadLine()) != null) { time = time_tag.Match(readin); while (time.Success) { tmp_tag = null; lrcOut = null; for (int i = 1; i < time.Groups.Count; i++) tmp_tag += time.Groups[i]; lrcOut = lyric_tag.Split(readin); Lyric item = new Lyric(tmp_tag, lrcOut[lrcOut.Length-1]); lines.Add(item); time = time.NextMatch(); } } lines.Sort(); for (int i = 0; i < lines.Count; i++) results.Enqueue((Lyric)lines[i]); // 实现时删除本行 //throw new NotImplementedException(); return results; } finally { if (reader != null) reader.Dispose(); } }