鱼C论坛

 找回密码
 立即注册
查看: 635|回复: 3

[已解决]有 按句子srt/lrc 字幕 复读的html-css-javascript 代码吗?

[复制链接]
发表于 2023-2-23 12:47:58 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
有 按句子srt/lrc 字幕 复读的html-css-javascript 代码吗?
最佳答案
2023-2-23 14:31:07
下面是一个简单的 HTML/CSS/JavaScript 代码,可以用来实现按句子复读 srt/lrc 字幕:
HTML:
  1. <div id="subtitle-container">
  2.   <div id="subtitle"></div>
  3. </div>
  4. <button id="play-button">播放</button>
复制代码

CSS:
  1. #subtitle-container {
  2.   width: 600px;
  3.   height: 80px;
  4.   border: 1px solid black;
  5.   margin-bottom: 20px;
  6.   overflow: hidden;
  7. }

  8. #subtitle {
  9.   width: 600px;
  10.   height: 80px;
  11.   font-size: 24px;
  12.   line-height: 1.5;
  13.   text-align: center;
  14. }

  15. #play-button {
  16.   font-size: 24px;
  17.   padding: 10px;
  18.   border-radius: 10px;
  19.   background-color: #007bff;
  20.   color: white;
  21.   cursor: pointer;
  22. }
复制代码

JavaScript:
  1. var subtitles = [
  2.   {
  3.     time: 1000,
  4.     text: "第一句话"
  5.   },
  6.   {
  7.     time: 5000,
  8.     text: "第二句话"
  9.   },
  10.   {
  11.     time: 9000,
  12.     text: "第三句话"
  13.   }
  14. ];

  15. var currentSubtitleIndex = 0;
  16. var timeoutId;

  17. function showSubtitle() {
  18.   var subtitleElement = document.getElementById("subtitle");
  19.   subtitleElement.innerText = subtitles[currentSubtitleIndex].text;
  20.   
  21.   timeoutId = setTimeout(function() {
  22.     currentSubtitleIndex++;
  23.     if (currentSubtitleIndex < subtitles.length) {
  24.       showSubtitle();
  25.     }
  26.   }, subtitles[currentSubtitleIndex].time);
  27. }

  28. var playButton = document.getElementById("play-button");
  29. playButton.onclick = function() {
  30.   currentSubtitleIndex = 0;
  31.   clearTimeout(timeoutId);
  32.   showSubtitle();
  33. }
复制代码

说明:

HTML 部分包含一个用于显示字幕的 div 元素和一个用于触发播放的 button 元素。
CSS 部分定义了字幕容器、字幕样式和播放按钮样式。
JavaScript 部分定义了一个包含时间和文本的字幕数组 subtitles,当前字幕的索引 currentSubtitleIndex,和一个定时器 ID timeoutId。
showSubtitle 函数用于显示当前索引指向的字幕,并通过 setTimeout 函数设置下一个字幕的显示时间。
playButton 元素的 onclick 事件处理函数用于初始化当前索引,清除定时器,然后调用 showSubtitle 函数开始播放字幕。
你需要将 subtitles 数组中的时间和文本替换为你自己的 srt/lrc 字幕数据,以及根据需要调整样式和其他代码。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2023-2-23 14:31:07 | 显示全部楼层    本楼为最佳答案   
下面是一个简单的 HTML/CSS/JavaScript 代码,可以用来实现按句子复读 srt/lrc 字幕:
HTML:
  1. <div id="subtitle-container">
  2.   <div id="subtitle"></div>
  3. </div>
  4. <button id="play-button">播放</button>
复制代码

CSS:
  1. #subtitle-container {
  2.   width: 600px;
  3.   height: 80px;
  4.   border: 1px solid black;
  5.   margin-bottom: 20px;
  6.   overflow: hidden;
  7. }

  8. #subtitle {
  9.   width: 600px;
  10.   height: 80px;
  11.   font-size: 24px;
  12.   line-height: 1.5;
  13.   text-align: center;
  14. }

  15. #play-button {
  16.   font-size: 24px;
  17.   padding: 10px;
  18.   border-radius: 10px;
  19.   background-color: #007bff;
  20.   color: white;
  21.   cursor: pointer;
  22. }
复制代码

JavaScript:
  1. var subtitles = [
  2.   {
  3.     time: 1000,
  4.     text: "第一句话"
  5.   },
  6.   {
  7.     time: 5000,
  8.     text: "第二句话"
  9.   },
  10.   {
  11.     time: 9000,
  12.     text: "第三句话"
  13.   }
  14. ];

  15. var currentSubtitleIndex = 0;
  16. var timeoutId;

  17. function showSubtitle() {
  18.   var subtitleElement = document.getElementById("subtitle");
  19.   subtitleElement.innerText = subtitles[currentSubtitleIndex].text;
  20.   
  21.   timeoutId = setTimeout(function() {
  22.     currentSubtitleIndex++;
  23.     if (currentSubtitleIndex < subtitles.length) {
  24.       showSubtitle();
  25.     }
  26.   }, subtitles[currentSubtitleIndex].time);
  27. }

  28. var playButton = document.getElementById("play-button");
  29. playButton.onclick = function() {
  30.   currentSubtitleIndex = 0;
  31.   clearTimeout(timeoutId);
  32.   showSubtitle();
  33. }
复制代码

说明:

HTML 部分包含一个用于显示字幕的 div 元素和一个用于触发播放的 button 元素。
CSS 部分定义了字幕容器、字幕样式和播放按钮样式。
JavaScript 部分定义了一个包含时间和文本的字幕数组 subtitles,当前字幕的索引 currentSubtitleIndex,和一个定时器 ID timeoutId。
showSubtitle 函数用于显示当前索引指向的字幕,并通过 setTimeout 函数设置下一个字幕的显示时间。
playButton 元素的 onclick 事件处理函数用于初始化当前索引,清除定时器,然后调用 showSubtitle 函数开始播放字幕。
你需要将 subtitles 数组中的时间和文本替换为你自己的 srt/lrc 字幕数据,以及根据需要调整样式和其他代码。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2023-2-23 15:37:36 | 显示全部楼层
本帖最后由 blackantt 于 2023-2-23 15:39 编辑
ouyunfu 发表于 2023-2-23 14:31
下面是一个简单的 HTML/CSS/JavaScript 代码,可以用来实现按句子复读 srt/lrc 字幕:
HTML:


整个网上没找到这种复读代码,能否完善功能(输入MP3,srt/lrc/vtt, 定义每句复读次数,取消复读),放到github上。

https://fishc.com.cn/thread-224605-1-1.html, 能跟这个2合1吗?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2023-2-23 16:36:39 | 显示全部楼层
ouyunfu 发表于 2023-2-23 14:31
下面是一个简单的 HTML/CSS/JavaScript 代码,可以用来实现按句子复读 srt/lrc 字幕:
HTML:

能不能再把完整的 html头尾,尤其是 mp3 部分加进来,写成一个简单完整的demo。  不熟java, 一点不对,初学者就难住了。
谢谢
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-5-1 20:29

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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