Java播放mp3并简单实现歌词同步
需要導(dǎo)入tritonus_share.jar、mp3spi.jar和jl.jar三個(gè)包
package com.yc.musicplayer;
?
?
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.Clip;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;
?
public class Player02 {
public final Pattern pattern = Pattern.compile("(?<=\\[)[0-9]+?\\:[0-9]+?(\\.[0-9]+)?(?=\\])");//匹配[xx:xx.xx]中的內(nèi)容(不含[])
public AudioInputStream ais;
public AudioFormat format;
private Clip clip;
public ArrayList<Code> lrc = new ArrayList<Code>();
public static void main(String[] args) {
Player02 playLrc = new Player02();
playLrc.readFile();
playLrc.readMp3();
}
?
/**
* 讀取歌詞
*/
public void readFile() {
File file = new File("musics\\好兒好女好家園.lrc");
try (FileInputStream fis = new FileInputStream(file);BufferedReader reader = new BufferedReader(new InputStreamReader(fis,"utf-8"))){
String lrcString = null;
while ((lrcString = reader.readLine()) != null) {
parseLine(lrcString);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
?
/**
* 配置格式
* @param line
*/
public void parseLine(String line) {
Matcher matcher = pattern.matcher(line);
String time = "";
String str = "";
while (matcher.find()) { // 匹配獲取每一句歌詞
time = matcher.group(); // 獲取每句歌詞前面的時(shí)間
str = line.substring(line.indexOf(time) + time.length() + 1); // 獲取后面的歌詞
str = str.replaceAll("^(\\[[0-9]+?\\:[0-9]+?(\\.[0-9]+?)?\\])*", ""); /?// 處理一行有多個(gè)時(shí)間
lrc.add(new Code(strToLong(time), str));
}
}
?
/**
* 獲取歌詞時(shí)長(zhǎng),及其轉(zhuǎn)為毫秒數(shù)
* @param timeStr
* @return
*/
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() {
File file = new File("musics/好兒好女好家園.mp3");
try {
ais = AudioSystem.getAudioInputStream(file); // 獲取音頻流
format = ais.getFormat(); // 獲得此音頻輸入流中聲音數(shù)據(jù)的音頻格式
?
// 獲取此格式聲音的編碼類型 如果不是? 有符號(hào)的線性 PCM 數(shù)據(jù)
if(format.getEncoding() != AudioFormat.Encoding.PCM_SIGNED) {
format = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED,? // 音頻編碼技術(shù)
format.getSampleRate(), // 每秒的樣本數(shù)
16, // 每個(gè)樣本中的位數(shù)
format.getChannels(), // 聲道數(shù)(單聲道 1 個(gè),立體聲 2 個(gè),等等)
format.getChannels() * 2, // 每幀中的字節(jié)數(shù)
format.getSampleRate(), // 每秒的幀數(shù)
false // 指示是否以 big-endian 字節(jié)順序存儲(chǔ)單個(gè)樣本中的數(shù)據(jù)(false 意味著 little-endian)
);
ais = AudioSystem.getAudioInputStream(format, ais);
}
?
//初始化Clip
clip = AudioSystem.getClip();
clip.open(ais);
clip.start();
?
// 同步歌詞
new Thread() {
@Override
public void run() {
int index = 1;
boolean mark = false;
while (true) {
long time = clip.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) {
e.printStackTrace();
}
}
}
}.start();
} catch (UnsupportedAudioFileException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (LineUnavailableException e) {
e.printStackTrace();
}
}
?
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;
}
}
}
總結(jié)
以上是生活随笔為你收集整理的Java播放mp3并简单实现歌词同步的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 天大《电工技术实验》大作业期末考核
- 下一篇: 【Android笔记25】Android