• java 歌词解析 源代码, 在windows10下调试运行成功。


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

    1. package week3.exam6;
    2. public class Info {
    3. private final String info;
    4. public Info(String info){
    5. this.info=info;
    6. }
    7. public String getInfo() {
    8. return info;
    9. }
    10. public String toString(){
    11. return info;
    12. }
    13. }

    1. package week3.exam6;
    2. public class Lyric extends Info{
    3. private long time;
    4. private String strTime;
    5. public long getTime() {
    6. return time;
    7. }
    8. public Lyric(String lyric,String strTime){
    9. super(lyric);
    10. this.strTime=strTime;
    11. time = calSeconds(this.strTime);
    12. }
    13. private long calSeconds(String str){
    14. long minute;
    15. double second;
    16. String[] arrStr=str.split(":");
    17. minute=Long.parseLong(arrStr[0]);
    18. second=Double.parseDouble(arrStr[1]);
    19. return(long)((minute*60+second)*1000);
    20. }
    21. public String toString(){
    22. return strTime +" "+super.toString();
    23. }
    24. }

    1. package week3.exam6;
    2. import javax.sound.sampled.*;
    3. import java.io.BufferedReader;
    4. import java.io.File;
    5. import java.io.FileReader;
    6. import java.io.IOException;
    7. public class Yun2_4 {
    8. static Lyric[] lyrics = new Lyric[100];// 定义一个数组用于存储歌词
    9. static int length = 0;// 实际歌词的元素个数
    10. public static void main(String[] args) {
    11. String cyanText="\u001B[36m";
    12. String yellowText="\u001B[33m";
    13. String resetText="\u001B[0m";
    14. System.out.println(cyanText +"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~成员信息~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"+resetText);
    15. System.out.println(cyanText + " 云二 4组 2220010114 " + yellowText + " 李松博 " + resetText + " 何以解忧?唯有暴富!");
    16. System.out.println(cyanText + " 云二 4组 2220010070 " + yellowText + " 张兆鑫 " + resetText + " 读万卷书行万里路");
    17. System.out.println(cyanText + " 云二 4组 2220010048 " + yellowText + " 陈鑫 " + resetText + " 何学如逆水行舟,不进则退。");
    18. System.out.println(cyanText + " 云二 4组 2220020055 " + yellowText + " 刘子厚 " + resetText + " 改变总是好事。");
    19. System.out.println(cyanText + " 云二 4组 2220010103 " + yellowText + " 毛宇恒 " + resetText + " 努力加油!");
    20. System.out.println(cyanText + " 云二 4组 2220020044 " + yellowText + " 任旭龙 " + resetText + " 光而不耀,与光同行");
    21. System.out.println(cyanText +"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"+ resetText);
    22. System.out.println();
    23. System.out.println();
    24. System.out.println("\u001B[32m"+"欢迎使用音乐播放器!");
    25. System.out.println("\u001B[33m"+ "正在播放音乐:真心英雄 - 李宗盛");//音乐名称
    26. String musicFile = "真心英雄.wav"; //歌曲文件的路径,绝对路径或者相对路径,音乐格式个别不能播放
    27. Clip clip = null;//播放音乐的接口,初始化
    28. try {
    29. File file = new File(musicFile);
    30. AudioInputStream audioStream = AudioSystem.getAudioInputStream(file);//通过AudioSystem.getAudioInputStream(file)方法获取音频输入流audioStream
    31. clip = AudioSystem.getClip();//AudioSystem.getClip()用于获取一个clip对象
    32. clip.open(audioStream);//准备播放音乐
    33. } catch (UnsupportedAudioFileException | LineUnavailableException | IOException e) {
    34. e.printStackTrace();
    35. }//处理音乐不能播放的情况
    36. String[] arr = readFile("hero.lrc");//歌词的路径,可以是相对路径或绝对路径
    37. // 加工数据
    38. processingData(arr);
    39. //歌词排序
    40. sort();
    41. clip.start();//打开音乐播放
    42. showLyric();//歌词显示
    43. }
    44. public static String[] readFile(String fileName) {
    45. try (BufferedReader reader = new BufferedReader(new FileReader(fileName))) {
    46. /*
    47. * 使用try-with-resources语句,创建一个BufferedReader对象并初始化它
    48. *BufferedReader将使用FileReader来读取指定文件的内容*/
    49. String[] arrStr = new String[100];
    50. int cnt = 0;//用于记录行数
    51. String line;
    52. while ((line = reader.readLine()) != null) {
    53. arrStr[cnt] = line;
    54. cnt++;
    55. /*
    56. * 通过循环reader.readLine()方法会读取文件的下一行,并将其存储在line变量中
    57. * 如果读取的行不为空,则将其存储在arrStr数组的当前索引位置,然后索引++*/
    58. }
    59. String[] arr = new String[cnt]; //创建一个新的数组,大小为歌词拥有的行数
    60. System.arraycopy(arrStr, 0, arr, 0, cnt);
    61. /*
    62. * System.arraycopy复制方法,复制一个新的数组去掉多余的空
    63. * arrStr表示是原数组,0表示起始位置,arr表示复制的目标数组,0表示起始位置,cnt表示复制元素的个数*/
    64. return arr;//返回值arr数组
    65. } catch (IOException e) {
    66. throw new RuntimeException(e);
    67. }//错误处理方法
    68. }
    69. /*try {
    70. Scanner sc = new Scanner(new File(fileName));
    71. String[] arrStr = new String[100];
    72. while (sc.hasNext()) {
    73. String line = sc.nextLine();
    74. arrStr[cnt] = line;
    75. cnt++;
    76. }
    77. String[] arr = new String[cnt];
    78. System.arraycopy(arrStr, 0, arr, 0, cnt);
    79. sc.close();
    80. return arr;
    81. } catch (FileNotFoundException e) {
    82. throw new RuntimeException(e);
    83. }
    84. }
    85. */
    86. public static void processingData(String[] arr) {
    87. for (String s : arr) {
    88. Lyric[] tmp = lyricSplit(s);//将当前字符串s拆分为Lyric对象数组tmp
    89. System.arraycopy(tmp, 0, lyrics, length, tmp.length);// System.arraycopy()方法将tmp数组中的元素复制到全局的lyrics数组
    90. length += tmp.length;//更新歌词长度
    91. }
    92. }
    93. // 歌词字符串拆分为Lyric对象数组,其中每个Lyric对象包含了歌词内容和对应的时间。
    94. public static Lyric[] lyricSplit(String str) {
    95. String[] strArr = str.split("]");
    96. int length = strArr.length - 1;// 使用split()方法将字符串str按照"]"进行拆分,得到一个字符串数组strArr。数组的长度减1得到变量length,表示有几个显示点。
    97. String lyric = strArr[length];//从strArr数组中获取最后一个元素,即歌词内容,赋值给变量lyric
    98. Lyric[] lyrics = new Lyric[length];//建一个长度为length的Lyric对象数组lyrics
    99. for (int i = 0; i < length; i++) {
    100. strArr[i] = strArr[i].substring(1);
    101. lyrics[i] = new Lyric(lyric, strArr[i]);
    102. /*用循环遍历strArr数组的前length个元素。
    103. 在每次循环中,将strArr[i]的第一个字符去掉,得到去掉显示点的时间字符串,
    104. 然后使用lyric和时间字符串创建一个Lyric对象,并将其赋值给lyrics数组的相应位置。
    105. * */
    106. }
    107. return lyrics;//返回lyrics数组,其中包含了拆分后的歌词数据
    108. }
    109. // 冒泡法排序歌词
    110. public static void sort() {
    111. for (int i = 0; i < length - 1; i++) {//比较轮数
    112. for (int j = 0; j < length - i - 1; j++) {//比较次数
    113. if (lyrics[j].getTime() > lyrics[j + 1].getTime()) {//如果前面的歌词时间大于后面则调换顺序
    114. Lyric lyric = lyrics[j];
    115. lyrics[j] = lyrics[j + 1];
    116. lyrics[j + 1] = lyric;
    117. }
    118. }
    119. }
    120. }
    121. public static void showLyric() {
    122. long start = System.currentTimeMillis();//首先获取当前时间的毫秒数,并将其赋值给变量start作为起始时间
    123. int cnt = 0;//追踪已经展示的歌词数量
    124. System.out.println("\u001B[36m" + " ~~~~~~~~~~~~~~~~~~~~~~歌词信息~~~~~~~~~~~~~~~~~~~~~~~~~" + "\u001B[0m");
    125. while (cnt < length) {//只要cnt小于歌词数组的长度,就执行以下操作
    126. try {
    127. Thread.sleep(10);
    128. } catch (InterruptedException e) {
    129. throw new RuntimeException(e);
    130. }
    131. long current = System.currentTimeMillis() - start;//计算当前时间与起始时间的差值,并将其赋值给变量current,表示已经过去的时间
    132. if (current > lyrics[cnt].getTime()) {//如果current大于当前歌词对象的时间,说明该歌词应该被展示
    133. String lyric = lyrics[cnt].toString();//将当前歌词对象转换为字符串,并将其赋值给变量lyric
    134. String formattedLyric = "\u001B[35m" + "\u001B[1m" + "\u001B[4m" + lyric + "\u001B[0m"; // 设置歌词颜色为红色 加粗 下划线
    135. System.out.println("\u001B[34m" + " * " + "\u001B[0m" + formattedLyric);
    136. cnt++;
    137. }
    138. }
    139. System.out.println("播放结束了~~~!");
    140. }
    141. }

    //hero.lrc

    1. [ti:真心英雄]
    2. [ar:成龙]
    3. [al:1]
    4. [by:w]
    5. [00:00.00][00:08.22]周华健、成龙、黄耀明、李宗盛演唱
    6. [00:09.08]李宗盛词曲
    7. [01:53.46][00:10.07][01:55.54][00:11.60]周:在我心中曾经有一个梦
    8. [02:00.11][00:17.34]要用歌声让你忘了所有的痛
    9. [02:05.68][00:21.29]成:灿烂星空谁是真的英雄
    10. [02:10.49][00:27.87]平凡的人们给我最多感动
    11. [02:15.50][00:32.20]黄:再没有恨也没有了痛
    12. [02:20.91][00:37.88]但愿人间处处都有爱的影踪
    13. [02:26.07][00:41.89]李:用我们的歌换你真心笑容
    14. [02:31.23][00:48.23]合:祝福你的人生从此与众不同
    15. [02:35.69][00:52.74][03:18.02][02:36.61][00:53.96]把握生命里的每一分钟
    16. [03:22.81][02:41.27][00:58.68]全力以赴我们心中的梦
    17. [03:27.96][02:46.10][01:03.30]不经历风雨怎么见彩虹
    18. [03:33.09][02:51.98][01:09.05]没有人能随随便便成功
    19. [03:37.84][02:56.25][01:12.94][03:38.44][02:57.03][01:14.54]把握生命里每一次感动
    20. [03:43.17][03:01.84][01:18.87]和心爱的朋友热情相拥
    21. [03:48.41][03:07.48][01:23.90]让真心的话和开心的泪
    22. [03:53.73][03:12.05][01:29.70]在你我的心里流动
    23. [03:58.31][01:33.65][03:59.46]LaLaLaLa.........
    24. [04:18.84]把握生命里每一次感动
    25. [04:23.97]和心爱的朋友热情相拥
    26. [04:29.66]让真心的话和开心的泪
    27. [04:34.64]在你我的心里流动
    28. [04:39.98]让真心的话和开心的泪
    29. [04:44.99]在你我的心里流动.
    30. [04:48.86]
    31. [04:49.70]
    32. [04:50.38]
    33. [01:36.2]

    音频直接找博主要吧。

  • 相关阅读:
    【Ansible】playbook剧本
    XV4001BD(数字输出陀螺传感器) 汽车级晶振
    FPGA-出租车计价器的实现
    WorldCoder, a Model-Based LLM Agent: Building World Models by Writing Code
    php反序列化+题
    pkg 打包 nodejs
    分分钟让你学会栈和队列
    《C++程序设计原理与实践》笔记 第3章 对象、类型和值
    第三章:Spring常用注解解释
    paddlepaddle 38 使用aistudio部署自己训练的动态图模型
  • 原文地址:https://blog.csdn.net/laocooon/article/details/132779763