需要两个素材。 歌词与音乐.wav

- package week3.exam6;
-
- public class Info {
- private final String info;
- public Info(String info){
- this.info=info;
- }
-
- public String getInfo() {
- return info;
- }
- public String toString(){
- return info;
- }
- }
- package week3.exam6;
-
- public class Lyric extends Info{
- private long time;
- private String strTime;
-
- public long getTime() {
- return time;
- }
- public Lyric(String lyric,String strTime){
- super(lyric);
- this.strTime=strTime;
- time = calSeconds(this.strTime);
- }
- private long calSeconds(String str){
- long minute;
- double second;
- String[] arrStr=str.split(":");
- minute=Long.parseLong(arrStr[0]);
- second=Double.parseDouble(arrStr[1]);
- return(long)((minute*60+second)*1000);
- }
- public String toString(){
- return strTime +" "+super.toString();
- }
- }
- package week3.exam6;
-
- import javax.sound.sampled.*;
- import java.io.BufferedReader;
- import java.io.File;
- import java.io.FileReader;
- import java.io.IOException;
-
- public class Yun2_4 {
- static Lyric[] lyrics = new Lyric[100];// 定义一个数组用于存储歌词
- static int length = 0;// 实际歌词的元素个数
-
- public static void main(String[] args) {
- String cyanText="\u001B[36m";
- String yellowText="\u001B[33m";
- String resetText="\u001B[0m";
- System.out.println(cyanText +"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~成员信息~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"+resetText);
- System.out.println(cyanText + " 云二 4组 2220010114 " + yellowText + " 李松博 " + resetText + " 何以解忧?唯有暴富!");
- System.out.println(cyanText + " 云二 4组 2220010070 " + yellowText + " 张兆鑫 " + resetText + " 读万卷书行万里路");
- System.out.println(cyanText + " 云二 4组 2220010048 " + yellowText + " 陈鑫 " + resetText + " 何学如逆水行舟,不进则退。");
- System.out.println(cyanText + " 云二 4组 2220020055 " + yellowText + " 刘子厚 " + resetText + " 改变总是好事。");
- System.out.println(cyanText + " 云二 4组 2220010103 " + yellowText + " 毛宇恒 " + resetText + " 努力加油!");
- System.out.println(cyanText + " 云二 4组 2220020044 " + yellowText + " 任旭龙 " + resetText + " 光而不耀,与光同行");
-
- System.out.println(cyanText +"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"+ resetText);
- System.out.println();
- System.out.println();
- System.out.println("\u001B[32m"+"欢迎使用音乐播放器!");
-
-
- System.out.println("\u001B[33m"+ "正在播放音乐:真心英雄 - 李宗盛");//音乐名称
-
- String musicFile = "真心英雄.wav"; //歌曲文件的路径,绝对路径或者相对路径,音乐格式个别不能播放
-
- Clip clip = null;//播放音乐的接口,初始化
- try {
- File file = new File(musicFile);
- AudioInputStream audioStream = AudioSystem.getAudioInputStream(file);//通过AudioSystem.getAudioInputStream(file)方法获取音频输入流audioStream
- clip = AudioSystem.getClip();//AudioSystem.getClip()用于获取一个clip对象
- clip.open(audioStream);//准备播放音乐
- } catch (UnsupportedAudioFileException | LineUnavailableException | IOException e) {
- e.printStackTrace();
- }//处理音乐不能播放的情况
-
-
- String[] arr = readFile("hero.lrc");//歌词的路径,可以是相对路径或绝对路径
-
- // 加工数据
- processingData(arr);
-
- //歌词排序
- sort();
-
- clip.start();//打开音乐播放
- showLyric();//歌词显示
- }
-
- public static String[] readFile(String fileName) {
- try (BufferedReader reader = new BufferedReader(new FileReader(fileName))) {
- /*
- * 使用try-with-resources语句,创建一个BufferedReader对象并初始化它
- *BufferedReader将使用FileReader来读取指定文件的内容*/
- String[] arrStr = new String[100];
- int cnt = 0;//用于记录行数
- String line;
- while ((line = reader.readLine()) != null) {
- arrStr[cnt] = line;
- cnt++;
- /*
- * 通过循环reader.readLine()方法会读取文件的下一行,并将其存储在line变量中
- * 如果读取的行不为空,则将其存储在arrStr数组的当前索引位置,然后索引++*/
- }
- String[] arr = new String[cnt]; //创建一个新的数组,大小为歌词拥有的行数
- System.arraycopy(arrStr, 0, arr, 0, cnt);
- /*
- * System.arraycopy复制方法,复制一个新的数组去掉多余的空
- * arrStr表示是原数组,0表示起始位置,arr表示复制的目标数组,0表示起始位置,cnt表示复制元素的个数*/
- return arr;//返回值arr数组
- } catch (IOException e) {
- throw new RuntimeException(e);
- }//错误处理方法
- }
-
- /*try {
- Scanner sc = new Scanner(new File(fileName));
- String[] arrStr = new String[100];
- while (sc.hasNext()) {
- String line = sc.nextLine();
- arrStr[cnt] = line;
- cnt++;
- }
- String[] arr = new String[cnt];
- System.arraycopy(arrStr, 0, arr, 0, cnt);
- sc.close();
- return arr;
- } catch (FileNotFoundException e) {
- throw new RuntimeException(e);
- }
- }
- */
- public static void processingData(String[] arr) {
- for (String s : arr) {
- Lyric[] tmp = lyricSplit(s);//将当前字符串s拆分为Lyric对象数组tmp
- System.arraycopy(tmp, 0, lyrics, length, tmp.length);// System.arraycopy()方法将tmp数组中的元素复制到全局的lyrics数组
- length += tmp.length;//更新歌词长度
- }
- }
-
- // 歌词字符串拆分为Lyric对象数组,其中每个Lyric对象包含了歌词内容和对应的时间。
- public static Lyric[] lyricSplit(String str) {
- String[] strArr = str.split("]");
- int length = strArr.length - 1;// 使用split()方法将字符串str按照"]"进行拆分,得到一个字符串数组strArr。数组的长度减1得到变量length,表示有几个显示点。
- String lyric = strArr[length];//从strArr数组中获取最后一个元素,即歌词内容,赋值给变量lyric
- Lyric[] lyrics = new Lyric[length];//建一个长度为length的Lyric对象数组lyrics
- for (int i = 0; i < length; i++) {
- strArr[i] = strArr[i].substring(1);
- lyrics[i] = new Lyric(lyric, strArr[i]);
- /*用循环遍历strArr数组的前length个元素。
- 在每次循环中,将strArr[i]的第一个字符去掉,得到去掉显示点的时间字符串,
- 然后使用lyric和时间字符串创建一个Lyric对象,并将其赋值给lyrics数组的相应位置。
- * */
- }
- return lyrics;//返回lyrics数组,其中包含了拆分后的歌词数据
- }
-
- // 冒泡法排序歌词
- public static void sort() {
- for (int i = 0; i < length - 1; i++) {//比较轮数
- for (int j = 0; j < length - i - 1; j++) {//比较次数
- if (lyrics[j].getTime() > lyrics[j + 1].getTime()) {//如果前面的歌词时间大于后面则调换顺序
- Lyric lyric = lyrics[j];
- lyrics[j] = lyrics[j + 1];
- lyrics[j + 1] = lyric;
- }
- }
- }
- }
-
- public static void showLyric() {
- long start = System.currentTimeMillis();//首先获取当前时间的毫秒数,并将其赋值给变量start作为起始时间
- int cnt = 0;//追踪已经展示的歌词数量
- System.out.println("\u001B[36m" + " ~~~~~~~~~~~~~~~~~~~~~~歌词信息~~~~~~~~~~~~~~~~~~~~~~~~~" + "\u001B[0m");
-
- while (cnt < length) {//只要cnt小于歌词数组的长度,就执行以下操作
- try {
- Thread.sleep(10);
- } catch (InterruptedException e) {
- throw new RuntimeException(e);
- }
- long current = System.currentTimeMillis() - start;//计算当前时间与起始时间的差值,并将其赋值给变量current,表示已经过去的时间
- if (current > lyrics[cnt].getTime()) {//如果current大于当前歌词对象的时间,说明该歌词应该被展示
- String lyric = lyrics[cnt].toString();//将当前歌词对象转换为字符串,并将其赋值给变量lyric
- String formattedLyric = "\u001B[35m" + "\u001B[1m" + "\u001B[4m" + lyric + "\u001B[0m"; // 设置歌词颜色为红色 加粗 下划线
- System.out.println("\u001B[34m" + " * " + "\u001B[0m" + formattedLyric);
-
- cnt++;
-
- }
- }
- System.out.println("播放结束了~~~!");
- }
-
- }
//hero.lrc
- [ti:真心英雄]
- [ar:成龙]
- [al:1]
- [by:w]
- [00:00.00][00:08.22]周华健、成龙、黄耀明、李宗盛演唱
- [00:09.08]李宗盛词曲
- [01:53.46][00:10.07][01:55.54][00:11.60]周:在我心中曾经有一个梦
- [02:00.11][00:17.34]要用歌声让你忘了所有的痛
- [02:05.68][00:21.29]成:灿烂星空谁是真的英雄
- [02:10.49][00:27.87]平凡的人们给我最多感动
- [02:15.50][00:32.20]黄:再没有恨也没有了痛
- [02:20.91][00:37.88]但愿人间处处都有爱的影踪
- [02:26.07][00:41.89]李:用我们的歌换你真心笑容
- [02:31.23][00:48.23]合:祝福你的人生从此与众不同
- [02:35.69][00:52.74][03:18.02][02:36.61][00:53.96]把握生命里的每一分钟
- [03:22.81][02:41.27][00:58.68]全力以赴我们心中的梦
- [03:27.96][02:46.10][01:03.30]不经历风雨怎么见彩虹
- [03:33.09][02:51.98][01:09.05]没有人能随随便便成功
- [03:37.84][02:56.25][01:12.94][03:38.44][02:57.03][01:14.54]把握生命里每一次感动
- [03:43.17][03:01.84][01:18.87]和心爱的朋友热情相拥
- [03:48.41][03:07.48][01:23.90]让真心的话和开心的泪
- [03:53.73][03:12.05][01:29.70]在你我的心里流动
- [03:58.31][01:33.65][03:59.46]LaLaLaLa.........
- [04:18.84]把握生命里每一次感动
- [04:23.97]和心爱的朋友热情相拥
- [04:29.66]让真心的话和开心的泪
- [04:34.64]在你我的心里流动
- [04:39.98]让真心的话和开心的泪
- [04:44.99]在你我的心里流动.
- [04:48.86]
- [04:49.70]
- [04:50.38]
- [01:36.2]
音频直接找博主要吧。