public class Lrc { /// <summary> /// 歌曲 /// </summary> public string Title { get; set; } /// <summary> /// 艺术家 /// </summary> public string Artist { get; set; } /// <summary> /// 专辑 /// </summary> public string Album { get; set; } /// <summary> /// 歌词作者 /// </summary> public string LrcBy { get; set; } /// <summary> /// 偏移量 /// </summary> public string Offset { get; set; } /// <summary> /// 歌词 /// </summary> public Dictionary<double, string> LrcWord = new Dictionary<double, string>(); /// <summary> /// 歌词路径 /// </summary> /// <param name="LrcPath"></param> public Lrc InitLrc(string LrcPath) { Lrc lrc = new Lrc(); using (FileStream fs = new FileStream(LrcPath, FileMode.Open, FileAccess.Read, FileShare.Read)) { string line; using (StreamReader sr = new StreamReader(fs, Encoding.Default)) { 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 { Regex regex = new Regex(@"\[([0-9.:]*)\]+(.*)", RegexOptions.Compiled); MatchCollection mc = regex.Matches(line); double time = TimeSpan.Parse("00:" + mc[0].Groups[1].Value).TotalSeconds; string word = mc[0].Groups[2].Value; lrc.LrcWord.Add(time, word); } } } } return lrc; } /// <summary> /// 处理信息 /// </summary> /// <param name="line"></param> /// <returns>返回基础信息</returns> string SplitInfo(string line) { return line.Substring(line.IndexOf(":") + 1).TrimEnd(']'); } }
我将分离好的歌词放入了Dictionary<double, string>里,当然也可以直接用数组存,格式就要看实际的用途了,把这些都交给TimeSpan来做吧。
测试: