java歌词解析代码_JAVA写的音乐播放代码附带歌词同步,可移植Android平台

lrc = new ArrayList();

public void readFile() {

File file = new File("D://workspace//Learn//src//lrc//song.lrc");

try {

FileInputStream fis = new FileInputStream(file);

BufferedReader reader = new BufferedReader(new InputStreamReader(

fis));

String lrcString = null;

while ((lrcString = reader.readLine()) != null)

parseLine(lrcString);

} catch (FileNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

private long strToLong(String timeStr) {

String[] s = timeStr.split(":");

int min = Integer.parseInt(s[0]);

String[] ss = s[1].split("//.");

int sec = Integer.parseInt(ss[0]);

int mill = Integer.parseInt(ss[1]);

return min * 60 * 1000 + sec * 1000 + mill * 10;

}

public void readMp3() {

System.out.println("----------play music----------");

File file = new File("D://workspace//Learn//src//lrc//xgfzj.wav");

try {

inputStream = AudioSystem.getAudioInputStream(file);

fileFormat = inputStream.getFormat();

if (fileFormat.getEncoding() != AudioFormat.Encoding.PCM_SIGNED) {

AudioFormat newFormat = new AudioFormat(

AudioFormat.Encoding.PCM_SIGNED,

fileFormat.getSampleRate(), 16,

fileFormat.getChannels(), fileFormat.getChannels() * 2,

fileFormat.getSampleRate(), false);

System.out.println("Converting audio format to " + newFormat);

AudioInputStream newStream = AudioSystem.getAudioInputStream(

newFormat, inputStream);

fileFormat = newFormat;

inputStream = newStream;

}

DataLine.Info info = new DataLine.Info(SourceDataLine.class,

fileFormat);

dataLine = (SourceDataLine) AudioSystem.getLine(info);

dataLine.open(fileFormat, dataLine.getBufferSize());

dataLine.start();

new Thread() {

@Override

public void run() {

// TODO Auto-generated method stub

int index = 1;

boolean mark = false;

while (true) {

long time = dataLine.getMicrosecondPosition() / 1000;

if (time < lrc.get(index).getTime()) {

if (!mark)

System.out.println(lrc.get(index - 1).getStr());

mark = true;

} else {

index++;

mark = false;

}

try {

sleep(100);

} catch (InterruptedException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

}.start();

int bufferSize = (int) fileFormat.getSampleRate()

* fileFormat.getFrameSize();

byte[] buffer = new byte[bufferSize];

int bytesRead = 0;

while (bytesRead >= 0) {

bytesRead = inputStream.read(buffer, 0, buffer.length);

if (bytesRead >= 0) {

dataLine.write(buffer, 0, bytesRead);

}

}

} catch (UnsupportedAudioFileException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (LineUnavailableException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

public void parseLine(String line) {

Matcher matcher = pattern.matcher(line);

while (matcher.find()) {

String time = matcher.group();

String str = line.substring(line.indexOf(time) + time.length() + 1);

Code code = new Code(strToLong(time), str);

lrc.add(code);

// System.out.println(code.getTime() + "/t" + code.getStr());

}

}

public static void main(String[] args) {

// TODO Auto-generated method stub

PlayLrc playLrc = new PlayLrc();

playLrc.readFile();

playLrc.readMp3();

}

class Code {

private long time;

private String str;

public Code(long time, String str) {

setTime(time);

setStr(str);

}

public long getTime() {

return time;

}

public void setTime(long time) {

this.time = time;

}

public String getStr() {

return str;

}

public void setStr(String str) {

this.str = str;

}

}

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值