package lrc;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.SourceDataLine;
import javax.sound.sampled.UnsupportedAudioFileException;
public class PlayLrc {
public final Pattern pattern = Pattern.compile("(?<=//[).*?(?=//])");
public AudioInputStream inputStream;
public SourceDataLine dataLine;
public AudioFormat fileFormat;
public ArrayList 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) X
正在加载信息~
推荐阅读
最新回复 (0)
站点信息
- 文章2305
- 用户1336
- 访客11455720
每日一句
Talent without working hard is nothing.
没有努力,天份不代表什么。
没有努力,天份不代表什么。
MySQL 数据库优化
This function has none of DETERMINISTIC, NO SQL, or READS SQL DATA in its de
免ROOT实现模拟点击任意位置
Mobaxterm终端神器
CreateProcessW要注意的细节问题
Autonomous NAT Traversal
【教程】win10 彻底卸载edge浏览器
eclipse工程基于Xposed的一个简单Hook
排名前5的开源在线机器学习
Mac OS最简单及(Karabiner)快捷键设置
发一款C++编写的麻将
VMware NAT端口映射外网访问虚拟机linux
独家发布最新可用My-AutoPost——wordpress 采集器
新会员