四零语境前端代码仓库
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.

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