Android 歌詞同步

2021-09-30 04:40:25 字數 3370 閱讀 6094

歌詞同步的原理其實很簡單:

歌詞顯示是乙個執行緒

, 網路標準的歌詞格式是

lrc.

我們看下乙個

lrc文件,其格式就一目了然了.

[ti:愛]

[ar:

小虎隊]

[al:

華納國語情濃13首

] [by:

愛上你了**網]

[02:08.00][00:38.00]

把你的心、我的心串一串

[02:11.00][00:41.00]

串一株幸運草、串乙個同心圓

[02:16.00][00:46.00]

讓所有期待未來的呼喚

[02:19.00][00:49.00]

趁青春做個伴

[03:16.00][02:24.00][00:53.00]

別讓年輕越長大越孤單

[03:19.00][02:27.00][00:56.00]

把我的幸運草種在你的夢田

[03:23.00][02:31.00][01:01.00]

讓地球隨我們的同心圓

….lrc

格式為[

歌詞顯示起始時間

][歌詞顯示結束時間

]歌詞內容。

了解歌詞同步原理,我們可以想到要做如下工作:

1.lrc

解析2.

lrc

歌詞顯示

3.4.

歌詞的獲取

一lrc

解析這邊我推薦

yoyoplayer

. 源**已經非常好的支援了

lrc解析

. 我拿過來就用了

. 過程大概如此: 把

lrc檔案讀到記憶體裡面,用

sentence

資料結構存放

. sentence

裡面有fromtime, totime, content

三個成員變數。顯示的時候需要這些資料。 二

. lrc

歌詞顯示

歌詞的繪製通過重寫

ondraw

方法.

繪製的**貼出來:

longt =

temptime;

intindex = getnowsentenceindex(t);

if(index == -1)

sentence now =

list

.get(index);

floatf = (t - now.getfromtime()) * 1.0f

/ (now.gettotime() - now.getfromtime());

if(f > 0.98f)

shader shader =newlineargradient(0, 0,

now.getcontentwidth(

mtxtpaint

), 0,newint,newfloat,

tilemode.

clamp);

mtxtpaint

.setshader(shader);

canvas.drawtext(now.getcontent(), 0, 20,

mtxtpaint);

上面的**很簡單,關鍵乙個函式是

getnowsentenceindex(t).

. 我們看下

getnowsentenceindex()

如何實現.

/**

* 不可能找不到,因為最開頭要加一句

自己的句子

,所以加了以後就不可能找不到了 *

*@return下標

*/ privateintgetnowsentenceindex(longt) }

// throw new runtimeexception("

竟然出現了找不到的情況!

");

return-1; }

還有乙個歌詞漸變的效果,其關鍵**在與對畫筆的設定,如下

. shader shader =newlineargradient(0, 0,

now.getcontentwidth(

mtxtpaint

), 0,newint,newfloat,

tilemode.

clamp);

mtxtpaint

.setshader(shader); 三

. .

view

的成員變數中

, 以達到同步的目的。

privateclassmyhandler

extendshandler }

四. 歌詞的獲取

. 具體**如下,很好看懂,關鍵**如下:

返回的結果是歌詞的

xml格式轉化字串就可以了,這個太簡單了,懶得說了.

專案原始碼

android歌詞同步

一 lrc歌詞檔案的解析 先要了解lrc檔案的格式,可以參考 總體思路是這樣的,按行讀入歌詞文字,忽略每行中的注釋,即 後的內容 再解析標識標籤 id tags 最後解析出時間標籤及其對應的歌詞語句。具體實現如下 1 忽略注釋 private string removecomment string ...

Android 實現歌詞同步

歌詞的繪製通過重寫 ondraw方法.繪製的 貼出來 long t temptime int index getnowsentenceindex t if index 1 sentence now list.get index float f t now.getfromtime 1.0f now.g...

Android 實現歌詞同步

歌詞同步的原理其實很簡單 網路標準的歌詞格式是lrc.我們看下乙個lrc文件,其格式就一目了然了.ti 愛 ar 小虎隊 al 華納國語情濃13首 by 愛上你了 網 02 08.00 00 38.00 把你的心 我的心串一串 02 11.00 00 41.00 串一株幸運草 串一?同心圓 02 1...