Android 實現歌詞同步

2021-08-31 01:48:37 字數 2164 閱讀 2944

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

網路標準的歌詞格式是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 歌詞顯示

4. 歌詞的獲取

一 lrc解析

過程大概如此: 把lrc檔案讀到記憶體裡面,用 sentence資料結構存放. sentence裡面有 fromtime, totime, content三個成員變數。顯示的時候需要這些資料。

二. lrc歌詞顯示

歌詞的繪製通過重寫 ondraw方法.

繪製的**貼出來:

long t = temptime;

int index = getnowsentenceindex(t);

if (index == -1)

sentence now = list.get(index);

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

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

if (f > 0.98f)

shader shader = new lineargradient(0, 0,

now.getcontentwidth(mtxtpaint), 0, new int , new float ,

tilemode.clamp);

mtxtpaint.setshader(shader);

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

private int getnowsentenceindex(long t)

}// throw new runtimeexception("竟然出現了找不到的情況!");

return -1;

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

shader shader = new lineargradient(0, 0,

now.getcontentwidth(mtxtpaint), 0, new int , new float ,

tilemode.clamp);

mtxtpaint.setshader(shader);

private class myhandler extends handler

}四. 歌詞的獲取

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...

android歌詞同步

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