Ejemplo n.º 1
0
        /// <summary>
        /// 将歌词行以形如 [mm:ss.mss]歌词文本 输出
        /// </summary>
        public override string ToString()
        {
            // 歌曲信息|[al:album]      | Time = null, Content = Info
            // 空白行  |                | Time = null, Content = empty
            // 正常歌词|[00:00.000]Info | Time = time, Content = content
            // 空白歌词|[00:00.000]     | Time = time, Content = empty

            // 正常歌词或空白歌词
            if (LrcTime.HasValue)
            {
                return($"[{LrcHelper.ToShortString(LrcTime.Value)}]{LrcText}");
            }
            // 歌曲信息
            else if (!string.IsNullOrWhiteSpace(LrcText))
            {
                return($"[{LrcText}]");
            }
            // 空白行
            else
            {
                return(string.Empty);
            }
        }
Ejemplo n.º 2
0
        public static LrcLine Parse(string line)
        {
            // 歌曲信息|[al:album]      | Time = null, Content = Info
            // 空白行  |                | Time = null, Content = empty
            // 正常歌词|[00:00.000]Info | Time = time, Content = content
            // 空白歌词|[00:00.000]     | Time = time, Content = empty
            // 多行歌词|[00:00.000][00:01.000]Info

            // 判断是否为空白行
            if (string.IsNullOrWhiteSpace(line))
            {
                return(Empty);
            }
            // 这里不考虑多行歌词的情况
            if (CheckMultiLine(line))
            {
                throw new FormatException();
            }

            // 此时只能为正常歌词
            var slices = line.TrimStart().TrimStart('[').Split(']');

            if (slices.Length != 2)
            {
                throw new FormatException();
            }

            // 如果方括号中的内容无法转化为时间,则认为是歌曲信息
            if (!LrcHelper.TryParseTimeSpan(slices[0], out TimeSpan time))
            {
                return(new LrcLine(null, slices[0]));
            }

            // 正常歌词和空白歌词不需要进行额外区分
            return(new LrcLine(time, slices[1]));
        }