四零语境前端代码仓库
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

3158 lines
107 KiB

1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
  1. <template>
  2. <view class="audio-controls-wrapper">
  3. <!-- 会员限制页面不显示任何音频控制 -->
  4. <view v-if="isAudioDisabled" class="member-restricted-container">
  5. <!-- 不显示任何内容完全隐藏音频功能 -->
  6. </view>
  7. <!-- 音频加载中 -->
  8. <view v-else-if="isTextPage && isAudioLoading" class="audio-loading-container">
  9. <uv-loading-icon mode="spinner" size="30" color="#06DADC"></uv-loading-icon>
  10. <text class="loading-text">{{currentPage}}页音频加载中请稍等...</text>
  11. </view>
  12. <!-- 正常音频控制栏 -->
  13. <view v-else-if="isTextPage && hasAudioData" class="audio-controls">
  14. <!-- 加载指示器 -->
  15. <view v-if="isAudioLoading" class="loading-indicator">
  16. <uv-loading-icon mode="spinner" size="16" color="#06DADC"></uv-loading-icon>
  17. <text class="loading-indicator-text">正在加载更多音频...</text>
  18. </view>
  19. <view class="audio-time">
  20. <text class="time-text">{{ formatTime(currentTime) }}</text>
  21. <view class="progress-container">
  22. <uv-slider
  23. v-model="sliderValue"
  24. :min="0"
  25. :max="totalTime"
  26. :step="0.1"
  27. activeColor="#06DADC"
  28. backgroundColor="#e0e0e0"
  29. :blockSize="16"
  30. blockColor="#ffffff"
  31. disabled
  32. :customStyle="{ flex: 1, margin: '0 10px' }"
  33. />
  34. </view>
  35. <text class="time-text">{{ formatTime(totalTime) }}</text>
  36. </view>
  37. <view class="audio-controls-row">
  38. <view class="control-btn" @click="toggleLoop">
  39. <uv-icon name="reload" size="20" :color="isLoop ? '#06DADC' : '#999'"></uv-icon>
  40. <text class="control-text">循环</text>
  41. </view>
  42. <view class="control-btn" @click="$emit('previous-page')">
  43. <text class="control-text">上一页</text>
  44. </view>
  45. <view class="play-btn" @click="togglePlay">
  46. <uv-icon :name="isPlaying ? 'pause-circle-fill' : 'play-circle-fill'" size="40" color="#666"></uv-icon>
  47. </view>
  48. <view class="control-btn" @click="$emit('next-page')">
  49. <text class="control-text">下一页</text>
  50. </view>
  51. <view class="control-btn" @click="toggleSpeed" :class="{ 'disabled': !playbackRateSupported }">
  52. <text class="control-text" :style="{ opacity: playbackRateSupported ? 1 : 0.5 }">
  53. {{ playbackRateSupported ? playSpeed + 'x' : '不支持' }}
  54. </text>
  55. </view>
  56. </view>
  57. </view>
  58. </view>
  59. </template>
  60. <script>
  61. import config from '@/mixins/config.js'
  62. export default {
  63. name: 'AudioControls',
  64. mixins: [config],
  65. props: {
  66. // 基础数据
  67. currentPage: {
  68. type: Number,
  69. default: 1
  70. },
  71. courseId: {
  72. type: String,
  73. default: ''
  74. },
  75. voiceId: {
  76. type: String,
  77. default: ''
  78. },
  79. bookPages: {
  80. type: Array,
  81. default: () => []
  82. },
  83. isTextPage: {
  84. type: Boolean,
  85. default: false
  86. },
  87. shouldLoadAudio: {
  88. type: Boolean,
  89. default: false
  90. },
  91. isMember: {
  92. type: Boolean,
  93. default: false
  94. },
  95. currentPageRequiresMember: {
  96. type: Boolean,
  97. default: false
  98. },
  99. pagePay: {
  100. type: Array,
  101. default: () => []
  102. },
  103. isWordAudioPlaying: {
  104. type: Boolean,
  105. default: false
  106. }
  107. },
  108. data() {
  109. return {
  110. // 音频控制相关数据
  111. isPlaying: false,
  112. currentTime: 0,
  113. totalTime: 0,
  114. sliderValue: 0, // 滑動條的值
  115. isDragging: false, // 是否正在拖動滑動條
  116. isLoop: false,
  117. playSpeed: 1.0,
  118. speedOptions: [0.5, 0.8, 1.0, 1.25, 1.5, 2.0], // 根據uni-app文檔的官方支持值
  119. playbackRateSupported: true, // 播放速度控制是否支持
  120. // 音频数组管理
  121. currentPageAudios: [], // 当前页面的音频数组
  122. currentAudioIndex: 0, // 当前播放的音频索引
  123. audioContext: null, // 音频上下文
  124. currentAudio: null, // 当前音频实例
  125. // 音频缓存管理
  126. audioCache: {}, // 页面音频缓存 {pageIndex: {audios: [], totalDuration: 0}}
  127. // 预加载相关状态
  128. isPreloading: false, // 是否正在预加载
  129. preloadProgress: 0, // 预加载进度 (0-100)
  130. preloadQueue: [], // 预加载队列
  131. // 音频加载状态
  132. isAudioLoading: false, // 音频是否正在加载
  133. hasAudioData: false, // 当前页面是否已有音频数据
  134. isVoiceChanging: false, // 音色切换中的加载状态
  135. audioLoadFailed: false, // 音频获取失败状态
  136. // 文本高亮相关
  137. currentHighlightIndex: -1, // 当前高亮的文本索引
  138. // 课程切换相关状态
  139. isJustSwitchedCourse: false, // 是否刚刚切换了课程
  140. // 页面切换防抖相关
  141. pageChangeTimer: null, // 页面切换防抖定时器
  142. isPageChanging: false, // 是否正在切换页面
  143. // 请求取消相关
  144. currentRequestId: null, // 当前音频请求ID
  145. shouldCancelRequest: false, // 是否应该取消当前请求
  146. // 本地音色ID(避免直接修改prop)
  147. localVoiceId: '', // 本地音色ID,从prop初始化
  148. // 倍速检查相关
  149. lastSpeedCheckTime: -1, // 上次检查倍速的时间点
  150. }
  151. },
  152. computed: {
  153. // 计算音频播放进度百分比
  154. progressPercent() {
  155. return this.totalTime > 0 ? (this.currentTime / this.totalTime) * 100 : 0;
  156. },
  157. // 检查当前页面是否有缓存的音频
  158. hasCurrentPageCache() {
  159. const cacheKey = `${this.courseId}_${this.currentPage}_${this.localVoiceId}`;
  160. const cachedData = this.audioCache[cacheKey];
  161. // 更严格的缓存有效性检查
  162. if (!cachedData || !cachedData.audios || cachedData.audios.length === 0) {
  163. return false;
  164. }
  165. // 检查缓存的音色ID是否与当前音色匹配
  166. if (cachedData.voiceId && cachedData.voiceId !== this.localVoiceId) {
  167. // console.warn('缓存音色不匹配:', cachedData.voiceId, '!=', this.localVoiceId);
  168. return false;
  169. }
  170. // 检查音频URL是否有效
  171. const firstAudio = cachedData.audios[0];
  172. if (!firstAudio || !firstAudio.url) {
  173. // console.warn('缓存音频数据无效');
  174. return false;
  175. }
  176. return true;
  177. },
  178. // 判断音频功能是否应该被禁用(会员限制页面且用户非会员)
  179. isAudioDisabled() {
  180. return this.currentPageRequiresMember && !this.isMember;
  181. },
  182. // 检查当前页面是否正在预加载中
  183. isCurrentPagePreloading() {
  184. // 如果全局预加载状态为true,需要检查当前页面是否在预加载队列中
  185. if (this.isPreloading) {
  186. // 检查当前页面是否有缓存(如果有缓存说明已经预加载完成)
  187. const cacheKey = `${this.courseId}_${this.currentPage}_${this.localVoiceId}`;
  188. const hasCache = this.audioCache[cacheKey] && this.audioCache[cacheKey].audios && this.audioCache[cacheKey].audios.length > 0;
  189. // 如果没有缓存且正在预加载,说明当前页面可能正在预加载中
  190. if (!hasCache) {
  191. // console.log('当前页面可能正在预加载中,页面:', this.currentPage, '缓存状态:', hasCache);
  192. return true;
  193. }
  194. }
  195. return false;
  196. }
  197. },
  198. watch: {
  199. // 监听页面变化,重置音频状态
  200. currentPage: {
  201. handler(newPage, oldPage) {
  202. if (newPage !== oldPage) {
  203. // console.log('页面切换:', oldPage, '->', newPage);
  204. // 设置页面切换状态
  205. this.isPageChanging = true;
  206. // 立即重置音频状态,防止穿音
  207. this.resetAudioState();
  208. // 清除之前的防抖定时器
  209. if (this.pageChangeTimer) {
  210. clearTimeout(this.pageChangeTimer);
  211. }
  212. // 使用防抖机制,避免频繁切换时重复加载
  213. this.pageChangeTimer = setTimeout(() => {
  214. this.isPageChanging = false;
  215. // 检查新页面是否有预加载完成的音频缓存
  216. this.$nextTick(() => {
  217. this.checkAndLoadPreloadedAudio();
  218. });
  219. }, 300); // 300ms防抖延迟
  220. }
  221. },
  222. immediate: false
  223. },
  224. // 监听音色变化,更新本地音色ID
  225. voiceId: {
  226. handler(newVoiceId, oldVoiceId) {
  227. if (newVoiceId !== oldVoiceId) {
  228. // console.log('🎵 音色ID变化:', oldVoiceId, '->', newVoiceId);
  229. // 更新本地音色ID
  230. this.localVoiceId = newVoiceId;
  231. }
  232. },
  233. immediate: true // 立即执行,用于初始化
  234. }
  235. },
  236. methods: {
  237. // 创建HTML5 Audio实例并包装为uni-app兼容接口
  238. createHTML5Audio() {
  239. const audio = new Audio();
  240. // 包装为uni-app兼容的接口
  241. const wrappedAudio = {
  242. // 原生HTML5 Audio实例
  243. _nativeAudio: audio,
  244. // 基本属性
  245. get src() { return audio.src; },
  246. set src(value) { audio.src = value; },
  247. get duration() { return audio.duration || 0; },
  248. get currentTime() { return audio.currentTime || 0; },
  249. get paused() { return audio.paused; },
  250. // 支持倍速的关键属性
  251. get playbackRate() { return audio.playbackRate; },
  252. set playbackRate(value) {
  253. try {
  254. audio.playbackRate = value;
  255. // console.log(`🎵 HTML5 Audio倍速设置成功: ${value}x`);
  256. } catch (error) {
  257. console.error('❌ HTML5 Audio倍速设置失败:', error);
  258. }
  259. },
  260. // 基本方法
  261. play() {
  262. return audio.play().catch(error => {
  263. console.error('HTML5 Audio播放失败:', error);
  264. });
  265. },
  266. pause() {
  267. audio.pause();
  268. },
  269. stop() {
  270. audio.pause();
  271. audio.currentTime = 0;
  272. },
  273. seek(time) {
  274. audio.currentTime = time;
  275. },
  276. destroy() {
  277. audio.pause();
  278. audio.src = '';
  279. audio.load();
  280. },
  281. // 事件绑定方法
  282. onCanplay(callback) {
  283. audio.addEventListener('canplay', callback);
  284. },
  285. onPlay(callback) {
  286. audio.addEventListener('play', callback);
  287. },
  288. onPause(callback) {
  289. audio.addEventListener('pause', callback);
  290. },
  291. onEnded(callback) {
  292. audio.addEventListener('ended', callback);
  293. },
  294. onTimeUpdate(callback) {
  295. audio.addEventListener('timeupdate', callback);
  296. },
  297. onError(callback) {
  298. // 包装错误事件,过滤掉非关键错误
  299. const wrappedCallback = (error) => {
  300. // 只在有src且音频正在播放时才传递错误事件
  301. if (audio.src && audio.src.trim() !== '' && !audio.paused) {
  302. callback(error);
  303. } else {
  304. console.log('🔇 HTML5 Audio错误(已忽略):', {
  305. hasSrc: !!audio.src,
  306. paused: audio.paused,
  307. errorType: error.type || 'unknown'
  308. });
  309. }
  310. };
  311. audio.addEventListener('error', wrappedCallback);
  312. },
  313. // 移除事件监听
  314. offCanplay(callback) {
  315. audio.removeEventListener('canplay', callback);
  316. },
  317. offPlay(callback) {
  318. audio.removeEventListener('play', callback);
  319. },
  320. offPause(callback) {
  321. audio.removeEventListener('pause', callback);
  322. },
  323. offEnded(callback) {
  324. audio.removeEventListener('ended', callback);
  325. },
  326. offTimeUpdate(callback) {
  327. audio.removeEventListener('timeupdate', callback);
  328. },
  329. offError(callback) {
  330. audio.removeEventListener('error', callback);
  331. }
  332. };
  333. console.log('🎧 创建HTML5 Audio包装实例,支持倍速播放');
  334. return wrappedAudio;
  335. },
  336. // 检查并自动加载预加载完成的音频
  337. checkAndLoadPreloadedAudio() {
  338. // 只在需要加载音频的页面检查
  339. if (!this.shouldLoadAudio) {
  340. return;
  341. }
  342. // 检查当前页面是否有缓存的音频数据
  343. const pageKey = `${this.courseId}_${this.currentPage}_${this.localVoiceId}`;
  344. const cachedAudio = this.audioCache[pageKey];
  345. if (cachedAudio && cachedAudio.audios && cachedAudio.audios.length > 0) {
  346. // 有缓存:直接显示控制栏
  347. this.currentPageAudios = cachedAudio.audios;
  348. this.totalTime = cachedAudio.totalDuration || 0;
  349. this.hasAudioData = true;
  350. this.isAudioLoading = false;
  351. console.log('检测到缓存音频,显示控制栏');
  352. } else {
  353. // 没有缓存:自动开始加载音频
  354. console.log('没有缓存音频,开始加载当前页面音频');
  355. this.getCurrentPageAudio();
  356. }
  357. },
  358. // 智能分割文本,按句号和逗号分割中英文文本
  359. splitTextIntelligently(text) {
  360. if (!text || typeof text !== 'string') {
  361. return [text];
  362. }
  363. // 判断是否为中文文本(包含中文字符)
  364. const isChinese = /[\u4e00-\u9fa5]/.test(text);
  365. const maxLength = isChinese ? 100 : 200;
  366. // 如果文本长度不超过限制,直接返回
  367. if (text.length <= maxLength) {
  368. return [{
  369. text: text,
  370. startIndex: 0,
  371. endIndex: text.length - 1
  372. }];
  373. }
  374. const segments = [];
  375. let currentText = text;
  376. let globalStartIndex = 0;
  377. while (currentText.length > 0) {
  378. if (currentText.length <= maxLength) {
  379. // 剩余文本不超过限制,直接添加
  380. segments.push({
  381. text: currentText,
  382. startIndex: globalStartIndex,
  383. endIndex: globalStartIndex + currentText.length - 1
  384. });
  385. break;
  386. }
  387. // 在限制长度内寻找最佳分割点
  388. let splitIndex = maxLength;
  389. let bestSplitIndex = -1;
  390. // 优先寻找句号
  391. for (let i = Math.min(maxLength, currentText.length - 1); i >= Math.max(0, maxLength - 50); i--) {
  392. const char = currentText[i];
  393. if (char === '。' || char === '.') {
  394. bestSplitIndex = i + 1; // 包含句号
  395. break;
  396. }
  397. }
  398. // 如果没找到句号,寻找逗号
  399. if (bestSplitIndex === -1) {
  400. for (let i = Math.min(maxLength, currentText.length - 1); i >= Math.max(0, maxLength - 50); i--) {
  401. const char = currentText[i];
  402. if (char === ',' || char === ',' || char === ';' || char === ';') {
  403. bestSplitIndex = i + 1; // 包含标点符号
  404. break;
  405. }
  406. }
  407. }
  408. // 如果还是没找到合适的分割点,使用默认长度
  409. if (bestSplitIndex === -1) {
  410. bestSplitIndex = maxLength;
  411. }
  412. // 提取当前段落
  413. const segment = currentText.substring(0, bestSplitIndex).trim();
  414. if (segment.length > 0) {
  415. segments.push({
  416. text: segment,
  417. startIndex: globalStartIndex,
  418. endIndex: globalStartIndex + segment.length - 1
  419. });
  420. }
  421. // 更新剩余文本和全局索引
  422. currentText = currentText.substring(bestSplitIndex).trim();
  423. globalStartIndex += bestSplitIndex;
  424. }
  425. console.log(`文本分割完成,原长度: ${text.length},分割为 ${segments.length} 段:`, segments);
  426. return segments;
  427. },
  428. // 分批次请求音频
  429. async requestAudioInBatches(text, voiceType) {
  430. const segments = this.splitTextIntelligently(text);
  431. const audioSegments = [];
  432. let totalDuration = 0;
  433. const requestId = this.currentRequestId; // 保存当前请求ID
  434. console.log(`开始分批次请求音频,共 ${segments.length} 段,请求ID: ${requestId}`);
  435. for (let i = 0; i < segments.length; i++) {
  436. // 检查是否应该取消请求
  437. if (this.shouldCancelRequest || this.currentRequestId !== requestId) {
  438. console.log(`请求已取消,停止音频加载,请求ID: ${requestId}`);
  439. return null; // 返回null表示请求被取消
  440. }
  441. const segment = segments[i];
  442. try {
  443. console.log(`请求第 ${i + 1}/${segments.length} 段音频:`, segment.text.substring(0, 50) + '...');
  444. const radioRes = await this.$api.music.textToVoice({
  445. text: segment.text,
  446. voiceType: voiceType,
  447. });
  448. if (radioRes.code === 200 && radioRes.result && radioRes.result.url) {
  449. const audioUrl = radioRes.result.url;
  450. const duration = radioRes.result.time || 0;
  451. audioSegments.push({
  452. url: audioUrl,
  453. text: segment.text,
  454. duration: duration,
  455. startIndex: segment.startIndex,
  456. endIndex: segment.endIndex,
  457. segmentIndex: i,
  458. isSegmented: segments.length > 1,
  459. originalText: text
  460. });
  461. totalDuration += duration;
  462. console.log(`${i + 1} 段音频请求成功,时长: ${duration}`);
  463. } else {
  464. console.error(`${i + 1} 段音频请求失败:`, radioRes);
  465. // 即使某段失败,也继续处理其他段
  466. audioSegments.push({
  467. url: null,
  468. text: segment.text,
  469. duration: 0,
  470. startIndex: segment.startIndex,
  471. endIndex: segment.endIndex,
  472. segmentIndex: i,
  473. error: true,
  474. isSegmented: segments.length > 1,
  475. originalText: text
  476. });
  477. }
  478. } catch (error) {
  479. console.error(`${i + 1} 段音频请求异常:`, error);
  480. audioSegments.push({
  481. url: null,
  482. text: segment.text,
  483. duration: 0,
  484. startIndex: segment.startIndex,
  485. endIndex: segment.endIndex,
  486. segmentIndex: i,
  487. error: true,
  488. isSegmented: segments.length > 1,
  489. originalText: text
  490. });
  491. }
  492. // 每个请求之间间隔200ms,避免请求过于频繁
  493. if (i < segments.length - 1) {
  494. await new Promise(resolve => setTimeout(resolve, 200));
  495. }
  496. }
  497. console.log(`分批次音频请求完成,成功 ${audioSegments.filter(s => !s.error).length}/${segments.length}`);
  498. return {
  499. audioSegments: audioSegments,
  500. totalDuration: totalDuration,
  501. originalText: text
  502. };
  503. },
  504. // 获取当前页面的音频内容
  505. async getCurrentPageAudio(autoPlay = false) {
  506. // 🎯 确保音色ID已加载完成后再获取音频
  507. if (!this.localVoiceId || this.localVoiceId === '' || this.localVoiceId === null || this.localVoiceId === undefined) {
  508. console.log('⚠️ 音色ID未加载,无法获取音频');
  509. console.log(' localVoiceId:', this.localVoiceId);
  510. console.log(' 类型:', typeof this.localVoiceId);
  511. // 设置加载失败状态
  512. this.isAudioLoading = false;
  513. this.audioLoadFailed = true;
  514. this.hasAudioData = false;
  515. // 通知父组件音频状态变化
  516. this.$emit('audio-state-change', {
  517. hasAudioData: false,
  518. isLoading: false,
  519. currentHighlightIndex: -1
  520. });
  521. uni.showToast({
  522. title: '音色未加载,请稍后重试',
  523. icon: 'none',
  524. duration: 2000
  525. });
  526. return;
  527. }
  528. console.log('✅ 音色ID验证通过:', this.localVoiceId);
  529. // 检查是否正在页面切换中,如果是则不加载音频
  530. if (this.isPageChanging) {
  531. console.log('页面切换中,跳过音频加载');
  532. return;
  533. }
  534. // 检查是否需要加载音频
  535. if (!this.shouldLoadAudio) {
  536. console.log('当前页面不需要加载音频');
  537. // 清空音频状态
  538. this.currentPageAudios = [];
  539. this.hasAudioData = false;
  540. this.isAudioLoading = false;
  541. this.audioLoadFailed = false;
  542. this.currentAudioIndex = 0;
  543. this.currentTime = 0;
  544. this.totalTime = 0;
  545. this.currentHighlightIndex = -1;
  546. // 通知父组件音频状态变化
  547. this.$emit('audio-state-change', {
  548. hasAudioData: false,
  549. isLoading: false,
  550. currentHighlightIndex: -1
  551. });
  552. return;
  553. }
  554. // 检查会员限制
  555. if (this.isAudioDisabled) {
  556. return;
  557. }
  558. // 检查是否已经在加载中,防止重复加载(音色切换时除外)
  559. if (this.isAudioLoading && !this.isVoiceChanging) {
  560. console.log('音频已在加载中,跳过重复加载');
  561. return;
  562. }
  563. // 检查缓存中是否已有当前页面的音频数据
  564. const cacheKey = `${this.courseId}_${this.currentPage}_${this.localVoiceId}`;
  565. if (this.audioCache[cacheKey]) {
  566. console.log('从缓存加载音频数据:', cacheKey);
  567. // 从缓存加载音频数据
  568. this.currentPageAudios = this.audioCache[cacheKey].audios;
  569. this.totalTime = this.audioCache[cacheKey].totalDuration;
  570. this.currentAudioIndex = 0;
  571. this.isPlaying = false;
  572. this.currentTime = 0;
  573. this.hasAudioData = true;
  574. this.isAudioLoading = false;
  575. // 如果是课程切换后的自动加载,清除切换标识
  576. if (this.isJustSwitchedCourse) {
  577. this.isJustSwitchedCourse = false;
  578. }
  579. // 通知父组件音频状态变化
  580. this.$emit('audio-state-change', {
  581. hasAudioData: this.hasAudioData,
  582. isLoading: this.isAudioLoading,
  583. currentHighlightIndex: this.currentHighlightIndex
  584. });
  585. return;
  586. }
  587. // 开始加载状态
  588. this.isAudioLoading = true;
  589. this.hasAudioData = false;
  590. // 重置请求取消标识并生成新的请求ID
  591. this.shouldCancelRequest = false;
  592. this.currentRequestId = Date.now() + '_' + Math.random().toString(36).substr(2, 9);
  593. console.log('开始新的音频请求,ID:', this.currentRequestId);
  594. // 清空当前页面音频数组
  595. this.currentPageAudios = [];
  596. this.currentAudioIndex = 0;
  597. this.isPlaying = false;
  598. this.currentTime = 0;
  599. this.totalTime = 0;
  600. // 通知父组件开始加载
  601. this.$emit('audio-state-change', {
  602. hasAudioData: this.hasAudioData,
  603. isLoading: this.isAudioLoading,
  604. currentHighlightIndex: this.currentHighlightIndex
  605. });
  606. try {
  607. // 对着当前页面的每一个[]元素进行切割 如果是文本text类型则进行音频请求
  608. const currentPageData = this.bookPages[this.currentPage - 1];
  609. if (currentPageData) {
  610. // 收集所有text类型的元素
  611. const textItems = currentPageData.filter(item => item.type === 'text');
  612. if (textItems.length > 0) {
  613. let firstAudioPlayed = false; // 标记是否已播放第一个音频
  614. let loadedAudiosCount = 0; // 已加载的音频数量
  615. // 逐个处理文本项,支持长文本分割
  616. for (let index = 0; index < textItems.length; index++) {
  617. const item = textItems[index];
  618. try {
  619. console.log(`处理第 ${index + 1}/${textItems.length} 个文本项,长度: ${item.content.length}`);
  620. // 使用分批次请求音频
  621. console.log('🎵 请求音频 - 当前音色ID:', this.localVoiceId);
  622. const batchResult = await this.requestAudioInBatches(item.content, this.localVoiceId);
  623. // 检查请求是否被取消
  624. if (batchResult === null) {
  625. console.log('音频请求被取消,停止处理');
  626. return;
  627. }
  628. if (batchResult.audioSegments.length > 0) {
  629. // 同时保存到原始数据中以保持兼容性(使用第一段的URL)
  630. const firstValidSegment = batchResult.audioSegments.find(seg => !seg.error);
  631. if (firstValidSegment) {
  632. item.audioUrl = firstValidSegment.url;
  633. }
  634. // 将所有音频段添加到音频数组
  635. for (const segment of batchResult.audioSegments) {
  636. if (!segment.error) {
  637. const audioData = {
  638. url: segment.url,
  639. text: segment.text,
  640. duration: segment.duration,
  641. startIndex: segment.startIndex,
  642. endIndex: segment.endIndex,
  643. segmentIndex: segment.segmentIndex,
  644. originalTextIndex: index, // 标记属于哪个原始文本项
  645. isSegmented: batchResult.audioSegments.length > 1 // 标记是否为分段音频
  646. };
  647. this.currentPageAudios.push(audioData);
  648. loadedAudiosCount++;
  649. }
  650. }
  651. // 如果是第一个音频,立即开始播放
  652. if (!firstAudioPlayed && this.currentPageAudios.length > 0) {
  653. firstAudioPlayed = true;
  654. this.hasAudioData = true;
  655. this.currentAudioIndex = 0;
  656. console.log(`第一个音频时长: ${this.currentPageAudios[0].duration}`);
  657. // 通知父组件有音频数据了,但仍在加载中
  658. this.$emit('audio-state-change', {
  659. hasAudioData: this.hasAudioData,
  660. isLoading: this.isAudioLoading, // 保持加载状态
  661. currentHighlightIndex: this.currentHighlightIndex
  662. });
  663. // 立即创建音频实例并开始播放
  664. console.log('创建第一个音频实例并开始播放');
  665. this.createAudioInstance();
  666. // 等待音频实例准备好后开始播放(仅在非音色切换时或明确要求自动播放时)
  667. setTimeout(() => {
  668. if (this.currentAudio && !this.isPlaying && (!this.isVoiceChanging || autoPlay)) {
  669. if (autoPlay || !this.isVoiceChanging) {
  670. console.log('自动播放第一个音频,autoPlay:', autoPlay);
  671. this.currentAudio.play();
  672. // isPlaying状态会在onPlay事件中自动设置
  673. this.updateHighlightIndex();
  674. }
  675. }
  676. }, 100);
  677. }
  678. console.log(`文本项 ${index + 1} 处理完成,获得 ${batchResult.audioSegments.filter(s => !s.error).length} 个音频段`);
  679. } else {
  680. console.error(`文本项 ${index + 1} 音频请求全部失败`);
  681. }
  682. } catch (error) {
  683. console.error(`文本项 ${index + 1} 处理异常:`, error);
  684. }
  685. }
  686. console.log('所有音频请求完成,共获取', this.currentPageAudios.length, '个音频');
  687. // 如果有音频,重新计算精确的总时长
  688. if (this.currentPageAudios.length > 0) {
  689. await this.calculateTotalDuration();
  690. // 将音频数据保存到缓存中
  691. const cacheKey = `${this.courseId}_${this.currentPage}_${this.localVoiceId}`;
  692. this.audioCache[cacheKey] = {
  693. audios: [...this.currentPageAudios], // 深拷贝音频数组
  694. totalDuration: this.totalTime,
  695. voiceId: this.localVoiceId, // 保存音色ID用于验证
  696. timestamp: Date.now() // 保存时间戳
  697. };
  698. console.log('音频数据已缓存:', cacheKey, this.audioCache[cacheKey]);
  699. // 限制缓存大小
  700. this.limitCacheSize(10);
  701. }
  702. }
  703. }
  704. // 结束加载状态
  705. this.isAudioLoading = false;
  706. this.isVoiceChanging = false; // 清除音色切换加载状态
  707. // 如果是课程切换后的自动加载,清除切换标识
  708. if (this.isJustSwitchedCourse) {
  709. this.isJustSwitchedCourse = false;
  710. }
  711. // 设置音频数据状态和失败状态
  712. this.hasAudioData = this.currentPageAudios.length > 0;
  713. this.audioLoadFailed = !this.hasAudioData && this.shouldLoadAudio; // 如果需要音频但没有音频数据,则认为获取失败
  714. // 通知父组件音频状态变化
  715. this.$emit('audio-state-change', {
  716. hasAudioData: this.hasAudioData,
  717. isLoading: this.isAudioLoading,
  718. audioLoadFailed: this.audioLoadFailed,
  719. currentHighlightIndex: this.currentHighlightIndex
  720. });
  721. } catch (error) {
  722. console.error('getCurrentPageAudio 方法执行异常:', error);
  723. // 确保在异常情况下重置加载状态
  724. this.isAudioLoading = false;
  725. this.isVoiceChanging = false;
  726. this.audioLoadFailed = true;
  727. this.hasAudioData = false;
  728. // 通知父组件音频加载失败
  729. this.$emit('audio-state-change', {
  730. hasAudioData: false,
  731. isLoading: false,
  732. audioLoadFailed: true,
  733. currentHighlightIndex: this.currentHighlightIndex
  734. });
  735. // 显示错误提示
  736. uni.showToast({
  737. title: '音频加载失败,请重试',
  738. icon: 'none',
  739. duration: 2000
  740. });
  741. }
  742. },
  743. // 重新获取音频
  744. retryGetAudio() {
  745. console.log('用户点击重新获取音频');
  746. // 检查是否需要加载音频
  747. if (!this.shouldLoadAudio) {
  748. console.log('当前页面不支持音频功能');
  749. return;
  750. }
  751. // 重置失败状态
  752. this.audioLoadFailed = false;
  753. // 清除当前页面的音频缓存
  754. const pageKey = `${this.courseId}_${this.currentPage}_${this.localVoiceId}`;
  755. if (this.audioCache[pageKey]) {
  756. delete this.audioCache[pageKey];
  757. console.log('清除失败页面的音频缓存:', pageKey);
  758. }
  759. // 重新获取音频
  760. this.getCurrentPageAudio();
  761. },
  762. // 重置音频状态
  763. resetAudioState() {
  764. // 取消当前正在进行的音频请求
  765. this.shouldCancelRequest = true;
  766. console.log('页面切换:标记取消当前音频请求');
  767. // 立即停止并销毁当前音频实例,防止穿音
  768. if (this.currentAudio) {
  769. if (this.isPlaying) {
  770. this.currentAudio.pause();
  771. console.log('页面切换:立即停止音频播放');
  772. }
  773. this.currentAudio.destroy();
  774. this.currentAudio = null;
  775. console.log('页面切换:销毁音频实例');
  776. }
  777. // 重置播放状态
  778. this.currentAudioIndex = 0;
  779. this.isPlaying = false;
  780. this.currentTime = 0;
  781. this.totalTime = 0;
  782. this.sliderValue = 0;
  783. this.isAudioLoading = false;
  784. this.audioLoadFailed = false;
  785. this.currentHighlightIndex = -1;
  786. this.playSpeed = 1.0;
  787. // 如果不需要加载音频,直接重置为初始状态
  788. if (!this.shouldLoadAudio) {
  789. console.log('不需要音频的页面:重置音频状态');
  790. this.currentPageAudios = [];
  791. this.totalTime = 0;
  792. this.hasAudioData = false;
  793. } else {
  794. // 文本页面:检查是否有缓存的音频数据
  795. const pageKey = `${this.courseId}_${this.currentPage}_${this.localVoiceId}`;
  796. const cachedAudio = this.audioCache[pageKey];
  797. if (cachedAudio && cachedAudio.audios && cachedAudio.audios.length > 0) {
  798. // 有缓存:直接显示控制栏
  799. this.currentPageAudios = cachedAudio.audios;
  800. this.totalTime = cachedAudio.totalDuration || 0;
  801. this.hasAudioData = true;
  802. console.log('检测到缓存音频,显示控制栏');
  803. } else {
  804. // 没有缓存:重置为初始状态,但不立即加载(由checkAndLoadPreloadedAudio处理)
  805. this.currentPageAudios = [];
  806. this.totalTime = 0;
  807. this.hasAudioData = false;
  808. console.log('没有缓存音频,重置状态');
  809. }
  810. }
  811. // 通知父组件音频状态变化
  812. this.$emit('audio-state-change', {
  813. hasAudioData: this.hasAudioData,
  814. isLoading: this.isAudioLoading,
  815. currentHighlightIndex: this.currentHighlightIndex
  816. });
  817. },
  818. // 加载缓存的音频数据并显示播放控制栏
  819. loadCachedAudioData() {
  820. const cacheKey = `${this.courseId}_${this.currentPage}_${this.localVoiceId}`;
  821. const cachedData = this.audioCache[cacheKey];
  822. // 严格验证缓存数据
  823. if (!cachedData || !cachedData.audios || cachedData.audios.length === 0) {
  824. console.warn('缓存数据不存在或为空:', cacheKey);
  825. uni.showToast({
  826. title: '缓存音频数据不存在',
  827. icon: 'none'
  828. });
  829. return;
  830. }
  831. // 检查音色ID是否匹配
  832. if (cachedData.voiceId && cachedData.voiceId !== this.localVoiceId) {
  833. console.warn('缓存音色不匹配:', cachedData.voiceId, '!=', this.localVoiceId);
  834. uni.showToast({
  835. title: '音色已切换,请重新获取音频',
  836. icon: 'none'
  837. });
  838. return;
  839. }
  840. // 检查音频URL是否有效
  841. const firstAudio = cachedData.audios[0];
  842. if (!firstAudio || !firstAudio.url) {
  843. console.warn('缓存音频URL无效');
  844. uni.showToast({
  845. title: '缓存音频数据损坏',
  846. icon: 'none'
  847. });
  848. return;
  849. }
  850. console.log('手动加载缓存音频数据:', cacheKey);
  851. // 从缓存加载音频数据
  852. this.currentPageAudios = cachedData.audios;
  853. this.totalTime = cachedData.totalDuration || 0;
  854. this.currentAudioIndex = 0;
  855. this.isPlaying = false;
  856. this.currentTime = 0;
  857. this.hasAudioData = true;
  858. this.isAudioLoading = false;
  859. this.audioLoadFailed = false;
  860. this.currentHighlightIndex = -1;
  861. // 通知父组件音频状态变化
  862. this.$emit('audio-state-change', {
  863. hasAudioData: this.hasAudioData,
  864. isLoading: this.isAudioLoading,
  865. currentHighlightIndex: this.currentHighlightIndex
  866. });
  867. console.log('缓存音频加载完成,播放控制栏已显示');
  868. },
  869. // 手动获取音频
  870. async handleGetAudio() {
  871. // 检查会员限制
  872. if (this.isAudioDisabled) {
  873. return;
  874. }
  875. // 检查是否有音色ID
  876. if (!this.localVoiceId) {
  877. uni.showToast({
  878. title: '音色未加载,请稍后重试',
  879. icon: 'none'
  880. });
  881. return;
  882. }
  883. // 检查当前页面是否支持音频播放
  884. if (!this.shouldLoadAudio) {
  885. uni.showToast({
  886. title: '当前页面不支持音频播放',
  887. icon: 'none'
  888. });
  889. return;
  890. }
  891. // 检查是否正在加载
  892. if (this.isAudioLoading) {
  893. return;
  894. }
  895. // 调用获取音频方法
  896. await this.getCurrentPageAudio();
  897. },
  898. // 计算音频总时长
  899. async calculateTotalDuration() {
  900. let totalDuration = 0;
  901. for (let i = 0; i < this.currentPageAudios.length; i++) {
  902. const audio = this.currentPageAudios[i];
  903. // 使用API返回的准确时长信息
  904. if (audio.duration && audio.duration > 0) {
  905. console.log(`使用API返回的准确时长 ${i + 1}:`, audio.duration, '秒');
  906. totalDuration += audio.duration;
  907. } else {
  908. // 如果没有时长信息,使用文字长度估算(备用方案)
  909. const textLength = audio.text.length;
  910. // 假设较快语速每分钟约300个字符,即每秒约5个字符
  911. const estimatedDuration = Math.max(1, textLength / 5);
  912. audio.duration = estimatedDuration;
  913. totalDuration += estimatedDuration;
  914. console.log(`备用估算音频时长 ${i + 1}:`, estimatedDuration, '秒 (文字长度:', textLength, ')');
  915. }
  916. }
  917. this.totalTime = totalDuration;
  918. console.log('音频总时长:', totalDuration, '秒');
  919. },
  920. // 获取音频时长
  921. getAudioDuration(audioUrl) {
  922. return new Promise((resolve, reject) => {
  923. const audio = uni.createInnerAudioContext();
  924. audio.src = audioUrl;
  925. let resolved = false;
  926. // 监听音频加载完成事件
  927. audio.onCanplay(() => {
  928. console.log('音频可以播放,duration:', audio.duration);
  929. if (!resolved && audio.duration && audio.duration > 0) {
  930. resolved = true;
  931. resolve(audio.duration);
  932. audio.destroy();
  933. }
  934. });
  935. // 监听音频元数据加载完成事件
  936. audio.onLoadedmetadata = () => {
  937. console.log('音频元数据加载完成,duration:', audio.duration);
  938. if (!resolved && audio.duration && audio.duration > 0) {
  939. resolved = true;
  940. resolve(audio.duration);
  941. audio.destroy();
  942. }
  943. };
  944. // 监听音频时长更新事件
  945. audio.onDurationChange = () => {
  946. console.log('音频时长更新,duration:', audio.duration);
  947. if (!resolved && audio.duration && audio.duration > 0) {
  948. resolved = true;
  949. resolve(audio.duration);
  950. audio.destroy();
  951. }
  952. };
  953. // 移除onPlay監聽器,避免意外播放
  954. audio.onError((error) => {
  955. console.error('音频加载失败:', error);
  956. if (!resolved) {
  957. resolved = true;
  958. reject(error);
  959. audio.destroy();
  960. }
  961. });
  962. // 設置較長的超時時間,但不播放音頻
  963. setTimeout(() => {
  964. if (!resolved) {
  965. console.log('無法獲取音頻時長,使用默認值');
  966. resolved = true;
  967. reject(new Error('無法獲取音頻時長'));
  968. audio.destroy();
  969. }
  970. }, 1000);
  971. // 最終超時處理
  972. setTimeout(() => {
  973. if (!resolved) {
  974. console.warn('獲取音頻時長超時,使用默認值');
  975. resolved = true;
  976. reject(new Error('获取音频时长超时'));
  977. audio.destroy();
  978. }
  979. }, 5000);
  980. });
  981. },
  982. // 音频控制方法
  983. togglePlay() {
  984. // 检查会员限制
  985. if (this.isAudioDisabled) {
  986. return;
  987. }
  988. if (this.currentPageAudios.length === 0) {
  989. uni.showToast({
  990. title: '当前页面没有音频内容',
  991. icon: 'none'
  992. });
  993. return;
  994. }
  995. if (this.isPlaying) {
  996. this.pauseAudio();
  997. } else {
  998. this.playAudio();
  999. }
  1000. },
  1001. // 播放音频
  1002. playAudio() {
  1003. // 检查会员限制
  1004. if (this.isAudioDisabled) {
  1005. return;
  1006. }
  1007. if (this.currentPageAudios.length === 0) return;
  1008. // 🎯 全局音频管理:停止单词音频播放,确保只有一个音频播放
  1009. this.stopWordAudio();
  1010. // 如果没有当前音频实例或者需要切换音频
  1011. if (!this.currentAudio || this.currentAudio.src !== this.currentPageAudios[this.currentAudioIndex].url) {
  1012. this.createAudioInstance();
  1013. // 创建实例后稍等一下再播放,确保音频准备就绪
  1014. setTimeout(() => {
  1015. if (this.currentAudio) {
  1016. this.currentAudio.play();
  1017. // isPlaying状态会在onPlay事件中自动设置
  1018. this.updateHighlightIndex();
  1019. }
  1020. }, 50);
  1021. } else {
  1022. // 音频实例已存在,直接播放
  1023. this.currentAudio.play();
  1024. // isPlaying状态会在onPlay事件中自动设置
  1025. this.updateHighlightIndex();
  1026. // 确保倍速设置正确
  1027. setTimeout(() => {
  1028. this.applyPlaybackRate(this.currentAudio);
  1029. }, 100);
  1030. }
  1031. },
  1032. // 暂停音频
  1033. pauseAudio() {
  1034. if (this.currentAudio) {
  1035. this.currentAudio.pause();
  1036. }
  1037. this.isPlaying = false;
  1038. // 暂停时清除高亮
  1039. this.currentHighlightIndex = -1;
  1040. // 通知父组件高亮状态变化
  1041. this.emitHighlightChange(-1);
  1042. },
  1043. // 文本标准化函数 - 移除多余空格、标点符号等
  1044. normalizeText(text) {
  1045. if (!text || typeof text !== 'string') return '';
  1046. return text
  1047. .trim()
  1048. .replace(/\s+/g, ' ') // 将多个空格替换为单个空格
  1049. .replace(/[,。!?;:""''()【】《》]/g, '') // 移除中文标点
  1050. .replace(/[,.!?;:"'()\[\]<>]/g, '') // 移除英文标点
  1051. .toLowerCase(); // 转为小写(对英文有效)
  1052. },
  1053. // 备用方案:使用 TTS API 实时生成并播放音频
  1054. async playTextWithTTS(textContent) {
  1055. try {
  1056. console.log('🔊 使用 TTS 实时生成音频:', textContent);
  1057. // 停止当前播放的音频
  1058. if (this.currentAudio) {
  1059. this.currentAudio.pause();
  1060. this.currentAudio.destroy();
  1061. this.currentAudio = null;
  1062. }
  1063. // 显示加载提示
  1064. uni.showLoading({
  1065. title: '正在生成音频...'
  1066. });
  1067. // 调用 TTS API
  1068. const audioRes = await this.$api.music.textToVoice({
  1069. text: textContent,
  1070. voiceType: this.voiceId || 1 // 使用当前语音类型,默认为1
  1071. });
  1072. uni.hideLoading();
  1073. console.log('🔊 TTS API 响应:', audioRes);
  1074. if (audioRes && audioRes.result && audioRes.result.url) {
  1075. const audioUrl = audioRes.result.url;
  1076. // 创建并播放音频
  1077. const audio = uni.createInnerAudioContext();
  1078. audio.src = audioUrl;
  1079. audio.onPlay(() => {
  1080. console.log('🔊 TTS 音频开始播放');
  1081. this.isPlaying = true;
  1082. });
  1083. audio.onEnded(() => {
  1084. console.log('🔊 TTS 音频播放完成');
  1085. this.isPlaying = false;
  1086. audio.destroy();
  1087. if (this.currentAudio === audio) {
  1088. this.currentAudio = null;
  1089. }
  1090. });
  1091. audio.onError((error) => {
  1092. console.error('🔊 TTS 音频播放失败:', error);
  1093. this.isPlaying = false;
  1094. audio.destroy();
  1095. if (this.currentAudio === audio) {
  1096. this.currentAudio = null;
  1097. }
  1098. uni.showToast({
  1099. title: '音频播放失败',
  1100. icon: 'none'
  1101. });
  1102. });
  1103. // 保存当前音频实例并播放
  1104. this.currentAudio = audio;
  1105. audio.play();
  1106. console.log('✅ TTS 音频播放成功');
  1107. return true;
  1108. } else {
  1109. console.error('❌ TTS API 请求失败:', audioRes);
  1110. uni.showToast({
  1111. title: '音频生成失败',
  1112. icon: 'none'
  1113. });
  1114. return false;
  1115. }
  1116. } catch (error) {
  1117. uni.hideLoading();
  1118. console.error('❌ TTS 音频生成异常:', error);
  1119. uni.showToast({
  1120. title: '音频生成失败',
  1121. icon: 'none'
  1122. });
  1123. return false;
  1124. }
  1125. },
  1126. // 播放指定的音频段落(通过文本内容匹配)
  1127. playSpecificAudio(textContent) {
  1128. console.log('🎯 尝试播放指定音频段落:', textContent);
  1129. console.log('📊 当前页面音频数组长度:', this.currentPageAudios.length);
  1130. console.log('📋 音频加载状态:', this.isAudioLoading);
  1131. // 检查单词音频播放状态
  1132. console.log('🎵 检查单词音频播放状态:', this.isWordAudioPlaying);
  1133. if (this.isWordAudioPlaying) {
  1134. console.log('⚠️ 单词音频正在播放,阻止句子音频播放');
  1135. uni.showToast({
  1136. title: '请等待单词音频播放完成',
  1137. icon: 'none'
  1138. });
  1139. return false;
  1140. }
  1141. // 检查textContent是否有效
  1142. if (!textContent || typeof textContent !== 'string') {
  1143. console.error('❌ 无效的文本内容:', textContent);
  1144. uni.showToast({
  1145. title: '无效的文本内容',
  1146. icon: 'none'
  1147. });
  1148. return false;
  1149. }
  1150. // 检查音频数据是否已加载
  1151. if (this.currentPageAudios.length === 0) {
  1152. console.warn('⚠️ 当前页面音频数据为空,可能还在加载中');
  1153. uni.showToast({
  1154. title: '音频正在加载中,请稍后再试',
  1155. icon: 'none'
  1156. });
  1157. return false;
  1158. }
  1159. // 标准化目标文本
  1160. const normalizedTarget = this.normalizeText(textContent);
  1161. console.log('🔍 标准化后的目标文本:', normalizedTarget);
  1162. // 打印所有音频文本用于调试
  1163. console.log('📝 当前页面所有音频文本:');
  1164. this.currentPageAudios.forEach((audio, index) => {
  1165. console.log(` [${index}] 原始文本: "${audio.text}"`);
  1166. console.log(` [${index}] 标准化文本: "${this.normalizeText(audio.text)}"`);
  1167. if (audio.originalText) {
  1168. console.log(` [${index}] 原始完整文本: "${audio.originalText}"`);
  1169. }
  1170. });
  1171. let audioIndex = -1;
  1172. // 第一步:精确匹配(标准化后)
  1173. audioIndex = this.currentPageAudios.findIndex(audio => {
  1174. if (!audio.text) return false;
  1175. const normalizedAudio = this.normalizeText(audio.text);
  1176. return normalizedAudio === normalizedTarget;
  1177. });
  1178. if (audioIndex !== -1) {
  1179. console.log('✅ 精确匹配成功,索引:', audioIndex);
  1180. } else {
  1181. console.log('❌ 精确匹配失败,尝试模糊匹配');
  1182. // 第二步:包含匹配
  1183. audioIndex = this.currentPageAudios.findIndex(audio => {
  1184. if (!audio.text) return false;
  1185. const normalizedAudio = this.normalizeText(audio.text);
  1186. // 双向包含检查
  1187. return normalizedAudio.includes(normalizedTarget) || normalizedTarget.includes(normalizedAudio);
  1188. });
  1189. if (audioIndex !== -1) {
  1190. console.log('✅ 包含匹配成功,索引:', audioIndex);
  1191. } else {
  1192. console.log('❌ 包含匹配失败,尝试分段音频匹配');
  1193. // 第三步:分段音频匹配
  1194. audioIndex = this.currentPageAudios.findIndex(audio => {
  1195. if (!audio.text) return false;
  1196. // 检查是否为分段音频,且原始文本匹配
  1197. if (audio.isSegmented && audio.originalText) {
  1198. const normalizedOriginal = this.normalizeText(audio.originalText);
  1199. return normalizedOriginal === normalizedTarget ||
  1200. normalizedOriginal.includes(normalizedTarget) ||
  1201. normalizedTarget.includes(normalizedOriginal);
  1202. }
  1203. return false;
  1204. });
  1205. if (audioIndex !== -1) {
  1206. console.log('✅ 分段音频匹配成功,索引:', audioIndex);
  1207. } else {
  1208. console.log('❌ 所有匹配方式都失败');
  1209. // 第四步:句子分割匹配(针对长句子)
  1210. console.log('❌ 尝试句子分割匹配...');
  1211. // 将目标句子按标点符号分割
  1212. const targetSentences = normalizedTarget.split(/[,。!?;:,!?;:]/).filter(s => s.trim().length > 0);
  1213. console.log('🔪 目标句子分割结果:', targetSentences);
  1214. if (targetSentences.length > 1) {
  1215. // 尝试匹配分割后的句子片段
  1216. for (let i = 0; i < targetSentences.length; i++) {
  1217. const sentence = targetSentences[i].trim();
  1218. if (sentence.length < 3) continue; // 跳过太短的片段
  1219. audioIndex = this.currentPageAudios.findIndex(audio => {
  1220. if (!audio.text) return false;
  1221. const normalizedAudio = this.normalizeText(audio.text);
  1222. return normalizedAudio.includes(sentence) || sentence.includes(normalizedAudio);
  1223. });
  1224. if (audioIndex !== -1) {
  1225. console.log(`✅ 句子片段匹配成功,片段: "${sentence}", 索引: ${audioIndex}`);
  1226. break;
  1227. }
  1228. }
  1229. }
  1230. if (audioIndex === -1) {
  1231. console.log('❌ 句子分割匹配失败,尝试关键词匹配...');
  1232. // 第五步:关键词匹配(提取关键词进行匹配)
  1233. const keywords = normalizedTarget.split(/\s+/).filter(word => word.length > 2);
  1234. console.log('🔑 提取的关键词:', keywords);
  1235. let bestKeywordMatch = -1;
  1236. let bestKeywordCount = 0;
  1237. this.currentPageAudios.forEach((audio, index) => {
  1238. if (!audio.text) return;
  1239. const normalizedAudio = this.normalizeText(audio.text);
  1240. // 计算匹配的关键词数量
  1241. const matchedKeywords = keywords.filter(keyword => normalizedAudio.includes(keyword));
  1242. const matchCount = matchedKeywords.length;
  1243. if (matchCount > bestKeywordCount && matchCount >= Math.min(2, keywords.length)) {
  1244. bestKeywordCount = matchCount;
  1245. bestKeywordMatch = index;
  1246. console.log(` [${index}] 关键词匹配: ${matchCount}/${keywords.length}, 匹配词: [${matchedKeywords.join(', ')}]`);
  1247. }
  1248. });
  1249. if (bestKeywordMatch !== -1) {
  1250. audioIndex = bestKeywordMatch;
  1251. console.log(`✅ 关键词匹配成功,索引: ${audioIndex}, 匹配数: ${bestKeywordCount}/${keywords.length}`);
  1252. } else {
  1253. console.log('❌ 关键词匹配失败,尝试相似度匹配...');
  1254. // 第六步:相似度匹配(最后的尝试)
  1255. let bestMatch = -1;
  1256. let bestSimilarity = 0;
  1257. this.currentPageAudios.forEach((audio, index) => {
  1258. if (!audio.text) return;
  1259. const normalizedAudio = this.normalizeText(audio.text);
  1260. // 计算简单的相似度(共同字符数 / 较长文本长度)
  1261. const commonChars = [...normalizedTarget].filter(char => normalizedAudio.includes(char)).length;
  1262. const maxLength = Math.max(normalizedTarget.length, normalizedAudio.length);
  1263. const similarity = maxLength > 0 ? commonChars / maxLength : 0;
  1264. console.log(` [${index}] 相似度: ${similarity.toFixed(2)}, 文本: "${audio.text}"`);
  1265. if (similarity > bestSimilarity && similarity > 0.5) { // 降低相似度阈值到50%
  1266. bestSimilarity = similarity;
  1267. bestMatch = index;
  1268. }
  1269. });
  1270. if (bestMatch !== -1) {
  1271. audioIndex = bestMatch;
  1272. console.log(`✅ 相似度匹配成功,索引: ${audioIndex}, 相似度: ${bestSimilarity.toFixed(2)}`);
  1273. }
  1274. }
  1275. }
  1276. }
  1277. }
  1278. }
  1279. if (audioIndex !== -1) {
  1280. console.log('🎵 找到匹配的音频,开始播放,索引:', audioIndex);
  1281. console.log('🎵 匹配的音频文本:', this.currentPageAudios[audioIndex].text);
  1282. // 停止当前播放的音频
  1283. if (this.currentAudio) {
  1284. this.currentAudio.pause();
  1285. this.currentAudio.destroy();
  1286. this.currentAudio = null;
  1287. }
  1288. // 设置新的音频索引
  1289. this.currentAudioIndex = audioIndex;
  1290. // 重置播放状态
  1291. this.isPlaying = false;
  1292. this.currentTime = 0;
  1293. this.sliderValue = 0;
  1294. // 更新高亮索引
  1295. this.currentHighlightIndex = audioIndex;
  1296. this.emitHighlightChange(audioIndex);
  1297. // 创建新的音频实例并播放
  1298. this.createAudioInstance();
  1299. // 稍等一下再播放,确保音频准备就绪
  1300. setTimeout(() => {
  1301. if (this.currentAudio) {
  1302. this.currentAudio.play();
  1303. console.log('🎵 开始播放指定音频段落');
  1304. } else {
  1305. console.error('❌ 音频实例创建失败');
  1306. }
  1307. }, 100);
  1308. return true; // 成功找到并播放
  1309. } else {
  1310. console.error('❌ 未找到匹配的音频段落:', textContent);
  1311. console.log('💡 尝试最后的匹配策略:首字符匹配');
  1312. // 最后的尝试:首字符匹配(针对划线重点等特殊情况)
  1313. if (normalizedTarget.length > 5) {
  1314. const firstChars = normalizedTarget.substring(0, Math.min(10, normalizedTarget.length));
  1315. console.log('🔍 尝试首字符匹配:', firstChars);
  1316. audioIndex = this.currentPageAudios.findIndex(audio => {
  1317. if (!audio.text) return false;
  1318. const normalizedAudio = this.normalizeText(audio.text);
  1319. return normalizedAudio.startsWith(firstChars) || firstChars.startsWith(normalizedAudio.substring(0, Math.min(10, normalizedAudio.length)));
  1320. });
  1321. if (audioIndex !== -1) {
  1322. console.log('✅ 首字符匹配成功,索引:', audioIndex);
  1323. // 停止当前播放的音频
  1324. if (this.currentAudio) {
  1325. this.currentAudio.pause();
  1326. this.currentAudio.destroy();
  1327. this.currentAudio = null;
  1328. }
  1329. // 设置新的音频索引
  1330. this.currentAudioIndex = audioIndex;
  1331. // 重置播放状态
  1332. this.isPlaying = false;
  1333. this.currentTime = 0;
  1334. this.sliderValue = 0;
  1335. // 更新高亮索引
  1336. this.currentHighlightIndex = audioIndex;
  1337. this.emitHighlightChange(audioIndex);
  1338. // 创建新的音频实例并播放
  1339. this.createAudioInstance();
  1340. // 稍等一下再播放,确保音频准备就绪
  1341. setTimeout(() => {
  1342. if (this.currentAudio) {
  1343. this.currentAudio.play();
  1344. console.log('🎵 开始播放首字符匹配的音频段落');
  1345. } else {
  1346. console.error('❌ 音频实例创建失败');
  1347. }
  1348. }, 100);
  1349. return true;
  1350. }
  1351. }
  1352. console.log('💡 所有匹配策略都失败,尝试使用备用方案:实时生成音频');
  1353. // 备用方案:使用 textToVoice API 实时生成音频
  1354. return this.playTextWithTTS(textContent);
  1355. }
  1356. },
  1357. // 创建音频实例
  1358. createAudioInstance() {
  1359. // 销毁之前的音频实例
  1360. if (this.currentAudio) {
  1361. this.currentAudio.pause();
  1362. this.currentAudio.destroy();
  1363. this.currentAudio = null;
  1364. }
  1365. // 重置播放状态
  1366. this.isPlaying = false;
  1367. // 優先使用微信原生API以支持playbackRate
  1368. let audio;
  1369. // if (typeof wx !== 'undefined' && wx.createInnerAudioContext) {
  1370. // console.log('使用微信原生音頻API');
  1371. // audio = wx.createInnerAudioContext();
  1372. // } else {
  1373. // // 在H5环境下,尝试使用原生HTML5 Audio API来支持倍速
  1374. // if (typeof window !== 'undefined' && window.Audio) {
  1375. // console.log('使用原生HTML5 Audio API以支持倍速');
  1376. audio = this.createHTML5Audio();
  1377. // } else {
  1378. // console.log('使用uni-app音頻API');
  1379. // audio = uni.createInnerAudioContext();
  1380. // }
  1381. // }
  1382. const audioUrl = this.currentPageAudios[this.currentAudioIndex].url;
  1383. audio.src = audioUrl;
  1384. console.log('🎵 创建音频实例 - 当前音色ID:', this.voiceId);
  1385. console.log('🎵 创建音频实例 - 音频URL:', audioUrl);
  1386. console.log('🎵 创建音频实例 - 音频索引:', this.currentAudioIndex);
  1387. console.log('🎵 创建音频实例 - 音频文本:', this.currentPageAudios[this.currentAudioIndex].text?.substring(0, 50) + '...');
  1388. // 在音頻可以播放時檢測playbackRate支持
  1389. audio.onCanplay(() => {
  1390. console.log('🎵 音頻可以播放事件觸發,開始檢測playbackRate支持');
  1391. console.log('🔍 音頻實例基本信息:', {
  1392. src: audio.src,
  1393. duration: audio.duration,
  1394. readyState: audio.readyState || '未知',
  1395. constructor: audio.constructor.name
  1396. });
  1397. this.checkPlaybackRateSupport(audio);
  1398. });
  1399. // 音频事件监听
  1400. audio.onPlay(() => {
  1401. console.log('🎵 音频开始播放');
  1402. this.isPlaying = true;
  1403. // 在播放开始时立即设置倍速
  1404. this.applyPlaybackRate(audio);
  1405. });
  1406. audio.onPause(() => {
  1407. console.log('⏸️ 音频暂停');
  1408. console.log('📊 暫停時倍速狀態:', {
  1409. 當前播放速度: audio.playbackRate + 'x',
  1410. 期望播放速度: this.playSpeed + 'x'
  1411. });
  1412. this.isPlaying = false;
  1413. });
  1414. audio.onTimeUpdate(() => {
  1415. this.updateCurrentTime();
  1416. // 定期检查倍速设置是否正确(每5秒检查一次)
  1417. if (Math.floor(audio.currentTime) % 5 === 0 && Math.floor(audio.currentTime) !== this.lastSpeedCheckTime) {
  1418. this.lastSpeedCheckTime = Math.floor(audio.currentTime);
  1419. const rateDifference = Math.abs(audio.playbackRate - this.playSpeed);
  1420. if (rateDifference > 0.01) {
  1421. console.log('⚠️ 检测到倍速偏差,重新应用倍速设置');
  1422. this.applyPlaybackRate(audio);
  1423. }
  1424. }
  1425. });
  1426. audio.onEnded(() => {
  1427. console.log('当前音频播放结束');
  1428. this.onAudioEnded();
  1429. });
  1430. audio.onError((error) => {
  1431. // 更精确的错误判断:只在用户主动播放时出现的错误才提示
  1432. if (audio.src && audio.src.trim() !== '' && this.isPlaying) {
  1433. console.error('音频播放错误:', error);
  1434. this.isPlaying = false;
  1435. uni.showToast({
  1436. title: '音频播放失败',
  1437. icon: 'none'
  1438. });
  1439. } else {
  1440. // 其他情况的错误(初始化、预加载等),只记录日志不提示用户
  1441. console.log('🔇 音频错误(已忽略):', {
  1442. hasSrc: !!audio.src,
  1443. isPlaying: this.isPlaying,
  1444. errorType: error.type || 'unknown'
  1445. });
  1446. }
  1447. });
  1448. this.currentAudio = audio;
  1449. },
  1450. // 更新当前播放时间
  1451. updateCurrentTime() {
  1452. if (!this.currentAudio) return;
  1453. let totalTime = 0;
  1454. // 计算之前音频的总时长
  1455. for (let i = 0; i < this.currentAudioIndex; i++) {
  1456. totalTime += this.currentPageAudios[i].duration;
  1457. }
  1458. // 加上当前音频的播放时间
  1459. totalTime += this.currentAudio.currentTime;
  1460. this.currentTime = totalTime;
  1461. // 如果不是正在拖動滑動條,則同步更新滑動條的值
  1462. if (!this.isDragging) {
  1463. this.sliderValue = this.currentTime;
  1464. }
  1465. // 更新当前高亮的文本索引
  1466. this.updateHighlightIndex();
  1467. },
  1468. // 更新高亮文本索引
  1469. updateHighlightIndex() {
  1470. if (!this.isPlaying || this.currentPageAudios.length === 0) {
  1471. this.currentHighlightIndex = -1;
  1472. this.emitHighlightChange(-1);
  1473. return;
  1474. }
  1475. // 获取当前播放的音频数据
  1476. const currentAudio = this.currentPageAudios[this.currentAudioIndex];
  1477. if (!currentAudio) {
  1478. this.currentHighlightIndex = -1;
  1479. this.emitHighlightChange(-1);
  1480. return;
  1481. }
  1482. // 如果是分段音频,需要计算正确的高亮索引
  1483. if (currentAudio.isSegmented && typeof currentAudio.originalTextIndex !== 'undefined') {
  1484. // 使用原始文本项的索引作为高亮索引
  1485. this.currentHighlightIndex = currentAudio.originalTextIndex;
  1486. console.log('分段音频高亮索引:', this.currentHighlightIndex, '原始文本索引:', currentAudio.originalTextIndex, '段索引:', currentAudio.segmentIndex);
  1487. } else {
  1488. // 非分段音频,使用音频索引
  1489. this.currentHighlightIndex = this.currentAudioIndex;
  1490. console.log('普通音频高亮索引:', this.currentHighlightIndex, '当前音频索引:', this.currentAudioIndex);
  1491. }
  1492. // 使用辅助方法发送高亮变化事件
  1493. this.emitHighlightChange(this.currentAudioIndex);
  1494. },
  1495. // 发送高亮变化事件的辅助方法
  1496. emitHighlightChange(highlightIndex = -1) {
  1497. if (highlightIndex === -1) {
  1498. // 清除高亮
  1499. this.$emit('highlight-change', -1);
  1500. return;
  1501. }
  1502. // 获取对应的音频数据
  1503. const audioData = this.currentPageAudios[highlightIndex];
  1504. if (!audioData) {
  1505. this.$emit('highlight-change', -1);
  1506. return;
  1507. }
  1508. // 发送详细的高亮信息
  1509. this.$emit('highlight-change', {
  1510. highlightIndex: audioData.originalTextIndex !== undefined ? audioData.originalTextIndex : highlightIndex,
  1511. isSegmented: audioData.isSegmented || false,
  1512. segmentIndex: audioData.segmentIndex || 0,
  1513. startIndex: audioData.startIndex || 0,
  1514. endIndex: audioData.endIndex || 0,
  1515. currentText: audioData.text || ''
  1516. });
  1517. },
  1518. // 音频播放结束处理
  1519. onAudioEnded() {
  1520. if (this.currentAudioIndex < this.currentPageAudios.length - 1) {
  1521. // 播放下一个音频
  1522. this.currentAudioIndex++;
  1523. this.playAudio();
  1524. } else {
  1525. // 所有音频播放完毕
  1526. if (this.isLoop) {
  1527. // 循环播放
  1528. this.currentAudioIndex = 0;
  1529. this.playAudio();
  1530. } else {
  1531. // 停止播放
  1532. this.isPlaying = false;
  1533. this.currentTime = this.totalTime;
  1534. this.currentHighlightIndex = -1;
  1535. this.$emit('highlight-change', -1);
  1536. }
  1537. }
  1538. },
  1539. toggleLoop() {
  1540. this.isLoop = !this.isLoop;
  1541. },
  1542. toggleSpeed() {
  1543. console.log('🎛️ 用户点击倍速切换按钮...');
  1544. // 简化检测:只在极少数情况下阻止倍速切换
  1545. console.log('🔍 检查倍速支持状态:', {
  1546. playbackRateSupported: this.playbackRateSupported,
  1547. 当前状态: this.playbackRateSupported ? '✅ 支持' : '⚠️ 受限'
  1548. });
  1549. // 只有在明确禁用的情况下才阻止(比如Android 4.x)
  1550. if (!this.playbackRateSupported) {
  1551. console.log('⚠️ 倍速功能受限,但仍尝试切换');
  1552. // 不再直接返回,而是继续尝试
  1553. }
  1554. const currentIndex = this.speedOptions.indexOf(this.playSpeed);
  1555. const nextIndex = (currentIndex + 1) % this.speedOptions.length;
  1556. const oldSpeed = this.playSpeed;
  1557. this.playSpeed = this.speedOptions[nextIndex];
  1558. console.log('⚡ 倍速切换详情:', {
  1559. 可用选项: this.speedOptions,
  1560. 当前索引: currentIndex,
  1561. 下一个索引: nextIndex,
  1562. 旧速度: oldSpeed + 'x',
  1563. 新速度: this.playSpeed + 'x',
  1564. 切换时间: new Date().toLocaleTimeString()
  1565. });
  1566. // 如果当前有音频在播放,更新播放速度
  1567. console.log('🎵 检查音频实例状态:', {
  1568. 音频实例存在: !!this.currentAudio,
  1569. 正在播放: this.isPlaying,
  1570. 音频src: this.currentAudio ? this.currentAudio.src : '无'
  1571. });
  1572. if (this.currentAudio) {
  1573. const wasPlaying = this.isPlaying;
  1574. const currentTime = this.currentAudio.currentTime;
  1575. console.log('🔧 准备更新音频播放速度:', {
  1576. 播放状态: wasPlaying ? '正在播放' : '已暂停',
  1577. 当前时间: currentTime + 's',
  1578. 目标速度: this.playSpeed + 'x'
  1579. });
  1580. // 使用统一的倍速设置方法
  1581. this.applyPlaybackRate(this.currentAudio);
  1582. // 如果正在播放,需要重启播放才能使播放速度生效
  1583. if (wasPlaying) {
  1584. console.log('🔄 重启播放以应用新速度...');
  1585. this.currentAudio.pause();
  1586. setTimeout(() => {
  1587. // 不使用seek方法,直接重新播放
  1588. this.currentAudio.play();
  1589. console.log('▶️ 播放已重启,新速度已生效');
  1590. }, 100);
  1591. }
  1592. console.log('📊 最终音频状态:', {
  1593. 播放速度: this.currentAudio.playbackRate + 'x',
  1594. 播放时间: this.currentAudio.currentTime + 's',
  1595. 播放状态: this.isPlaying ? '播放中' : '已暂停'
  1596. });
  1597. // 显示速度变更提示
  1598. uni.showToast({
  1599. title: `🎵 播放速度: ${this.playSpeed}x`,
  1600. icon: 'none',
  1601. duration: 1000
  1602. });
  1603. console.log('🎉 倍速切换完成!新速度:', this.playSpeed + 'x');
  1604. } else {
  1605. console.log('⚠️ 没有音频实例,仅更新速度设置');
  1606. uni.showToast({
  1607. title: `⚡ 速度设为: ${this.playSpeed}x`,
  1608. icon: 'none',
  1609. duration: 1000
  1610. });
  1611. }
  1612. },
  1613. // 滑動條值實時更新 (@input 事件)
  1614. onSliderInput(value) {
  1615. // 在拖動過程中實時更新顯示的時間,但不影響實際播放
  1616. if (this.isDragging) {
  1617. // 可以在這裡實時更新顯示時間,讓用戶看到拖動到的時間點
  1618. // 但不改變實際的 currentTime,避免影響播放邏輯
  1619. console.log('實時更新滑動條值:', value);
  1620. }
  1621. },
  1622. // 滑動條拖動過程中的處理 (@changing 事件)
  1623. onSliderChanging(value) {
  1624. // 第一次觸發 changing 事件時,暫停播放並標記為拖動狀態
  1625. if (!this.isDragging) {
  1626. if (this.isPlaying) {
  1627. this.pauseAudio();
  1628. console.log('開始拖動滑動條,暫停播放');
  1629. }
  1630. this.isDragging = true;
  1631. }
  1632. // 更新滑動條的值,但不改變實際播放位置
  1633. this.sliderValue = value;
  1634. console.log('拖動中,滑動條值:', value);
  1635. },
  1636. // 滑動條拖動結束的處理 (@change 事件)
  1637. onSliderChange(value) {
  1638. console.log('滑動條變化,跳轉到位置:', value, '是否為拖動:', this.isDragging);
  1639. // 如果不是拖動狀態(即單點),需要先暫停播放
  1640. if (!this.isDragging && this.isPlaying) {
  1641. this.pauseAudio();
  1642. console.log('單點滑動條,暫停播放');
  1643. }
  1644. // 重置拖動狀態
  1645. this.isDragging = false;
  1646. this.sliderValue = value;
  1647. // 跳轉到指定位置,但不自動恢復播放
  1648. this.seekToTime(value, false);
  1649. console.log('滑動條操作完成,保持暫停狀態,需要手動點擊播放');
  1650. },
  1651. // 跳轉到指定時間
  1652. seekToTime(targetTime, shouldResume = false) {
  1653. if (!this.currentPageAudios || this.currentPageAudios.length === 0) {
  1654. console.log('沒有音頻數據,無法跳轉');
  1655. return;
  1656. }
  1657. // 確保目標時間在有效範圍內
  1658. targetTime = Math.max(0, Math.min(targetTime, this.totalTime));
  1659. console.log('跳轉到時間:', targetTime, '秒', '總時長:', this.totalTime, '是否恢復播放:', shouldResume);
  1660. let accumulatedTime = 0;
  1661. let targetAudioIndex = -1;
  1662. let targetAudioTime = 0;
  1663. // 找到目標時間對應的音頻片段
  1664. for (let i = 0; i < this.currentPageAudios.length; i++) {
  1665. const audioDuration = this.currentPageAudios[i].duration || 0;
  1666. console.log(`音頻片段 ${i}: 時長=${audioDuration}, 累計時間=${accumulatedTime}, 範圍=[${accumulatedTime}, ${accumulatedTime + audioDuration}]`);
  1667. if (targetTime >= accumulatedTime && targetTime <= accumulatedTime + audioDuration) {
  1668. targetAudioIndex = i;
  1669. targetAudioTime = targetTime - accumulatedTime;
  1670. break;
  1671. }
  1672. accumulatedTime += audioDuration;
  1673. }
  1674. // 如果沒有找到合適的音頻片段,使用最後一個
  1675. if (targetAudioIndex === -1 && this.currentPageAudios.length > 0) {
  1676. targetAudioIndex = this.currentPageAudios.length - 1;
  1677. targetAudioTime = this.currentPageAudios[targetAudioIndex].duration || 0;
  1678. console.log('使用最後一個音頻片段作為目標');
  1679. }
  1680. console.log('目標音頻索引:', targetAudioIndex, '目標音頻時間:', targetAudioTime);
  1681. if (targetAudioIndex === -1) {
  1682. console.error('無法找到目標音頻片段');
  1683. return;
  1684. }
  1685. // 如果需要切換到不同的音頻片段
  1686. if (targetAudioIndex !== this.currentAudioIndex) {
  1687. console.log(`切換音頻片段: ${this.currentAudioIndex} -> ${targetAudioIndex}`);
  1688. this.currentAudioIndex = targetAudioIndex;
  1689. this.createAudioInstance();
  1690. // 等待音頻實例創建完成後再跳轉
  1691. this.waitForAudioReady(() => {
  1692. if (this.currentAudio) {
  1693. this.currentAudio.seek(targetAudioTime);
  1694. this.currentTime = targetTime;
  1695. console.log('切換音頻並跳轉到:', targetAudioTime, '秒');
  1696. // 如果拖動前正在播放,則恢復播放
  1697. if (shouldResume) {
  1698. this.currentAudio.play();
  1699. this.isPlaying = true;
  1700. console.log('恢復播放狀態');
  1701. }
  1702. }
  1703. });
  1704. } else {
  1705. // 在當前音頻片段內跳轉
  1706. if (this.currentAudio) {
  1707. this.currentAudio.seek(targetAudioTime);
  1708. this.currentTime = targetTime;
  1709. console.log('在當前音頻內跳轉到:', targetAudioTime, '秒');
  1710. // 如果拖動前正在播放,則恢復播放
  1711. if (shouldResume) {
  1712. this.currentAudio.play();
  1713. this.isPlaying = true;
  1714. console.log('恢復播放狀態');
  1715. }
  1716. }
  1717. }
  1718. },
  1719. // 等待音頻實例準備就緒
  1720. waitForAudioReady(callback, maxAttempts = 10, currentAttempt = 0) {
  1721. if (currentAttempt >= maxAttempts) {
  1722. console.error('音頻實例準備超時');
  1723. return;
  1724. }
  1725. if (this.currentAudio && this.currentAudio.src) {
  1726. // 音頻實例已準備好
  1727. setTimeout(callback, 50); // 稍微延遲確保完全準備好
  1728. } else {
  1729. // 繼續等待
  1730. setTimeout(() => {
  1731. this.waitForAudioReady(callback, maxAttempts, currentAttempt + 1);
  1732. }, 100);
  1733. }
  1734. },
  1735. // 初始检测播放速度支持(简化版本,默认启用)
  1736. checkInitialPlaybackRateSupport() {
  1737. console.log('🚀 开始初始倍速播放支持检测...');
  1738. try {
  1739. const systemInfo = uni.getSystemInfoSync();
  1740. console.log('📱 系统信息:', {
  1741. platform: systemInfo.platform,
  1742. system: systemInfo.system,
  1743. SDKVersion: systemInfo.SDKVersion,
  1744. brand: systemInfo.brand,
  1745. model: systemInfo.model
  1746. });
  1747. // 简化检测逻辑:默认启用倍速功能
  1748. // 只在极少数明确不支持的情况下禁用
  1749. this.playbackRateSupported = true;
  1750. // 仅对非常老的Android版本进行限制(Android 4.x及以下)
  1751. if (systemInfo.platform === 'android') {
  1752. const androidVersion = systemInfo.system.match(/Android (\d+)/);
  1753. if (androidVersion && parseInt(androidVersion[1]) < 5) {
  1754. this.playbackRateSupported = false;
  1755. console.log(`⚠️ Android版本过低 (${androidVersion[1]}),禁用倍速功能`);
  1756. uni.showToast({
  1757. title: `Android ${androidVersion[1]} 不支持倍速`,
  1758. icon: 'none',
  1759. duration: 2000
  1760. });
  1761. return;
  1762. }
  1763. }
  1764. console.log('✅ 倍速功能已启用!');
  1765. console.log('📊 可用倍速选项:', this.speedOptions);
  1766. console.log('⚡ 当前播放速度:', this.playSpeed + 'x');
  1767. // 显示成功提示
  1768. uni.showToast({
  1769. title: '✅ 倍速功能可用',
  1770. icon: 'none',
  1771. duration: 1500
  1772. });
  1773. } catch (error) {
  1774. console.error('💥 检测播放速度支持时出错:', error);
  1775. // 即使出错也默认启用
  1776. this.playbackRateSupported = true;
  1777. console.log('⚠️ 检测出错,但仍启用倍速功能');
  1778. }
  1779. },
  1780. // 应用播放速度设置
  1781. applyPlaybackRate(audio) {
  1782. if (!audio) return;
  1783. console.log('⚙️ 开始应用播放速度设置...');
  1784. console.log('📊 当前状态检查:', {
  1785. playbackRateSupported: this.playbackRateSupported,
  1786. 期望速度: this.playSpeed + 'x',
  1787. 音频当前速度: audio.playbackRate + 'x',
  1788. 音频播放状态: this.isPlaying ? '播放中' : '未播放'
  1789. });
  1790. if (this.playbackRateSupported) {
  1791. try {
  1792. // 多次尝试设置倍速,确保生效
  1793. const maxAttempts = 3;
  1794. let attempt = 0;
  1795. const trySetRate = () => {
  1796. attempt++;
  1797. audio.playbackRate = this.playSpeed;
  1798. setTimeout(() => {
  1799. const actualRate = audio.playbackRate;
  1800. const rateDifference = Math.abs(actualRate - this.playSpeed);
  1801. console.log(`🔍 倍速设置尝试 ${attempt}/${maxAttempts}:`, {
  1802. 期望速度: this.playSpeed,
  1803. 实际速度: actualRate,
  1804. 差值: rateDifference,
  1805. 设置成功: rateDifference < 0.01
  1806. });
  1807. if (rateDifference >= 0.01 && attempt < maxAttempts) {
  1808. console.log(`⚠️ 倍速设置未完全生效,进行第 ${attempt + 1} 次尝试...`);
  1809. setTimeout(trySetRate, 100);
  1810. } else if (rateDifference < 0.01) {
  1811. console.log('✅ 倍速设置成功!');
  1812. } else {
  1813. console.log('⚠️ 倍速设置可能不完全支持,但已尽力尝试');
  1814. }
  1815. }, 50);
  1816. };
  1817. trySetRate();
  1818. } catch (error) {
  1819. console.log('💥 设置播放速度时出错:', error);
  1820. }
  1821. } else {
  1822. console.log('❌ 当前环境不支持播放速度控制');
  1823. }
  1824. },
  1825. // 检查播放速度控制支持(简化版本)
  1826. checkPlaybackRateSupport(audio) {
  1827. console.log('🎵 开始音频实例倍速支持检测...');
  1828. try {
  1829. // 如果初始检测已经禁用,直接返回
  1830. if (!this.playbackRateSupported) {
  1831. console.log('⚠️ 初始检测已禁用倍速功能,跳过音频实例检测');
  1832. return;
  1833. }
  1834. console.log('🎧 音频实例信息:', {
  1835. 音频对象存在: !!audio,
  1836. 音频对象类型: typeof audio,
  1837. 音频src: audio ? audio.src : '无'
  1838. });
  1839. // 检测音频实例类型和倍速支持
  1840. let isHTML5Audio = false;
  1841. let supportsPlaybackRate = false;
  1842. if (audio) {
  1843. // 检查是否为HTML5 Audio包装实例
  1844. if (audio._nativeAudio && audio._nativeAudio instanceof Audio) {
  1845. isHTML5Audio = true;
  1846. supportsPlaybackRate = true;
  1847. console.log('✅ 检测到HTML5 Audio包装实例,完全支持倍速播放');
  1848. }
  1849. // 检查是否为原生HTML5 Audio
  1850. else if (audio instanceof Audio) {
  1851. isHTML5Audio = true;
  1852. supportsPlaybackRate = true;
  1853. console.log('✅ 检测到原生HTML5 Audio实例,完全支持倍速播放');
  1854. }
  1855. // 检查uni-app音频实例的playbackRate属性
  1856. else if (typeof audio.playbackRate !== 'undefined') {
  1857. supportsPlaybackRate = true;
  1858. console.log('✅ 音频实例支持playbackRate属性');
  1859. } else {
  1860. console.log('⚠️ 音频实例不支持playbackRate属性');
  1861. }
  1862. // console.log('🔍 音频实例分析:', {
  1863. // 是否HTML5Audio: isHTML5Audio,
  1864. // 支持倍速: supportsPlaybackRate,
  1865. // 实例类型: audio.constructor?.name || 'unknown',
  1866. // playbackRate属性: typeof audio.playbackRate
  1867. // });
  1868. // 如果支持倍速,尝试设置当前播放速度
  1869. if (supportsPlaybackRate) {
  1870. try {
  1871. const currentSpeed = this.playSpeed || 1.0;
  1872. audio.playbackRate = currentSpeed;
  1873. // console.log(`🔧 设置播放速度为 ${currentSpeed}x`);
  1874. // 验证设置结果
  1875. // setTimeout(() => {
  1876. // const actualRate = audio.playbackRate;
  1877. // console.log('🔍 播放速度验证:', {
  1878. // 期望值: currentSpeed,
  1879. // 实际值: actualRate,
  1880. // 设置成功: Math.abs(actualRate - currentSpeed) < 0.1
  1881. // });
  1882. // }, 50);
  1883. } catch (error) {
  1884. console.log('⚠️ 设置播放速度时出现错误:', error.message);
  1885. }
  1886. }
  1887. } else {
  1888. console.log('⚠️ 音频实例不存在');
  1889. }
  1890. // 保持倍速功能启用状态
  1891. console.log('✅ 音频实例检测完成,倍速功能保持启用');
  1892. } catch (error) {
  1893. console.error('💥 检查播放速度支持时出错:', error);
  1894. // 即使出错也保持启用状态
  1895. console.log('⚠️ 检测出错,但保持倍速功能启用');
  1896. }
  1897. },
  1898. formatTime(seconds) {
  1899. const mins = Math.floor(seconds / 60);
  1900. const secs = Math.floor(seconds % 60);
  1901. return `${mins.toString().padStart(2, '0')}:${secs.toString().padStart(2, '0')}`;
  1902. },
  1903. // 清理音频缓存
  1904. clearAudioCache() {
  1905. this.audioCache = {};
  1906. console.log('音频缓存已清理');
  1907. },
  1908. // 限制缓存大小,保留最近访问的页面
  1909. limitCacheSize(maxSize = 10) {
  1910. const cacheKeys = Object.keys(this.audioCache);
  1911. if (cacheKeys.length > maxSize) {
  1912. // 删除最旧的缓存项
  1913. const keysToDelete = cacheKeys.slice(0, cacheKeys.length - maxSize);
  1914. keysToDelete.forEach(key => {
  1915. delete this.audioCache[key];
  1916. });
  1917. console.log('缓存大小已限制,删除了', keysToDelete.length, '个缓存项');
  1918. }
  1919. },
  1920. // 自動加載第一頁音頻並播放
  1921. async autoLoadAndPlayFirstPage() {
  1922. try {
  1923. console.log('開始自動加載第一頁音頻');
  1924. // 確保當前是第一頁且需要加載音頻
  1925. if (this.currentPage === 1 && this.shouldLoadAudio) {
  1926. console.log('當前是第一頁且需要音頻,開始加載音頻');
  1927. // 加載音頻
  1928. await this.getCurrentPageAudio();
  1929. // 檢查是否成功加載音頻
  1930. if (this.currentPageAudios && this.currentPageAudios.length > 0) {
  1931. console.log('音頻加載成功,getCurrentPageAudio已經自動播放第一個音頻');
  1932. // getCurrentPageAudio方法已經處理了第一個音頻的播放,這裡不需要再次調用playAudio
  1933. } else {
  1934. console.log('第一頁沒有音頻數據');
  1935. }
  1936. } else {
  1937. console.log('當前頁面不是第一頁文字頁面,跳過自動播放');
  1938. }
  1939. } catch (error) {
  1940. console.error('自動加載和播放音頻失敗:', error);
  1941. }
  1942. },
  1943. // 清理音频资源
  1944. destroyAudio() {
  1945. console.log('🧹 AudioControls: 开始清理音频资源');
  1946. // 1. 停止并销毁当前音频实例
  1947. if (this.currentAudio) {
  1948. console.log('🧹 销毁当前音频实例');
  1949. try {
  1950. // 先暂停再销毁
  1951. if (this.isPlaying) {
  1952. this.currentAudio.pause();
  1953. }
  1954. this.currentAudio.destroy();
  1955. } catch (error) {
  1956. console.error('销毁音频实例失败:', error);
  1957. }
  1958. this.currentAudio = null;
  1959. }
  1960. // 2. 重置所有播放状态
  1961. this.isPlaying = false;
  1962. this.currentTime = 0;
  1963. this.sliderValue = 0;
  1964. this.currentHighlightIndex = -1;
  1965. // 3. 清理音频缓存
  1966. this.clearAudioCache();
  1967. // 4. 取消正在进行的请求
  1968. this.shouldCancelRequest = true;
  1969. // 5. 重置加载状态
  1970. this.isAudioLoading = false;
  1971. this.isVoiceChanging = false;
  1972. this.audioLoadFailed = false;
  1973. console.log('✅ AudioControls: 音频资源清理完成');
  1974. },
  1975. // 停止单词音频播放(全局音频管理)
  1976. stopWordAudio() {
  1977. try {
  1978. // 通过父组件访问book页面的单词音频
  1979. const pages = getCurrentPages();
  1980. const currentPage = pages[pages.length - 1];
  1981. if (currentPage && currentPage.$vm) {
  1982. const bookVm = currentPage.$vm;
  1983. if (bookVm.currentWordAudio) {
  1984. console.log('🔄 AudioControls: 停止单词音频播放');
  1985. bookVm.currentWordAudio.pause();
  1986. bookVm.currentWordAudio.destroy();
  1987. bookVm.currentWordAudio = null;
  1988. bookVm.isWordAudioPlaying = false;
  1989. console.log('✅ AudioControls: 单词音频已停止');
  1990. }
  1991. }
  1992. } catch (error) {
  1993. console.log('⚠️ AudioControls: 停止单词音频时出现异常:', error);
  1994. }
  1995. },
  1996. // 课程切换时的完整数据清理(保留音色设置)
  1997. resetForCourseChange() {
  1998. console.log('课程切换:开始清理所有数据,保留音色设置');
  1999. // 1. 停止并销毁当前音频
  2000. if (this.isPlaying && this.currentAudio) {
  2001. this.pauseAudio();
  2002. }
  2003. if (this.currentAudio) {
  2004. this.currentAudio.destroy();
  2005. this.currentAudio = null;
  2006. }
  2007. // 2. 清空所有音频相关数据
  2008. this.currentPageAudios = [];
  2009. this.currentAudioIndex = 0;
  2010. this.currentTime = 0;
  2011. this.totalTime = 0;
  2012. this.sliderValue = 0;
  2013. this.isDragging = false;
  2014. this.isPlaying = false;
  2015. this.hasAudioData = false;
  2016. this.isAudioLoading = false;
  2017. this.audioLoadFailed = false;
  2018. this.currentHighlightIndex = -1;
  2019. // 3. 清空音频缓存(因为课程变了,所有缓存都无效)
  2020. this.clearAudioCache();
  2021. // 4. 重置预加载状态
  2022. this.isPreloading = false;
  2023. this.preloadProgress = 0;
  2024. this.preloadedPages = new Set();
  2025. // 5. 重置播放控制状态
  2026. this.isLoop = false;
  2027. this.playSpeed = 1.0;
  2028. this.playbackRateSupported = true;
  2029. // 6. 重置音色切换状态
  2030. this.isVoiceChanging = false;
  2031. // 7. 设置课程切换状态
  2032. this.isJustSwitchedCourse = true;
  2033. // 注意:不清空 voiceId,保留用户的音色选择
  2034. console.log('课程切换:数据清理完成,音色保持为:', this.voiceId);
  2035. // 7. 通知父组件状态变化
  2036. this.$emit('audio-state-change', {
  2037. hasAudioData: false,
  2038. isLoading: false,
  2039. audioLoadFailed: false,
  2040. currentHighlightIndex: -1
  2041. });
  2042. },
  2043. // 自动加载并播放音频(课程切换后调用)
  2044. async autoLoadAndPlayAudio() {
  2045. console.log('开始自动加载音频');
  2046. // 检查是否需要加载音频
  2047. if (!this.shouldLoadAudio) {
  2048. console.log('当前页面不需要加载音频,跳过音频加载');
  2049. return;
  2050. }
  2051. // 检查必要条件
  2052. if (!this.courseId || !this.currentPage) {
  2053. console.log('缺少必要参数,跳过音频加载');
  2054. return;
  2055. }
  2056. try {
  2057. // 设置加载状态
  2058. this.isAudioLoading = true;
  2059. // 开始加载音频
  2060. await this.getCurrentPageAudio();
  2061. console.log('音频自动加载完成,hasAudioData:', this.hasAudioData);
  2062. } catch (error) {
  2063. console.error('自动加载音频失败:', error);
  2064. this.isAudioLoading = false;
  2065. }
  2066. },
  2067. // 暂停音频(页面隐藏时调用)
  2068. pauseOnHide() {
  2069. this.pauseAudio();
  2070. },
  2071. // 处理音色切换(由父组件调用)
  2072. async handleVoiceChange(newVoiceId, options = {}) {
  2073. console.log('🎵 AudioControls: 开始处理音色切换', this.voiceId, '->', newVoiceId);
  2074. // 检查是否正在加载音频,如果是则阻止音色切换
  2075. if (this.isAudioLoading) {
  2076. console.log('🎵 音频正在加载中,阻止音色切换');
  2077. throw new Error('音频加载中,请稍后再试');
  2078. }
  2079. const { preloadAllPages = true } = options; // 默认预加载所有页面
  2080. try {
  2081. // 1. 停止当前播放的音频
  2082. if (this.isPlaying) {
  2083. this.pauseAudio();
  2084. }
  2085. // 2. 销毁当前音频实例
  2086. if (this.currentAudio) {
  2087. this.currentAudio.destroy();
  2088. this.currentAudio = null;
  2089. }
  2090. // 3. 清理所有音频缓存(因为音色变了,所有缓存都无效)
  2091. this.clearAudioCache();
  2092. console.log('🎵 AudioControls: 已清理所有音频缓存');
  2093. // 4. 重置音频状态
  2094. this.currentPageAudios = [];
  2095. this.currentAudioIndex = 0;
  2096. this.isPlaying = false;
  2097. this.currentTime = 0;
  2098. this.totalTime = 0;
  2099. this.hasAudioData = false;
  2100. this.audioLoadFailed = false;
  2101. this.currentHighlightIndex = -1;
  2102. // 5. 设置音色切换加载状态
  2103. this.isVoiceChanging = true;
  2104. this.isAudioLoading = true;
  2105. // 6. 更新本地音色ID(不直接修改prop)
  2106. this.localVoiceId = newVoiceId;
  2107. // 7. 通知父组件开始加载状态
  2108. this.$emit('audio-state-change', {
  2109. hasAudioData: false,
  2110. isLoading: true,
  2111. currentHighlightIndex: -1
  2112. });
  2113. // 8. 如果当前页面需要加载音频,优先获取当前页面音频
  2114. if (this.shouldLoadAudio && this.courseId && this.currentPage) {
  2115. console.log('🎵 AudioControls: 优先获取当前页面音频,使用新音色:', newVoiceId);
  2116. await this.getCurrentPageAudio();
  2117. console.log('🎵 AudioControls: 当前页面音频获取完成');
  2118. } else {
  2119. // 如果不需要加载音频,直接清除加载状态
  2120. this.isAudioLoading = false;
  2121. console.log('🎵 AudioControls: 不需要音频的页面,直接清除加载状态');
  2122. }
  2123. // 9. 清除音色切换加载状态
  2124. this.isVoiceChanging = false;
  2125. // 10. 如果需要预加载其他页面,启动预加载
  2126. if (preloadAllPages) {
  2127. console.log('🎵 AudioControls: 开始预加载其他页面音频,使用新音色:', newVoiceId);
  2128. // 延迟启动预加载,确保当前页面音频加载完成
  2129. setTimeout(() => {
  2130. this.preloadAllPagesAudio();
  2131. }, 1000);
  2132. }
  2133. // 11. 通知父组件最终状态
  2134. this.$emit('audio-state-change', {
  2135. hasAudioData: this.hasAudioData,
  2136. isLoading: this.isAudioLoading,
  2137. currentHighlightIndex: this.currentHighlightIndex
  2138. });
  2139. // 12. 通知父组件音色切换完成
  2140. this.$emit('voice-change-complete', {
  2141. voiceId: newVoiceId,
  2142. hasAudioData: this.hasAudioData,
  2143. preloadAllPages: preloadAllPages
  2144. });
  2145. console.log('🎵 AudioControls: 音色切换处理完成');
  2146. } catch (error) {
  2147. console.error('🎵 AudioControls: 音色切换处理失败:', error);
  2148. // 清除加载状态
  2149. this.isVoiceChanging = false;
  2150. this.isAudioLoading = false;
  2151. // 通知父组件状态变化
  2152. this.$emit('audio-state-change', {
  2153. hasAudioData: false,
  2154. isLoading: false,
  2155. currentHighlightIndex: -1
  2156. });
  2157. this.$emit('voice-change-error', error);
  2158. }
  2159. },
  2160. // 预加载所有页面音频(音色切换时使用)
  2161. async preloadAllPagesAudio() {
  2162. if (this.isPreloading) {
  2163. console.log('已在预加载中,跳过');
  2164. return;
  2165. }
  2166. try {
  2167. console.log('开始预加载所有页面音频');
  2168. this.isPreloading = true;
  2169. this.preloadProgress = 0;
  2170. // 获取所有文本页面
  2171. const allTextPages = [];
  2172. for (let i = 0; i < this.bookPages.length; i++) {
  2173. const pageData = this.bookPages[i];
  2174. const hasTextContent = pageData && pageData.some(item => item.type === 'text');
  2175. if (hasTextContent && i !== this.currentPage - 1) { // 排除当前页面,因为已经加载过了
  2176. allTextPages.push({
  2177. pageIndex: i + 1,
  2178. pageData: pageData
  2179. });
  2180. }
  2181. }
  2182. if (allTextPages.length === 0) {
  2183. console.log('没有需要预加载的页面');
  2184. this.isPreloading = false;
  2185. return;
  2186. }
  2187. console.log(`需要预加载 ${allTextPages.length} 个页面的音频`);
  2188. // 逐页预加载音频
  2189. for (let i = 0; i < allTextPages.length; i++) {
  2190. const pageInfo = allTextPages[i];
  2191. try {
  2192. console.log(`预加载第 ${pageInfo.pageIndex} 页音频 (${i + 1}/${allTextPages.length})`);
  2193. await this.preloadPageAudio(pageInfo.pageIndex, pageInfo.pageData);
  2194. // 更新进度
  2195. this.preloadProgress = Math.round(((i + 1) / allTextPages.length) * 100);
  2196. console.log(`预加载进度: ${this.preloadProgress}%`);
  2197. // 添加小延迟,避免请求过于频繁
  2198. if (i < allTextPages.length - 1) {
  2199. await new Promise(resolve => setTimeout(resolve, 200));
  2200. }
  2201. } catch (error) {
  2202. console.error(`预加载第 ${pageInfo.pageIndex} 页音频失败:`, error);
  2203. // 继续预加载其他页面,不因单页失败而中断
  2204. }
  2205. }
  2206. console.log('所有页面音频预加载完成');
  2207. } catch (error) {
  2208. console.error('预加载所有页面音频失败:', error);
  2209. } finally {
  2210. this.isPreloading = false;
  2211. this.preloadProgress = 100;
  2212. console.log('音色切换后的音频预加载任务结束');
  2213. }
  2214. },
  2215. // 开始预加载音频(由父组件调用)
  2216. async startPreloadAudio() {
  2217. if (this.isPreloading) {
  2218. console.log('已在预加载中,跳过');
  2219. return;
  2220. }
  2221. try {
  2222. console.log('开始预加载音频数据');
  2223. this.isPreloading = true;
  2224. this.preloadProgress = 0;
  2225. // 获取需要预加载的页面列表(当前页面后的几页)
  2226. const preloadPages = this.getPreloadPageList();
  2227. if (preloadPages.length === 0) {
  2228. console.log('没有需要预加载的页面');
  2229. this.isPreloading = false;
  2230. return;
  2231. }
  2232. console.log('需要预加载的页面:', preloadPages);
  2233. // 逐个预加载页面音频
  2234. for (let i = 0; i < preloadPages.length; i++) {
  2235. const pageInfo = preloadPages[i];
  2236. try {
  2237. console.log(`开始预加载第${pageInfo.pageIndex + 1}页音频`);
  2238. await this.preloadPageAudio(pageInfo.pageIndex, pageInfo.pageData);
  2239. // 更新预加载进度
  2240. this.preloadProgress = Math.round(((i + 1) / preloadPages.length) * 100);
  2241. console.log(`预加载进度: ${this.preloadProgress}%`);
  2242. // 延迟一下,避免请求过于频繁
  2243. await new Promise(resolve => setTimeout(resolve, 300));
  2244. } catch (error) {
  2245. console.error(`预加载第${pageInfo.pageIndex + 1}页音频失败:`, error);
  2246. // 继续预加载其他页面
  2247. }
  2248. }
  2249. console.log('音频预加载完成');
  2250. } catch (error) {
  2251. console.error('预加载音频失败:', error);
  2252. } finally {
  2253. this.isPreloading = false;
  2254. this.preloadProgress = 100;
  2255. }
  2256. },
  2257. // 获取需要预加载的页面列表
  2258. getPreloadPageList() {
  2259. const preloadPages = [];
  2260. const maxPreloadPages = 3; // 优化:最多预加载3页,减少服务器压力
  2261. // 从当前页面的下一页开始预加载
  2262. for (let i = this.currentPage; i < Math.min(this.currentPage + maxPreloadPages, this.bookPages.length); i++) {
  2263. const pageData = this.bookPages[i];
  2264. // 检查页面是否需要会员且用户非会员,如果是则跳过
  2265. const pageRequiresMember = this.pagePay[i] === 'Y';
  2266. if (pageRequiresMember && !this.isMember) {
  2267. console.log(`${i + 1}页需要会员,跳过预加载`);
  2268. continue;
  2269. }
  2270. // 检查页面是否有文本内容且未缓存
  2271. if (pageData && pageData.length > 0) {
  2272. const hasTextContent = pageData.some(item => item.type === 'text' && item.content);
  2273. const cacheKey = `${this.courseId}_${i + 1}_${this.voiceId}`;
  2274. const isAlreadyCached = this.audioCache[cacheKey];
  2275. if (hasTextContent && !isAlreadyCached) {
  2276. preloadPages.push({
  2277. pageIndex: i,
  2278. pageData: pageData
  2279. });
  2280. }
  2281. }
  2282. }
  2283. return preloadPages;
  2284. },
  2285. // 预加载单个页面的音频
  2286. async preloadPageAudio(pageIndex, pageData) {
  2287. const cacheKey = `${this.courseId}_${pageIndex + 1}_${this.voiceId}`;
  2288. // 检查是否已经缓存
  2289. if (this.audioCache[cacheKey]) {
  2290. console.log(`${pageIndex + 1}页音频已缓存,跳过`);
  2291. return;
  2292. }
  2293. // 收集页面中的文本内容
  2294. const textItems = pageData.filter(item => item.type === 'text' && item.content);
  2295. if (textItems.length === 0) {
  2296. console.log(`${pageIndex + 1}页没有文本内容,跳过`);
  2297. return;
  2298. }
  2299. console.log(`${pageIndex + 1}页有${textItems.length}个文本段落需要预加载`);
  2300. const audioArray = [];
  2301. let totalDuration = 0;
  2302. // 逐个处理文本项,支持长文本分割
  2303. for (let i = 0; i < textItems.length; i++) {
  2304. const item = textItems[i];
  2305. try {
  2306. console.log(`${pageIndex + 1}页处理第 ${i + 1}/${textItems.length} 个文本项,长度: ${item.content.length}`);
  2307. // 使用分批次请求音频
  2308. const batchResult = await this.requestAudioInBatches(item.content, this.localVoiceId);
  2309. // 检查请求是否被取消
  2310. if (batchResult === null) {
  2311. console.log(`${pageIndex + 1}页预加载被取消`);
  2312. return;
  2313. }
  2314. if (batchResult.audioSegments.length > 0) {
  2315. // 将所有音频段添加到音频数组
  2316. for (const segment of batchResult.audioSegments) {
  2317. if (!segment.error) {
  2318. audioArray.push({
  2319. url: segment.url,
  2320. text: segment.text,
  2321. duration: segment.duration,
  2322. startIndex: segment.startIndex,
  2323. endIndex: segment.endIndex,
  2324. segmentIndex: segment.segmentIndex,
  2325. originalTextIndex: i, // 标记属于哪个原始文本项
  2326. isSegmented: batchResult.audioSegments.length > 1 // 标记是否为分段音频
  2327. });
  2328. totalDuration += segment.duration;
  2329. }
  2330. }
  2331. console.log(`${pageIndex + 1}页第${i + 1}个文本项预加载完成,获得 ${batchResult.audioSegments.filter(s => !s.error).length} 个音频段`);
  2332. } else {
  2333. console.error(`${pageIndex + 1}页第${i + 1}个文本项音频预加载全部失败`);
  2334. }
  2335. } catch (error) {
  2336. console.error(`${pageIndex + 1}页第${i + 1}个文本项处理异常:`, error);
  2337. }
  2338. // 每个文本项处理之间间隔300ms,避免请求过于频繁
  2339. if (i < textItems.length - 1) {
  2340. await new Promise(resolve => setTimeout(resolve, 300));
  2341. }
  2342. }
  2343. // 保存到缓存
  2344. if (audioArray.length > 0) {
  2345. this.audioCache[cacheKey] = {
  2346. audios: audioArray,
  2347. totalDuration: totalDuration,
  2348. voiceId: this.localVoiceId, // 保存音色ID用于验证
  2349. timestamp: Date.now() // 保存时间戳
  2350. };
  2351. console.log(`${pageIndex + 1}页音频预加载完成,共${audioArray.length}个音频,总时长: ${totalDuration}`);
  2352. // 限制缓存大小
  2353. this.limitCacheSize(10);
  2354. }
  2355. },
  2356. // 检查指定页面是否有音频缓存
  2357. checkAudioCache(pageNumber) {
  2358. const cacheKey = `${this.courseId}_${pageNumber}_${this.localVoiceId}`;
  2359. const cachedData = this.audioCache[cacheKey];
  2360. if (cachedData && cachedData.audios && cachedData.audios.length > 0) {
  2361. console.log(`${pageNumber}页音频已缓存,共${cachedData.audios.length}个音频`);
  2362. return true;
  2363. }
  2364. console.log(`${pageNumber}页音频未缓存`);
  2365. return false;
  2366. },
  2367. // 自动播放已缓存的音频
  2368. async autoPlayCachedAudio() {
  2369. try {
  2370. // 如果正在音色切换中,不自动播放
  2371. if (this.isVoiceChanging) {
  2372. console.log('音色切换中,跳过自动播放');
  2373. return;
  2374. }
  2375. const cacheKey = `${this.courseId}_${this.currentPage}_${this.voiceId}`;
  2376. const cachedData = this.audioCache[cacheKey];
  2377. if (!cachedData || !cachedData.audios || cachedData.audios.length === 0) {
  2378. console.log('当前页面没有缓存的音频');
  2379. return;
  2380. }
  2381. console.log(`开始自动播放第${this.currentPage}页的缓存音频`);
  2382. // 停止当前播放的音频
  2383. this.pauseAudio();
  2384. // 设置当前页面的音频数据
  2385. this.currentPageAudios = cachedData.audios;
  2386. this.totalDuration = cachedData.totalDuration;
  2387. // 重置播放状态
  2388. this.currentAudioIndex = 0;
  2389. this.currentTime = 0;
  2390. this.isPlaying = false;
  2391. // 延迟一下再开始播放,确保UI更新完成
  2392. setTimeout(() => {
  2393. this.playAudio();
  2394. }, 300);
  2395. } catch (error) {
  2396. console.error('自动播放缓存音频失败:', error);
  2397. }
  2398. }
  2399. },
  2400. mounted() {
  2401. console.log('🎛️ AudioControls組件初始化開始');
  2402. console.log('⚙️ 初始倍速配置:', {
  2403. 默認播放速度: this.playSpeed + 'x',
  2404. 可選速度選項: this.speedOptions.map(s => s + 'x'),
  2405. 初始支持狀態: this.playbackRateSupported
  2406. });
  2407. // 初始檢測播放速度支持
  2408. console.log('🔍 開始檢測播放速度支持...');
  2409. this.checkInitialPlaybackRateSupport();
  2410. console.log('✅ AudioControls組件初始化完成');
  2411. },
  2412. // 自动播放预加载的音频
  2413. async autoPlayPreloadedAudio() {
  2414. try {
  2415. // 如果正在音色切换中,不自动播放
  2416. if (this.isVoiceChanging) {
  2417. console.log('音色切换中,跳过预加载音频自动播放');
  2418. return;
  2419. }
  2420. // 检查是否有音频数据
  2421. if (!this.hasAudioData || this.currentPageAudios.length === 0) {
  2422. console.log('没有可播放的音频数据');
  2423. return;
  2424. }
  2425. // 检查第一个音频是否有效
  2426. const firstAudio = this.currentPageAudios[0];
  2427. if (!firstAudio || !firstAudio.url) {
  2428. console.log('第一个音频无效');
  2429. return;
  2430. }
  2431. console.log('开始自动播放预加载音频');
  2432. // 重置播放状态
  2433. this.currentAudioIndex = 0;
  2434. this.currentTime = 0;
  2435. this.sliderValue = 0;
  2436. this.currentHighlightIndex = 0;
  2437. // 创建音频实例
  2438. this.createAudioInstance();
  2439. // 稍等一下再播放,确保音频准备就绪
  2440. setTimeout(() => {
  2441. if (this.currentAudio && !this.isPlaying) {
  2442. this.currentAudio.play();
  2443. console.log('预加载音频自动播放成功');
  2444. }
  2445. }, 200);
  2446. } catch (error) {
  2447. console.error('自动播放预加载音频失败:', error);
  2448. }
  2449. },
  2450. beforeDestroy() {
  2451. // 清理页面切换防抖定时器
  2452. if (this.pageChangeTimer) {
  2453. clearTimeout(this.pageChangeTimer);
  2454. this.pageChangeTimer = null;
  2455. }
  2456. // 清理音频资源
  2457. this.destroyAudio();
  2458. console.log('AudioControls组件已销毁,清理所有资源');
  2459. }
  2460. }
  2461. </script>
  2462. <style lang="scss" scoped>
  2463. /* 音频控制栏样式 */
  2464. .audio-controls-wrapper {
  2465. position: relative;
  2466. z-index: 10;
  2467. }
  2468. .audio-controls {
  2469. background: #fff;
  2470. padding: 20rpx 40rpx;
  2471. border-bottom: 1rpx solid #eee;
  2472. transition: transform 0.3s ease;
  2473. position: relative;
  2474. z-index: 10;
  2475. &.audio-hidden {
  2476. transform: translateY(100%);
  2477. }
  2478. }
  2479. .audio-time {
  2480. display: flex;
  2481. align-items: center;
  2482. margin-bottom: 20rpx;
  2483. }
  2484. .time-text {
  2485. font-size: 28rpx;
  2486. color: #999;
  2487. min-width: 80rpx;
  2488. }
  2489. .progress-container {
  2490. flex: 1;
  2491. margin: 0 20rpx;
  2492. }
  2493. .audio-controls-row {
  2494. display: flex;
  2495. align-items: center;
  2496. justify-content: space-between;
  2497. }
  2498. .control-btn {
  2499. display: flex;
  2500. align-items: center;
  2501. padding: 10rpx;
  2502. gap: 8rpx;
  2503. }
  2504. .control-btn.disabled {
  2505. pointer-events: none;
  2506. opacity: 0.6;
  2507. }
  2508. .control-text {
  2509. font-size: 28rpx;
  2510. color: #4A4A4A;
  2511. }
  2512. .play-btn {
  2513. display: flex;
  2514. align-items: center;
  2515. justify-content: center;
  2516. padding: 10rpx;
  2517. }
  2518. /* 音频加载状态样式 */
  2519. .audio-loading-container {
  2520. background: #fff;
  2521. padding: 40rpx;
  2522. border-bottom: 1rpx solid #eee;
  2523. display: flex;
  2524. flex-direction: column;
  2525. align-items: center;
  2526. justify-content: center;
  2527. gap: 20rpx;
  2528. position: relative;
  2529. z-index: 10;
  2530. }
  2531. /* 加载指示器样式 */
  2532. .loading-indicator {
  2533. display: flex;
  2534. align-items: center;
  2535. justify-content: center;
  2536. gap: 10rpx;
  2537. padding: 10rpx 20rpx;
  2538. background: rgba(6, 218, 220, 0.1);
  2539. border-radius: 20rpx;
  2540. margin-bottom: 10rpx;
  2541. }
  2542. .loading-indicator-text {
  2543. font-size: 24rpx;
  2544. color: #06DADC;
  2545. }
  2546. .loading-text {
  2547. font-size: 28rpx;
  2548. color: #999;
  2549. }
  2550. /* 音色切换加载状态特殊样式 */
  2551. .voice-changing {
  2552. background: linear-gradient(135deg, #fff5f0 0%, #ffe7d9 100%);
  2553. border: 2rpx solid #ff6b35;
  2554. }
  2555. .voice-changing-text {
  2556. color: #ff6b35;
  2557. font-weight: 500;
  2558. }
  2559. /* 预加载状态特殊样式 */
  2560. .preloading {
  2561. background: linear-gradient(135deg, #f0f9ff 0%, #e0f2fe 100%);
  2562. border: 2rpx solid #06DADC;
  2563. }
  2564. .preloading .loading-text {
  2565. color: #06DADC;
  2566. font-weight: 500;
  2567. }
  2568. /* 课程切换状态特殊样式 */
  2569. .course-switching {
  2570. background: linear-gradient(135deg, #f6ffed 0%, #d9f7be 100%);
  2571. border: 2rpx solid #52c41a;
  2572. }
  2573. .course-switching .loading-text {
  2574. color: #52c41a;
  2575. font-weight: 500;
  2576. }
  2577. /* 获取音频按钮样式 */
  2578. .audio-get-button-container {
  2579. background: rgba(255, 255, 255, 0.95);
  2580. backdrop-filter: blur(10px);
  2581. padding: 30rpx;
  2582. border-radius: 20rpx;
  2583. border: 2rpx solid #E5E5E5;
  2584. transition: all 0.3s ease;
  2585. position: relative;
  2586. z-index: 10;
  2587. }
  2588. .get-audio-btn {
  2589. display: flex;
  2590. align-items: center;
  2591. justify-content: center;
  2592. gap: 16rpx;
  2593. padding: 20rpx 40rpx;
  2594. background: linear-gradient(135deg, #06DADC 0%, #04B8BA 100%);
  2595. border-radius: 50rpx;
  2596. box-shadow: 0 8rpx 20rpx rgba(6, 218, 220, 0.3);
  2597. transition: all 0.3s ease;
  2598. }
  2599. .get-audio-btn:active {
  2600. transform: scale(0.95);
  2601. box-shadow: 0 4rpx 10rpx rgba(6, 218, 220, 0.2);
  2602. }
  2603. /* 音频预加载提示样式 */
  2604. .audio-preloaded-container {
  2605. display: flex;
  2606. justify-content: center;
  2607. align-items: center;
  2608. padding: 20rpx;
  2609. transition: all 0.3s ease;
  2610. position: relative;
  2611. z-index: 10;
  2612. }
  2613. .preloaded-tip {
  2614. display: flex;
  2615. align-items: center;
  2616. justify-content: center;
  2617. gap: 16rpx;
  2618. padding: 20rpx 40rpx;
  2619. background: linear-gradient(135deg, #52c41a 0%, #389e0d 100%);
  2620. border-radius: 50rpx;
  2621. box-shadow: 0 8rpx 20rpx rgba(82, 196, 26, 0.3);
  2622. transition: all 0.3s ease;
  2623. }
  2624. .preloaded-text {
  2625. color: #ffffff;
  2626. font-size: 28rpx;
  2627. font-weight: 500;
  2628. }
  2629. /* 音频获取失败样式 */
  2630. .audio-failed-container {
  2631. display: flex;
  2632. flex-direction: column;
  2633. align-items: center;
  2634. justify-content: center;
  2635. padding: 20rpx;
  2636. gap: 20rpx;
  2637. }
  2638. .failed-tip {
  2639. display: flex;
  2640. align-items: center;
  2641. justify-content: center;
  2642. gap: 16rpx;
  2643. padding: 20rpx 40rpx;
  2644. background: linear-gradient(135deg, #ff4d4f 0%, #cf1322 100%);
  2645. border-radius: 50rpx;
  2646. box-shadow: 0 8rpx 20rpx rgba(255, 77, 79, 0.3);
  2647. }
  2648. .failed-text {
  2649. color: #ffffff;
  2650. font-size: 28rpx;
  2651. font-weight: 500;
  2652. }
  2653. .retry-btn {
  2654. display: flex;
  2655. align-items: center;
  2656. justify-content: center;
  2657. gap: 12rpx;
  2658. padding: 16rpx 32rpx;
  2659. background: linear-gradient(135deg, #06DADC 0%, #05B8BA 100%);
  2660. border-radius: 40rpx;
  2661. box-shadow: 0 6rpx 16rpx rgba(6, 218, 220, 0.3);
  2662. transition: all 0.3s ease;
  2663. }
  2664. .retry-btn:active {
  2665. transform: scale(0.95);
  2666. box-shadow: 0 4rpx 12rpx rgba(6, 218, 220, 0.4);
  2667. }
  2668. .retry-text {
  2669. color: #ffffff;
  2670. font-size: 26rpx;
  2671. font-weight: 500;
  2672. }
  2673. .get-audio-text {
  2674. font-size: 32rpx;
  2675. color: #FFFFFF;
  2676. font-weight: 500;
  2677. }
  2678. /* 会员限制容器样式 */
  2679. .member-restricted-container {
  2680. height: 0;
  2681. overflow: hidden;
  2682. opacity: 0;
  2683. pointer-events: none;
  2684. }
  2685. </style>