国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

C#解析Lrc歌詞文件過程詳解

2020-01-24 01:05:46
字體:
來源:轉載
供稿:網友

看到很多人解析歌詞文件時寫了一大片的字符處理代碼,而且看得不是很明白,所以自己研究了一下,
 首先來了解下Lrc文件
 時間格式:
 1、標準格式: [分鐘:秒.毫秒] 歌詞
 注釋:括號、冒號、點號全都要求英文輸入狀態(tài);
 2、其他格式①:[分鐘:秒] 歌詞;
 3、其他格式②:[分鐘:秒:毫秒] 歌詞,與標準格式相比,秒后邊的點號被改成了冒號。

標準格式:
 其格式為"[標識名:值]"。大小寫等價。以下是預定義的標簽。
[ar:藝人名]
[ti:曲名]
[al:專輯名] 
[by:編者(指編輯LRC歌詞的人)] 
[offset:時間補償值] 其單位是毫秒,正值表示整體提前,負值相反。這是用于總體調整顯示快慢的。
標準好啊,我就按照標準來做了 

 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>  /// <returns>返回歌詞信息(Lrc實例)</returns>  public static 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>  static string SplitInfo(string line)  {   return line.Substring(line.IndexOf(":") + 1).TrimEnd(']');  } }

一行代碼:Lrc lrc= Lrc.InitLrc("test.lrc"); 

我將分離好的歌詞放入了Dictionary<double, string>里,當然也可以直接用數組存,格式就要看實際的用途了,把這些都交給TimeSpan來做吧。 
測試: 

很久以前有人提出了這個問題:一行歌詞里面有多個時間會報錯,這么久了也沒見人把好的方案提供出來,今天我花了點時間,修改了下,下面是獲取歌詞方法 

 

/// <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.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      {       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).TotalSeconds;         dicword.Add(time, word);        }       }       catch       {        continue;       }      }     }    }   }   lrc.LrcWord = dicword.OrderBy(t => t.Key).ToDictionary(t => t.Key, p => p.Value);   return lrc;  }

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 麻江县| 扬中市| 义乌市| 乌兰察布市| 柘荣县| 太白县| 灵山县| 淮滨县| 宜宾县| 潞城市| 绥中县| 湖南省| 会泽县| 四会市| 上杭县| 静海县| 昭觉县| 墨脱县| 洞口县| 三亚市| 桦南县| 乐至县| 藁城市| 安丘市| 元氏县| 巴彦县| 广东省| 松原市| 阿合奇县| 星子县| 汝阳县| 南澳县| 海兴县| 大厂| 精河县| 鹤峰县| 临夏市| 诸城市| 金湖县| 葫芦岛市| 翁牛特旗|