c# 操作Lrc歌詞檔案 的類

來源:互聯網
上載者:User

 寫了一天吧,完成了c#操作歌詞的類。具體用法裡面都有解釋,還不夠完美,因為沒有完美的東西。如果覺得好就給我留個言吧!非常感謝。 本人不才,菜鳥一個。。。。

 

 

歌詞的類,注意命名空間我還沒改。

 

 using System;<br />using System.Collections.Generic;<br />using System.Text;<br />using System.IO;<br />using System.Windows.Forms;</p><p>namespace WindowsApplication1<br />{<br /> class Lyrics<br /> {<br /> public struct TLrc<br /> {<br /> public int ms;//毫秒<br /> public string lrc;//對應的歌詞<br /> }</p><p> /// <summary><br /> /// 標準的歌詞結構數組<br /> /// </summary><br /> public TLrc[] tlrc;<br /> /// <summary><br /> /// 輸入歌詞檔案路徑處理歌詞資訊<br /> /// </summary><br /> /// <param name="file"></param><br /> public Lyrics(string file)<br /> {<br /> StreamReader sr = new StreamReader(getLrcFile(file),Encoding.Default);<br /> string[] lrc_1 = sr.ReadToEnd().Split(new char[] { '[', ']' });<br /> sr.Close();</p><p> format_1(lrc_1);<br /> format_2(lrc_1);<br /> format_3();<br /> }<br /> /// <summary><br /> /// 格式化不同時間相同字元如“[00:34.52][00:34.53][00:34.54]因為我已經沒有力氣”<br /> /// </summary><br /> /// <param name="lrc_1"></param><br /> private void format_1(string[] lrc_1)<br /> {<br /> for (int i = 2,j = 0;i < lrc_1.Length; i += 2,j = i)<br /> {<br /> while (lrc_1[j] == string.Empty)<br /> {<br /> lrc_1[i] = lrc_1[j+=2];<br /> }<br /> }<br /> }<br /> /// <summary><br /> /// 資料添加到結構體<br /> /// </summary><br /> /// <param name="lrc_1"></param><br /> private void format_2(string[] lrc_1)<br /> {<br /> tlrc = new TLrc[lrc_1.Length / 2];<br /> for (int i = 1,j = 0; i < lrc_1.Length; i++,j++)<br /> {<br /> tlrc[j].ms = timeToMs(lrc_1[i]);<br /> tlrc[j].lrc = lrc_1[++i];<br /> }<br /> }<br /> /// <summary><br /> /// 時間格式”00:37.61“轉毫秒<br /> /// </summary><br /> /// <param name="lrc_t"></param><br /> /// <returns></returns><br /> private int timeToMs(string lrc_t)<br /> {<br /> float m, s, ms;<br /> string[] lrc_t_1 = lrc_t.Split(':');<br /> //這裡不能用TryParse如“by:253057646”則有問題<br /> try<br /> {<br /> m = float.Parse(lrc_t_1[0]);<br /> }<br /> catch<br /> {<br /> return 0;<br /> }<br /> float.TryParse(lrc_t_1[1], out s);<br /> ms = m * 60000 + s * 1000;<br /> return (int)ms;<br /> }<br /> /// <summary><br /> /// 排序,時間順序<br /> /// </summary><br /> private void format_3()<br /> {<br /> TLrc tlrc_temp;<br /> bool b = true;<br /> for (int i = 0; i < tlrc.Length - 1; i++, b = true)<br /> {<br /> for (int j = 0; j < tlrc.Length - i - 1; j++)<br /> {<br /> if (tlrc[j].ms > tlrc[j + 1].ms)<br /> {<br /> tlrc_temp = tlrc[j];<br /> tlrc[j] = tlrc[j + 1];<br /> tlrc[j + 1] = tlrc_temp;<br /> b = false;<br /> }<br /> }<br /> if (b) break;<br /> }<br /> }</p><p> public int mark;<br /> /// <summary><br /> /// 讀取下一條記錄,並跳到下一條記錄<br /> /// </summary><br /> /// <returns></returns><br /> public string ReadLine()<br /> {<br /> if (mark < tlrc.Length)<br /> {<br /> return tlrc[mark++].lrc;<br /> }<br /> else<br /> {<br /> return null;<br /> }</p><p> }<br /> /// <summary><br /> /// 讀取當前行的歌詞的時間<br /> /// </summary><br /> /// <returns></returns><br /> public int currentTime<br /> {<br /> get<br /> {<br /> if (mark < tlrc.Length)<br /> {<br /> return tlrc[mark].ms;<br /> }<br /> else<br /> {<br /> return -1;<br /> }<br /> }<br /> }</p><p> /// <summary><br /> /// 得到lrc歌詞檔案(目前的目錄)<br /> /// </summary><br /> /// <param name="file"></param><br /> /// <returns></returns><br /> private string getLrcFile(string file)<br /> {<br /> return Path.GetDirectoryName(file) + "//" + Path.GetFileNameWithoutExtension(file) + ".lrc";<br /> }<br /> }<br />}<br />    

 

 

怎麼操作很簡單。2011-05-06

作者:weiwei(銀河緣木)

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.