鱼C论坛

 找回密码
 立即注册
查看: 200|回复: 0

[JavaScript] 哪个html5 网页音乐播放器能指定单句重复次数呢?

[复制链接]
发表于 2024-3-20 15:14:30 | 显示全部楼层 |阅读模式
5鱼币
本帖最后由 blackantt 于 2024-3-20 18:28 编辑

AI写的(https://fishc.com.cn/thread-241179-1-1.html)  老是不对。

1. 能改改将就实现: 从1.mp3,1.srt 或者 1.lrc读句子,然后用按钮指定每句读 3 次,再跳到下一句,读3次。直至mp3播放完吗。
2.或者  github 上有这种现成的html5 网页播放器吗?(这里有一个,就是不能按句复读,供参考,https://imajineweb.com/javascriptaudioplayer/)

下面这个是 Gemini 写的,还是不行

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4.   <title>HTML5 Video with SRT Subtitles</title>
  5. </head>
  6. <body>
  7.   <video id="video" width="320" height="240" controls>
  8.     <source src="1.mp3" type="audio/mpeg">
  9.   </video>
  10.   <div id="subtitles"></div>

  11.   <script>
  12.     // Get the video and subtitles elements
  13.     var video = document.getElementById('video');
  14.     var subtitles = document.getElementById('subtitles');

  15.     // Create a new XMLHttpRequest object
  16.     var xhr = new XMLHttpRequest();

  17.     // Open a GET request to the SRT file
  18.     xhr.open('GET', '1.srt');

  19.     // Set the response type to text
  20.     xhr.responseType = 'text';

  21.     // Send the request
  22.     xhr.send();

  23.     // When the request is complete, parse the SRT file
  24.     xhr.onload = function() {
  25.       if (xhr.status === 200) {
  26.         // Parse the SRT file into an array of subtitle objects
  27.         var subtitlesArray = parseSRT(xhr.responseText);

  28.         // Add the subtitles to the page
  29.         for (var i = 0; i < subtitlesArray.length; i++) {
  30.           var subtitle = subtitlesArray[i];
  31.           var p = document.createElement('p');
  32.           p.textContent = subtitle.text;
  33.           p.style.display = 'none';
  34.           subtitles.appendChild(p);
  35.         }

  36.         // Listen for the video's timeupdate event
  37.         video.addEventListener('timeupdate', function() {
  38.           // Get the current time of the video
  39.           var currentTime = video.currentTime;

  40.           // Loop through the subtitles and find the one that matches the current time
  41.           for (var i = 0; i < subtitlesArray.length; i++) {
  42.             var subtitle = subtitlesArray[i];

  43.             // If the current time is between the start and end times of the subtitle, display the subtitle
  44.             if (currentTime >= subtitle.start && currentTime <= subtitle.end) {
  45.               var p = subtitles.querySelector('p:nth-child(' + (i+1));
  46.               p.style.display = 'block';
  47.               setTimeout(function () {
  48.                 p.style.display = 'none';
  49.               }, subtitle.end - subtitle.start);
  50.               setTimeout(function () {
  51.                 p.style.display = 'block';
  52.               }, subtitle.end - subtitle.start);
  53.               setTimeout(function () {
  54.                 p.style.display = 'none';
  55.               }, subtitle.end - subtitle.start);
  56.             } else {
  57.               var p = subtitles.querySelector('p:nth-child(' + (i+1));
  58.               p.style.display = 'none';
  59.             }
  60.           }
  61.         });
  62.       }
  63.     };

  64.     // Parse the SRT file into an array of subtitle objects
  65.     function parseSRT(srt) {
  66.       // Split the SRT file into an array of lines
  67.       var lines = srt.split('\n');

  68.       // Create an array of subtitle objects
  69.       var subtitlesArray = [];

  70.       // Loop through the lines and create a subtitle object for each line
  71.       for (var i = 0; i < lines.length; i++) {
  72.         // Get the start and end times of the subtitle
  73.         var timeString = lines[i];
  74.         var timeParts = timeString.split(' --> ');
  75.         var startTime = timeParts[0];
  76.         var endTime = timeParts[1];

  77.         // Get the text of the subtitle
  78.         var text = lines[i+1];

  79.         // Create a subtitle object
  80.         var subtitle = {
  81.           start: convertTimeStringToSeconds(startTime),
  82.           end: convertTimeStringToSeconds(endTime),
  83.           text: text
  84.         };

  85.         // Add the subtitle object to the array
  86.         subtitlesArray.push(subtitle);

  87.         // Skip the next line (which is an empty line)
  88.         i++;
  89.       }

  90.       // Return the array of subtitle objects
  91.       return subtitlesArray;
  92.     }

  93.     // Convert a time string to seconds
  94.     function convertTimeStringToSeconds(timeString) {
  95.       // Split the time string into hours, minutes, seconds, and milliseconds
  96.       var parts = timeString.split(':');
  97.       var hours = parseInt(parts[0]);
  98.       var minutes = parseInt(parts[1]);
  99.       var seconds = parseInt(parts[2]);
  100.       var milliseconds = parseInt(parts[3]);

  101.       // Convert the hours, minutes, and seconds to seconds
  102.       var totalSeconds = hours * 3600 + minutes * 60 + seconds;

  103.       // Add the milliseconds to the total seconds
  104.       totalSeconds += milliseconds / 1000;

  105.       // Return the total seconds
  106.       return totalSeconds;
  107.     }
  108.   </script>
  109. </body>
  110. </html>
复制代码


下面这个也不行
  1. <html>
  2. <head>
  3.   <title>HTML5 音频播放器</title>
  4. </head>
  5. <body>
  6.   <h1>HTML5 音频播放器</h1>
  7.   <audio id="audio" controls>
  8.     <source src="1.mp3">
  9.   </audio>
  10.   <div id="subtitles"></div>

  11.   <script>
  12.     var audio = document.getElementById('audio');
  13.     var subtitles = document.getElementById('subtitles');

  14.     // 加载字幕文件
  15.     var xhr = new XMLHttpRequest();
  16.     xhr.open('GET', '1.srt');
  17.     xhr.setRequestHeader('Content-Type', 'text/plain');
  18.     xhr.onload = function() {
  19.       if (xhr.status === 200) {
  20.         // 解析字幕文件
  21.         var srt = xhr.responseText;
  22.         var lines = srt.split('\n');
  23.         var cues = [];
  24.         for (var i = 0; i < lines.length; i++) {
  25.           var line = lines[i].trim();
  26.           if (line.startsWith('WEBVTT')) {
  27.             continue; // 跳过 WebVTT 头部
  28.           }

  29.           var parts = line.split(' --> ');
  30.           var start = parts[0];
  31.           var end = parts[1];
  32.           var text = parts[2];

  33.           cues.push({
  34.             start: start,
  35.             end: end,
  36.             text: text
  37.           });
  38.         }

  39.         // 将字幕添加到 DOM 中
  40.         for (var i = 0; i < cues.length; i++) {
  41.           var cue = cues[i];

  42.           var p = document.createElement('p');
  43.           p.innerHTML = cue.text;
  44.           subtitles.appendChild(p);
  45.         }

  46.         // 添加事件监听器以在音频播放时显示字幕
  47.         audio.addEventListener('timeupdate', function() {
  48.           // 获取当前播放时间
  49.           var time = audio.currentTime;

  50.           // 查找当前正在播放的字幕
  51.           var currentCue = null;
  52.           for (var i = 0; i < cues.length; i++) {
  53.             var cue = cues[i];
  54.             if (time >= cue.start && time <= cue.end) {
  55.               currentCue = cue;
  56.               break;
  57.             }
  58.           }

  59.           // 如果找到了匹配的字幕,则显示它
  60.           if (currentCue) {
  61.             for (var i = 0; i < 3; i++) {
  62.               subtitles.children[i].innerHTML = currentCue.text;
  63.             }
  64.           }
  65.         });
  66.       }
  67.     };
  68.     xhr.send();
  69.   </script>
  70. </body>
  71. </html>
复制代码

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-28 15:47

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表