Commit 78402008 authored by liujiangnan's avatar liujiangnan

feat: 逐字高亮

parent f5e2f981
我有一个JSON数据,字段解释如下:
```
{
"audio_points": {
"说明": "音频相关的信息",
"字段": {
"audio_url": "音频文件的URL地址",
"points": {
"说明": "音频断点信息数组",
"包含字段": {
"start": "该段音频的开始时间(秒)",
"end": "该段音频的结束时间(秒)",
"text": "该段音频对应的英文文本",
"words": {
"说明": "单词级别的时间轴信息",
"包含字段": {
"start": "单词开始时间",
"end": "单词结束时间",
"word": "具体单词"
}
}
}
}
}
},
"pages": {
"说明": "书籍页面信息数组",
"字段": {
"image_url": "页面图片的URL地址",
"page": "页码",
"start": "该页面内容在音频中的开始时间",
"end": "该页面内容在音频中的结束时间",
"groups": {
"说明": "页面中的文本组信息",
"包含字段": {
"sentence": "识别出的英文句子",
"sentence_cn": "对应的中文翻译",
"start": "句子在音频中的开始时间",
"end": "句子在音频中的结束时间",
"reason": "时间戳缺失或特殊情况的解释说明",
"group": {
"说明": "文本在图片中的位置信息",
"包含字段": {
"rect": {
"x": "文本框左上角的X坐标(相对值)",
"y": "文本框左上角的Y坐标(相对值)",
"width": "文本框的宽度(相对值)",
"height": "文本框的高度(相对值)"
},
"text": "实际文本内容"
}
}
}
}
}
}
}
```
# 绘本阅读器
# 需求
我希望你基于 ./play/index.html ./play/index.css ./play/index.js 帮我实现上面数据的播放功能,主要包含如下:
1、分页展示图片,每一页放一张图片。图片按照页面尺寸适配,如果是长图则左右留白,如果是宽图则上下留白
2、页面的左下角浮动两个按钮,一个是上一页,一个是下一页。
3、播放音频,有播放和暂停的按钮,也浮动在左下角,不用显示音频元素以及音频的进度条等。
4、当音频播放到哪一页,图片分页就翻到对应页。
5、翻页动画做成左右滑动然后渐入渐出。
6、手动点击上一页/下一页如果音频正在播放则立即跳转到对应页的开始时间进行播放,如果音频是暂停或者未播放的状态,这个时候点击播放则也是在这一页的开始时间进行播放,如果音频正好暂停在这一页的某个时间,此时点击播放就是继续播放,不用跳回到本页的开始时间。
7、页面的正下方居中浮动音频对应的字幕(audio_points.points.text),音频播放到哪一句就显示哪一句,播放完就隐藏,字幕的样式用 0.2透明度的黑色背景,然后字体用白色。
8、在图片上画蓝色的矩形框,数据在pages.groups[].group.rect里,是相对于图片宽高的归一化坐标,当音频读到某个矩形框的时候就显示,过了就消失。
9、当音频播放到最后结束,跳回到第一页,音频也从零开始。
基于JSON数据实现的绘本阅读器系统,支持音频同步播放、页面切换、文本高亮等功能。
# 技术框架描述
## 功能特性
Vue2、element-ui 2.12、jQuery 1.12.3
1. **分页展示图片** - 每一页显示一张图片,图片按照页面尺寸适配
2. **页面控制** - 左下角浮动上一页/下一页按钮
3. **音频播放控制** - 播放/暂停按钮,浮动在左下角
4. **音频页面同步** - 音频播放到哪一页就显示对应页面
5. **智能跳转** - 手动切换页面时,如果音频正在播放则跳转到对应页面的开始时间
6. **蓝色矩形框** - 当音频读到某个矩形框时显示,过了就消失
7. **单词高亮** - 当音频读到某个单词时显示黄色背景高亮,切换页时高亮消失
8. **循环播放** - 音频播放结束后自动回到第一页重新开始
都是通过CDN的方式引入好的,你就不要用组件的形式新建其他文件了,都在 ./play/index.js 通过new VUE()的方式 全部实现逻辑部分就好。
## 技术栈
我还给你提供了一个真实的数据文件 /form/defaultData.json
- Vue 2
- Element UI 2.12
- jQuery 1.12.3
- 原生HTML5 Audio API
记得必须要用 Vue2 来实现
## 数据结构
JSON数据包含以下字段:
- `audio_url`: 音频文件地址
- `pages`: 页面数组,每个页面包含:
- `image_url`: 图片地址
- `page`: 页面编号
- `start`: 页面开始时间(秒)
- `end`: 页面结束时间(秒)
- `groups`: 句子组数组,包含文本位置和时间信息
## 使用方法
1. 启动本地服务器:
```bash
cd play
python3 -m http.server 8000
```
2. 打开浏览器访问:`http://localhost:8000`
3. 使用控制按钮:
- 点击播放按钮开始播放音频
- 点击暂停按钮暂停播放
- 点击上一页/下一页按钮切换页面
- 观察文本高亮和矩形框显示效果
## 文件结构
- `index.html` - 主页面模板
- `index.css` - 样式文件
- `index.js` - Vue应用逻辑
- `defaultData_1.json` - 示例数据文件
- `schema.json` - 数据格式定义
## 注意事项
- 确保音频文件和图片文件可以正常访问
- 数据文件需要符合schema.json定义的格式
- 浏览器需要支持HTML5 Audio API
This diff is collapsed.
This diff is collapsed.
html, body {
height: 100%;
* {
margin: 0;
padding: 0;
background: #fff;
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
overflow: hidden;
box-sizing: border-box;
}
body {
min-height: 100vh;
min-width: 100vw;
box-sizing: border-box;
font-family: Arial, sans-serif;
background-color: #f5f5f5;
overflow: hidden;
}
#app {
width: 100vw;
height: 100vh;
position: relative;
overflow: hidden;
background-color: #000;
}
.page-container {
width: 100vw;
height: 100vh;
.book-container {
width: 100%;
height: 100%;
position: relative;
overflow: hidden;
}
.full-image-wrapper {
width: 100vw;
height: 100vh;
display: flex;
align-items: center;
justify-content: center;
background: #fff;
position: absolute;
top: 0;
left: 0;
}
.full-image {
max-width: 100vw;
max-height: 100vh;
width: auto;
height: auto;
display: block;
margin: auto;
.page-image {
max-width: 100%;
max-height: 100%;
object-fit: contain;
background: #fff;
border-radius: 0;
box-shadow: none;
}
.rect-layer {
position: absolute;
border: 3px dashed #2196f3;
box-sizing: border-box;
pointer-events: none;
border-radius: 4px;
z-index: 2;
/* background: rgba(33,150,243,0.15); */
/* opacity: 0; */
/* transform: scale(0.95); */
/* transition: opacity 0.3s ease, transform 0.3s ease; */
display: block;
}
.rect-layer.rect-visible {
opacity: 1;
transform: scale(1);
.image-container {
position: relative;
width: 100%;
height: 100%;
display: flex;
align-items: center;
justify-content: center;
}
/* 单词级别矩形高亮样式 */
.word-rect-layer {
.highlight-rect {
position: absolute;
background-color: rgba(255, 235, 59, 0.3);
border: 2px solid #ffeb3b;
border-radius: 4px;
border: 2px dashed #007bff;
background-color: transparent;
pointer-events: none;
z-index: 3;
box-shadow: 0 0 8px rgba(255, 235, 59, 0.5);
}
.word-rect-layer:hover {
background-color: rgba(255, 235, 59, 0.5);
box-shadow: 0 0 12px rgba(255, 235, 59, 0.8);
z-index: 15;
transition: all 0.2s ease;
}
.subtitle-bar {
position: fixed;
left: 50%;
bottom: 40px;
transform: translateX(-50%);
z-index: 20;
.word-highlight {
position: absolute;
background-color: rgba(255, 235, 59, 0.2);
border: 1px solid #f57f17;
border-radius: 3px;
pointer-events: none;
padding: 10px 20px;
background: rgba(0, 0, 0, 0.2);
border-radius: 4px;
width: 80%;
}
.fixed-btn-group {
position: fixed;
left: 32px;
bottom: 32px;
z-index: 30;
display: flex;
gap: 12px;
align-items: center;
}
.fixed-btn-group .el-button {
border-radius: 50px;
box-shadow: 0 2px 8px rgba(0,0,0,0.12);
padding: 12px 24px;
font-size: 18px;
}
/* 翻页动画 */
.slide-left-enter-active,
.slide-left-leave-active,
.slide-right-enter-active,
.slide-right-leave-active {
transition: all 0.5s ease;
}
.slide-left-enter {
transform: translateX(100%);
opacity: 0;
}
.slide-left-leave-to {
transform: translateX(-100%);
opacity: 0;
}
.slide-right-enter {
transform: translateX(-100%);
opacity: 0;
}
.slide-right-leave-to {
transform: translateX(100%);
opacity: 0;
}
/* 隐藏滚动条 */
body::-webkit-scrollbar, html::-webkit-scrollbar {
display: none;
}
/* 页数提示样式,可后续隐藏 */
#app > div[style*="color:red"] {
margin-bottom: 8px;
}
#test{
margin: 80px;
}
.container {
max-width: 1200px;
margin: 0 auto;
padding: 20px;
}
.content-wrapper {
display: flex;
flex-direction: column;
gap: 20px;
margin-bottom: 20px;
z-index: 1;
transition: all 0.2s ease;
box-shadow: 0 0 5px rgba(255, 235, 59, 0.5);
}
.image-container {
position: relative;
width: 100%;
background: #f5f5f5;
border-radius: 8px;
overflow: hidden;
}
.image-container img {
width: 100%;
height: auto;
display: block;
height: 100%;
display: flex;
align-items: center;
justify-content: center;
}
.highlight-rect {
.controls {
position: absolute;
border: 2px solid #409EFF;
background-color: rgba(64, 158, 255, 0.1);
pointer-events: none;
transition: all 0.3s ease;
bottom: 20px;
left: 20px;
z-index: 100;
display: flex;
gap: 10px;
}
.subtitle-container {
min-height: 60px;
padding: 15px;
background: #fff;
border-radius: 8px;
box-shadow: 0 2px 12px 0 rgba(0,0,0,0.1);
.control-btn {
width: 50px;
height: 50px;
border: none;
border-radius: 50%;
background-color: rgba(255, 255, 255, 0.9);
color: #333;
font-size: 18px;
cursor: pointer;
display: flex;
align-items: center;
justify-content: center;
transition: all 0.3s ease;
box-shadow: 0 2px 8px rgba(0, 0, 0, 0.2);
}
.subtitle-text {
color: #fff;
font-size: 2.4rem;
font-weight: bold;
text-align: center;
line-height: 1.5;
.control-btn:hover {
background-color: rgba(255, 255, 255, 1);
transform: scale(1.1);
}
.subtitle-text span {
display: inline-block;
margin: 0 8px;
transition: all 0.2s ease;
.control-btn:active {
transform: scale(0.95);
}
.subtitle-text .highlighted-word {
color: #ffeb3b;
text-shadow: 0 0 10px rgba(255, 235, 59, 0.8);
transform: scale(1.05);
font-weight: 900;
animation: wordPulse 0.5s ease-in-out;
.control-btn:disabled {
opacity: 0.5;
cursor: not-allowed;
}
@keyframes wordPulse {
0% { transform: scale(1); }
50% { transform: scale(1.1); }
100% { transform: scale(1.05); }
.play-btn {
background-color: rgba(76, 175, 80, 0.9);
color: white;
}
.controls {
display: flex;
justify-content: center;
gap: 20px;
padding: 20px 0;
.play-btn:hover {
background-color: rgba(76, 175, 80, 1);
}
.controls .el-button {
min-width: 100px;
.pause-btn {
background-color: rgba(255, 152, 0, 0.9);
color: white;
}
@media (max-width: 768px) {
.container {
padding: 10px;
}
.controls {
flex-direction: column;
align-items: center;
}
.controls .el-button {
width: 100%;
max-width: 300px;
}
.pause-btn:hover {
background-color: rgba(255, 152, 0, 1);
}
/* 调试面板样式 */
.debug-panel {
position: fixed;
.page-info {
position: absolute;
top: 20px;
right: 20px;
background: rgba(0, 0, 0, 0.8);
color: #fff;
padding: 15px;
border-radius: 8px;
background-color: rgba(0, 0, 0, 0.7);
color: white;
padding: 10px 15px;
border-radius: 5px;
font-size: 14px;
z-index: 100;
min-width: 200px;
}
.debug-info div {
margin-bottom: 5px;
font-family: monospace;
}
/* 预加载界面样式 */
.loading-screen {
position: fixed;
top: 0;
left: 0;
width: 100vw;
height: 100vh;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
display: flex;
align-items: center;
justify-content: center;
z-index: 9999;
}
.loading-content {
text-align: center;
.loading {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
color: white;
max-width: 400px;
padding: 40px;
}
.loading-spinner {
width: 60px;
height: 60px;
border: 4px solid rgba(255, 255, 255, 0.3);
border-top: 4px solid white;
border-radius: 50%;
animation: spin 1s linear infinite;
margin: 0 auto 20px;
}
@keyframes spin {
0% { transform: rotate(0deg); }
100% { transform: rotate(360deg); }
}
.loading-text {
font-size: 24px;
font-weight: bold;
margin-bottom: 30px;
}
.loading-progress {
margin-top: 20px;
}
.progress-bar {
width: 100%;
height: 8px;
background: rgba(255, 255, 255, 0.3);
border-radius: 4px;
overflow: hidden;
margin-bottom: 10px;
}
.progress-fill {
height: 100%;
background: white;
border-radius: 4px;
transition: width 0.3s ease;
font-size: 18px;
z-index: 200;
}
.progress-text {
font-size: 16px;
opacity: 0.8;
.error {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
color: #ff4444;
font-size: 18px;
text-align: center;
z-index: 200;
}
/* 主要内容样式 */
.main-content {
width: 100vw;
height: 100vh;
position: relative;
overflow: hidden;
}
\ No newline at end of file
......@@ -4,7 +4,7 @@
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Book Player</title>
<title>绘本阅读器</title>
<link href="./index.css" rel="stylesheet" >
<script type="text/javascript" src="//teach.cdn.ireadabc.com/h5template/h5-static-lib/js/jquery-1.12.3.min.js"></script>
<script type="text/javascript" src="//teach.cdn.ireadabc.com/h5template/h5-static-lib/js/air.js"></script>
......@@ -16,72 +16,82 @@
</head>
<body>
<div id="app">
<!-- 预加载界面 -->
<div class="loading-screen" v-if="isLoading">
<div class="loading-content">
<div class="loading-spinner"></div>
<div class="loading-text">正在加载资源...</div>
<div class="loading-progress">
<div class="progress-bar">
<div class="progress-fill" :style="{ width: loadingProgress + '%' }"></div>
</div>
<div class="progress-text">{{ loadedAssets }}/{{ totalAssets }} ({{ loadingProgress }}%)</div>
</div>
</div>
<div v-if="loading" class="loading">
加载中...
</div>
<!-- 主要内容 -->
<div class="main-content" v-else>
<div class="page-container">
<div class="full-image-wrapper" :key="currentPage">
<img ref="mainImg" :src="pages[currentPage].image_url" :alt="'Page ' + pages[currentPage].page" v-if="pages.length" class="full-image"/>
<template v-if="currentRects && isRectVisible">
<div v-for="(rect, idx) in currentRects" :key="idx" class="rect-layer rect-visible" :style="getRectStyle(rect)"></div>
</template>
<!-- 单词级别矩形高亮 -->
<template v-if="highlightedWordRects && isRectVisible">
<div
v-for="(wordRect, idx) in highlightedWordRects"
:key="'word-' + idx"
class="word-rect-layer"
:style="getWordRectStyle(wordRect)"
:title="wordRect.text"
></div>
</template>
</div>
</div>
<div class="subtitle-bar" v-if="currentAudioText">
<div class="subtitle-text">
<span
v-for="(word, index) in currentWords"
:key="index"
:class="{ 'highlighted-word': highlightedWordIndexes.includes(index) }"
>
{{ word.word }}
</span>
</div>
</div>
<div v-else-if="error" class="error">
{{ error }}
</div>
<div v-else class="book-container">
<!-- 调试面板 -->
<div class="debug-panel" v-if="isPlaying">
<div class="debug-info">
<div>时间: {{ debugInfo.currentTime.toFixed(2) }}s</div>
<div>单词数: {{ debugInfo.totalWords }}</div>
<div>高亮索引: {{ debugInfo.highlightedIndexes.join(', ') }}</div>
<div>当前单词: {{ debugInfo.currentWord }}</div>
<div>单词矩形数: {{ debugInfo.wordRectsCount }}</div>
<div>组单词数: {{ debugInfo.groupWordsCount }}</div>
</div>
<!-- 图片容器 -->
<div class="image-container">
<img
v-if="currentPage"
:src="currentPage.image_url"
:alt="'第' + (currentPageIndex + 1) + '页'"
class="page-image"
@load="onImageLoad"
@error="onImageError"
/>
<!-- 蓝色矩形框 -->
<div
v-for="(rect, index) in activeRects"
:key="'rect-' + index"
class="highlight-rect"
:style="getRectStyle(rect)"
></div>
<!-- 单词高亮 -->
<div
v-for="(word, index) in activeWords"
:key="'word-' + index"
class="word-highlight"
:style="getWordStyle(word)"
></div>
</div>
<div class="fixed-btn-group">
<el-button
type="primary"
:icon="isPlaying ? 'el-icon-video-pause' : 'el-icon-video-play'"
@click="togglePlay">
{{ isPlaying ? '暂停' : '播放' }}
</el-button>
<el-button icon="el-icon-arrow-left" @click="prevPage">上一页</el-button>
<el-button icon="el-icon-arrow-right" @click="nextPage">下一页</el-button>
<!-- 控制按钮 -->
<div class="controls">
<button
class="control-btn"
:disabled="currentPageIndex === 0"
@click="previousPage"
title="上一页"
>
‹
</button>
<button
v-if="!isPlaying"
class="control-btn play-btn"
@click="playAudio"
title="播放"
>
▶
</button>
<button
v-else
class="control-btn pause-btn"
@click="pauseAudio"
title="暂停"
>
❚❚
</button>
<button
class="control-btn"
:disabled="currentPageIndex === totalPages - 1"
@click="nextPage"
title="下一页"
>
›
</button>
</div>
</div>
</div>
......
This diff is collapsed.
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "教育内容数据结构",
"description": "包含音频和页面数据的教育内容 JSON Schema",
"type": "object",
"properties": {
"audio_url": {
"type": "string",
"format": "uri",
"description": "音频文件的 URL 地址"
},
"pages": {
"type": "array",
"description": "页面数据数组",
"items": {
"type": "object",
"properties": {
"image_url": {
"type": "string",
"format": "uri",
"description": "页面图片的 URL 地址"
},
"page": {
"type": "integer",
"description": "页面编号"
},
"start": {
"type": "number",
"description": "页面开始时间(秒)"
},
"end": {
"type": "number",
"description": "页面结束时间(秒)"
},
"groups": {
"type": "array",
"description": "句子组数组",
"items": {
"type": "object",
"properties": {
"sentence": {
"type": "string",
"description": "英文句子"
},
"sentence_cn": {
"type": "string",
"description": "中文翻译"
},
"start": {
"type": "number",
"description": "句子开始时间(秒)"
},
"end": {
"type": "number",
"description": "句子结束时间(秒)"
},
"reason": {
"type": ["string", "null"],
"description": "处理原因说明,可能为 null"
},
"group": {
"type": "array",
"description": "文本组数组",
"items": {
"type": "object",
"properties": {
"rect": {
"type": "object",
"properties": {
"x": {
"type": "string",
"pattern": "^[0-9]+\\.[0-9]+$",
"description": "矩形区域 X 坐标(相对于图片尺寸的位置)"
},
"y": {
"type": "string",
"pattern": "^[0-9]+\\.[0-9]+$",
"description": "矩形区域 Y 坐标(相对于图片尺寸的位置)"
},
"width": {
"type": "string",
"pattern": "^[0-9]+\\.[0-9]+$",
"description": "矩形区域宽度(相对于图片尺寸的位置)"
},
"height": {
"type": "string",
"pattern": "^[0-9]+\\.[0-9]+$",
"description": "矩形区域高度(相对于图片尺寸的位置)"
}
},
"required": ["x", "y", "width", "height"],
"additionalProperties": false
},
"text": {
"type": "string",
"description": "文本内容"
},
"words": {
"type": "array",
"description": "单词数组",
"items": {
"type": "object",
"properties": {
"text": {
"type": "string",
"description": "单词文本"
},
"rect": {
"type": "object",
"properties": {
"x": {
"type": "string",
"pattern": "^[0-9]+\\.[0-9]+$",
"description": "单词区域 X 坐标(相对于图片尺寸的位置)"
},
"y": {
"type": "string",
"pattern": "^[0-9]+\\.[0-9]+$",
"description": "单词区域 Y 坐标(相对于图片尺寸的位置)"
},
"width": {
"type": "string",
"pattern": "^[0-9]+\\.[0-9]+$",
"description": "单词区域宽度(相对于图片尺寸的位置)"
},
"height": {
"type": "string",
"pattern": "^[0-9]+\\.[0-9]+$",
"description": "单词区域高度(相对于图片尺寸的位置)"
}
},
"required": ["x", "y", "width", "height"],
"additionalProperties": false
},
"chars": {
"type": "array",
"description": "字符数组",
"items": {
"type": "object",
"properties": {
"text": {
"type": "string",
"description": "字符文本"
},
"rect": {
"type": "object",
"properties": {
"x": {
"type": "string",
"pattern": "^[0-9]+\\.[0-9]+$",
"description": "字符区域 X 坐标"
},
"y": {
"type": "string",
"pattern": "^[0-9]+\\.[0-9]+$",
"description": "字符区域 Y 坐标"
},
"width": {
"type": "string",
"pattern": "^[0-9]+\\.[0-9]+$",
"description": "字符区域宽度"
},
"height": {
"type": "string",
"pattern": "^[0-9]+\\.[0-9]+$",
"description": "字符区域高度"
}
},
"required": ["x", "y", "width", "height"],
"additionalProperties": false
}
},
"required": ["text", "rect"],
"additionalProperties": false
}
},
"start": {
"type": ["number", "null"],
"description": "单词开始时间(秒),可能为 null"
},
"end": {
"type": ["number", "null"],
"description": "单词结束时间(秒),可能为 null"
},
"reason": {
"type": ["string", "null"],
"description": "处理原因说明,可能为 null"
}
},
"required": ["text", "rect", "chars"],
"additionalProperties": false
}
}
},
"required": ["rect", "text", "words"],
"additionalProperties": false
}
}
},
"required": ["sentence", "sentence_cn", "start", "end", "reason", "group"],
"additionalProperties": false
}
}
},
"required": ["image_url", "page", "start", "end", "groups"],
"additionalProperties": false
}
}
},
"required": ["audio_url", "pages"],
"additionalProperties": false
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment