• JAVA 歌词解析 采用 TreeMap处理


    [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]

    1. package week3.exam6;
    2. //从文件导入数据,以Map 方式 TreeMap 保存 毫秒数做key,歌词做value
    3. import java.io.File;
    4. import java.io.FileInputStream;
    5. import java.io.FileNotFoundException;
    6. import java.util.*;
    7. public class Play_Lyric {
    8. private static long n = -100;//开始歌词前就立即显示的时间给设置为负值
    9. private static long calSeconds(String str) {
    10. long minute;
    11. double second;
    12. String[] arrStr = str.split(":");
    13. minute = Long.parseLong(arrStr[0]);
    14. second = Double.parseDouble(arrStr[1]);
    15. return (long) ((minute * 60 + second) * 1000);
    16. }
    17. private static String longToStr(Long num) {
    18. long minute;
    19. double second;
    20. minute = num / 1000 / 60;
    21. second = (double) (num % 60000) / 1000;
    22. return "[" + String.format("%1$02d", minute) + ":" + String.format("%1$02d.%2$02d", (int) second, (int) (second - (int) second) * 100) + "]";
    23. }
    24. public static Map lyricSplit(String str) {//解析一行歌词
    25. Map map = new HashMap<>();
    26. if (Character.isAlphabetic(str.charAt(1))) {//第2个字符是字母的处理
    27. map.put(n++, str.substring(str.indexOf(":") + 1, str.length() - 1));
    28. } else {
    29. //正规条件的歌词
    30. String[] strArr = str.split("]");
    31. int length = strArr.length - 1;// 使用split()方法将字符串str按照"]"进行拆分,得到一个字符串数组strArr。数组的长度减1得到变量length,表示有几个显示点。
    32. String lyric = strArr[length];//从strArr数组中获取最后一个元素,即歌词内容,赋值给变量lyric
    33. Lyric[] lyrics = new Lyric[length];//建一个长度为length的Lyric对象数组lyrics
    34. for (int i = 0; i < length; i++) {
    35. strArr[i] = strArr[i].substring(1);
    36. lyrics[i] = new Lyric(lyric, strArr[i]);
    37. map.put(calSeconds(strArr[i]), lyric);
    38. }
    39. }
    40. return map;
    41. }
    42. public static void showLyric(TreeMap map) {
    43. long start = System.currentTimeMillis();//首先获取当前时间的毫秒数,并将其赋值给变量start作为起始时间
    44. System.out.println(" ~~~~~~~~~~~~~~~~~~~~~~歌词信息~~~~~~~~~~~~~~~~~~~~~~~~~");
    45. //将map转为list
    46. //先map转为Set
    47. Set> entrySet = map.entrySet();
    48. //Set转为 list
    49. List> entryList = new ArrayList<>(entrySet);
    50. int i = 0;//追踪歌词展现的位置
    51. while (entryList.get(i).getKey() < 0) {
    52. System.out.println(entryList.get(i++).getValue());
    53. }
    54. while (i < entrySet.size()) {
    55. try {
    56. Thread.sleep(10);
    57. } catch (InterruptedException e) {
    58. throw new RuntimeException(e);
    59. }
    60. long current = System.currentTimeMillis() - start;//计算当前时间与起始时间的差值,并将其赋值给变量current,表示已经过去的时间
    61. if (current > entryList.get(i).getKey()) {//如果current大于当前歌词对象的时间,说明该歌词应该被展示
    62. String lyric = longToStr(entryList.get(i).getKey()) + " " + entryList.get(i).getValue();//将当前歌词对象转换为字符串,并将其赋值给变量lyric
    63. System.out.println(lyric);
    64. i++;
    65. }
    66. }
    67. System.out.println("播放结束了~~~!");
    68. }
    69. public static void main(String[] args) {
    70. TreeMap map = new TreeMap<>();
    71. File file = new File("hero.lrc");
    72. try {
    73. FileInputStream fis = new FileInputStream(file);
    74. Scanner sc = new Scanner(fis);
    75. while (sc.hasNext()) {
    76. String str = sc.nextLine();
    77. map.putAll(lyricSplit(str));
    78. }
    79. sc.close();
    80. showLyric(map);
    81. } catch (FileNotFoundException e) {
    82. throw new RuntimeException(e);
    83. }
    84. }
    85. }

  • 相关阅读:
    consul--基础--05--api
    【虚幻引擎UE】UE4/UE5 GIS相关插件推荐及使用介绍
    文举论金:非农到来!黄金原油全面走势分析策略独家指导
    Flink实现kafka到kafka、kafka到doris的精准一次消费
    苹果手机内嵌h5如何禁止全局弹性效果
    自定义Key类型的字典无法序列化的N种解决方案
    基于注释处理生成代码的RxBus[Deprecated!]
    Python数据分析-2023-2024 NBA 球员统计数据分析
    (算法设计与分析)第四章贪心算法-第一节:贪心算法概述
    轮转数组(超详细!)
  • 原文地址:https://blog.csdn.net/laocooon/article/details/132915113