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

2505 lines
83 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. <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. isMember: {
  88. type: Boolean,
  89. default: false
  90. },
  91. currentPageRequiresMember: {
  92. type: Boolean,
  93. default: false
  94. },
  95. pagePay: {
  96. type: Array,
  97. default: () => []
  98. }
  99. },
  100. data() {
  101. return {
  102. // 音频控制相关数据
  103. isPlaying: false,
  104. currentTime: 0,
  105. totalTime: 0,
  106. sliderValue: 0, // 滑動條的值
  107. isDragging: false, // 是否正在拖動滑動條
  108. isLoop: false,
  109. playSpeed: 1.0,
  110. speedOptions: [0.5, 0.8, 1.0, 1.25, 1.5, 2.0], // 根據uni-app文檔的官方支持值
  111. playbackRateSupported: true, // 播放速度控制是否支持
  112. // 音频数组管理
  113. currentPageAudios: [], // 当前页面的音频数组
  114. currentAudioIndex: 0, // 当前播放的音频索引
  115. audioContext: null, // 音频上下文
  116. currentAudio: null, // 当前音频实例
  117. // 音频缓存管理
  118. audioCache: {}, // 页面音频缓存 {pageIndex: {audios: [], totalDuration: 0}}
  119. // 预加载相关状态
  120. isPreloading: false, // 是否正在预加载
  121. preloadProgress: 0, // 预加载进度 (0-100)
  122. preloadQueue: [], // 预加载队列
  123. // 音频加载状态
  124. isAudioLoading: false, // 音频是否正在加载
  125. hasAudioData: false, // 当前页面是否已有音频数据
  126. isVoiceChanging: false, // 音色切换中的加载状态
  127. audioLoadFailed: false, // 音频获取失败状态
  128. // 文本高亮相关
  129. currentHighlightIndex: -1, // 当前高亮的文本索引
  130. // 课程切换相关状态
  131. isJustSwitchedCourse: false, // 是否刚刚切换了课程
  132. // 页面切换防抖相关
  133. pageChangeTimer: null, // 页面切换防抖定时器
  134. isPageChanging: false, // 是否正在切换页面
  135. // 请求取消相关
  136. currentRequestId: null, // 当前音频请求ID
  137. shouldCancelRequest: false, // 是否应该取消当前请求
  138. // 本地音色ID(避免直接修改prop)
  139. localVoiceId: '', // 本地音色ID,从prop初始化
  140. }
  141. },
  142. computed: {
  143. // 计算音频播放进度百分比
  144. progressPercent() {
  145. return this.totalTime > 0 ? (this.currentTime / this.totalTime) * 100 : 0;
  146. },
  147. // 检查当前页面是否有缓存的音频
  148. hasCurrentPageCache() {
  149. const cacheKey = `${this.courseId}_${this.currentPage}_${this.localVoiceId}`;
  150. const cachedData = this.audioCache[cacheKey];
  151. // 更严格的缓存有效性检查
  152. if (!cachedData || !cachedData.audios || cachedData.audios.length === 0) {
  153. return false;
  154. }
  155. // 检查缓存的音色ID是否与当前音色匹配
  156. if (cachedData.voiceId && cachedData.voiceId !== this.localVoiceId) {
  157. console.warn('缓存音色不匹配:', cachedData.voiceId, '!=', this.localVoiceId);
  158. return false;
  159. }
  160. // 检查音频URL是否有效
  161. const firstAudio = cachedData.audios[0];
  162. if (!firstAudio || !firstAudio.url) {
  163. console.warn('缓存音频数据无效');
  164. return false;
  165. }
  166. return true;
  167. },
  168. // 判断音频功能是否应该被禁用(会员限制页面且用户非会员)
  169. isAudioDisabled() {
  170. return this.currentPageRequiresMember && !this.isMember;
  171. },
  172. // 检查当前页面是否正在预加载中
  173. isCurrentPagePreloading() {
  174. // 如果全局预加载状态为true,需要检查当前页面是否在预加载队列中
  175. if (this.isPreloading) {
  176. // 检查当前页面是否有缓存(如果有缓存说明已经预加载完成)
  177. const cacheKey = `${this.courseId}_${this.currentPage}_${this.localVoiceId}`;
  178. const hasCache = this.audioCache[cacheKey] && this.audioCache[cacheKey].audios && this.audioCache[cacheKey].audios.length > 0;
  179. // 如果没有缓存且正在预加载,说明当前页面可能正在预加载中
  180. if (!hasCache) {
  181. console.log('当前页面可能正在预加载中,页面:', this.currentPage, '缓存状态:', hasCache);
  182. return true;
  183. }
  184. }
  185. return false;
  186. }
  187. },
  188. watch: {
  189. // 监听页面变化,重置音频状态
  190. currentPage: {
  191. handler(newPage, oldPage) {
  192. if (newPage !== oldPage) {
  193. console.log('页面切换:', oldPage, '->', newPage);
  194. // 设置页面切换状态
  195. this.isPageChanging = true;
  196. // 立即重置音频状态,防止穿音
  197. this.resetAudioState();
  198. // 清除之前的防抖定时器
  199. if (this.pageChangeTimer) {
  200. clearTimeout(this.pageChangeTimer);
  201. }
  202. // 使用防抖机制,避免频繁切换时重复加载
  203. this.pageChangeTimer = setTimeout(() => {
  204. this.isPageChanging = false;
  205. // 检查新页面是否有预加载完成的音频缓存
  206. this.$nextTick(() => {
  207. this.checkAndLoadPreloadedAudio();
  208. });
  209. }, 300); // 300ms防抖延迟
  210. }
  211. },
  212. immediate: false
  213. },
  214. // 监听音色变化,更新本地音色ID
  215. voiceId: {
  216. handler(newVoiceId, oldVoiceId) {
  217. if (newVoiceId !== oldVoiceId) {
  218. console.log('🎵 音色ID变化:', oldVoiceId, '->', newVoiceId);
  219. // 更新本地音色ID
  220. this.localVoiceId = newVoiceId;
  221. }
  222. },
  223. immediate: true // 立即执行,用于初始化
  224. }
  225. },
  226. methods: {
  227. // 检查并自动加载预加载完成的音频
  228. checkAndLoadPreloadedAudio() {
  229. // 只在文本页面检查
  230. if (!this.isTextPage) {
  231. return;
  232. }
  233. // 检查当前页面是否有缓存的音频数据
  234. const pageKey = `${this.courseId}_${this.currentPage}_${this.localVoiceId}`;
  235. const cachedAudio = this.audioCache[pageKey];
  236. if (cachedAudio && cachedAudio.audios && cachedAudio.audios.length > 0) {
  237. // 有缓存:直接显示控制栏
  238. this.currentPageAudios = cachedAudio.audios;
  239. this.totalTime = cachedAudio.totalDuration || 0;
  240. this.hasAudioData = true;
  241. this.isAudioLoading = false;
  242. console.log('检测到缓存音频,显示控制栏');
  243. } else {
  244. // 没有缓存:自动开始加载音频
  245. console.log('没有缓存音频,开始加载当前页面音频');
  246. this.getCurrentPageAudio();
  247. }
  248. },
  249. // 智能分割文本,按句号和逗号分割中英文文本
  250. splitTextIntelligently(text) {
  251. if (!text || typeof text !== 'string') {
  252. return [text];
  253. }
  254. // 判断是否为中文文本(包含中文字符)
  255. const isChinese = /[\u4e00-\u9fa5]/.test(text);
  256. const maxLength = isChinese ? 100 : 200;
  257. // 如果文本长度不超过限制,直接返回
  258. if (text.length <= maxLength) {
  259. return [{
  260. text: text,
  261. startIndex: 0,
  262. endIndex: text.length - 1
  263. }];
  264. }
  265. const segments = [];
  266. let currentText = text;
  267. let globalStartIndex = 0;
  268. while (currentText.length > 0) {
  269. if (currentText.length <= maxLength) {
  270. // 剩余文本不超过限制,直接添加
  271. segments.push({
  272. text: currentText,
  273. startIndex: globalStartIndex,
  274. endIndex: globalStartIndex + currentText.length - 1
  275. });
  276. break;
  277. }
  278. // 在限制长度内寻找最佳分割点
  279. let splitIndex = maxLength;
  280. let bestSplitIndex = -1;
  281. // 优先寻找句号
  282. for (let i = Math.min(maxLength, currentText.length - 1); i >= Math.max(0, maxLength - 50); i--) {
  283. const char = currentText[i];
  284. if (char === '。' || char === '.') {
  285. bestSplitIndex = i + 1; // 包含句号
  286. break;
  287. }
  288. }
  289. // 如果没找到句号,寻找逗号
  290. if (bestSplitIndex === -1) {
  291. for (let i = Math.min(maxLength, currentText.length - 1); i >= Math.max(0, maxLength - 50); i--) {
  292. const char = currentText[i];
  293. if (char === ',' || char === ',' || char === ';' || char === ';') {
  294. bestSplitIndex = i + 1; // 包含标点符号
  295. break;
  296. }
  297. }
  298. }
  299. // 如果还是没找到合适的分割点,使用默认长度
  300. if (bestSplitIndex === -1) {
  301. bestSplitIndex = maxLength;
  302. }
  303. // 提取当前段落
  304. const segment = currentText.substring(0, bestSplitIndex).trim();
  305. if (segment.length > 0) {
  306. segments.push({
  307. text: segment,
  308. startIndex: globalStartIndex,
  309. endIndex: globalStartIndex + segment.length - 1
  310. });
  311. }
  312. // 更新剩余文本和全局索引
  313. currentText = currentText.substring(bestSplitIndex).trim();
  314. globalStartIndex += bestSplitIndex;
  315. }
  316. console.log(`文本分割完成,原长度: ${text.length},分割为 ${segments.length} 段:`, segments);
  317. return segments;
  318. },
  319. // 分批次请求音频
  320. async requestAudioInBatches(text, voiceType) {
  321. const segments = this.splitTextIntelligently(text);
  322. const audioSegments = [];
  323. let totalDuration = 0;
  324. const requestId = this.currentRequestId; // 保存当前请求ID
  325. console.log(`开始分批次请求音频,共 ${segments.length} 段,请求ID: ${requestId}`);
  326. for (let i = 0; i < segments.length; i++) {
  327. // 检查是否应该取消请求
  328. if (this.shouldCancelRequest || this.currentRequestId !== requestId) {
  329. console.log(`请求已取消,停止音频加载,请求ID: ${requestId}`);
  330. return null; // 返回null表示请求被取消
  331. }
  332. const segment = segments[i];
  333. try {
  334. console.log(`请求第 ${i + 1}/${segments.length} 段音频:`, segment.text.substring(0, 50) + '...');
  335. const radioRes = await this.$api.music.textToVoice({
  336. text: segment.text,
  337. voiceType: voiceType,
  338. });
  339. if (radioRes.code === 200 && radioRes.result && radioRes.result.url) {
  340. const audioUrl = radioRes.result.url;
  341. const duration = radioRes.result.time || 0;
  342. audioSegments.push({
  343. url: audioUrl,
  344. text: segment.text,
  345. duration: duration,
  346. startIndex: segment.startIndex,
  347. endIndex: segment.endIndex,
  348. segmentIndex: i,
  349. isSegmented: segments.length > 1,
  350. originalText: text
  351. });
  352. totalDuration += duration;
  353. console.log(`${i + 1} 段音频请求成功,时长: ${duration}`);
  354. } else {
  355. console.error(`${i + 1} 段音频请求失败:`, radioRes);
  356. // 即使某段失败,也继续处理其他段
  357. audioSegments.push({
  358. url: null,
  359. text: segment.text,
  360. duration: 0,
  361. startIndex: segment.startIndex,
  362. endIndex: segment.endIndex,
  363. segmentIndex: i,
  364. error: true,
  365. isSegmented: segments.length > 1,
  366. originalText: text
  367. });
  368. }
  369. } catch (error) {
  370. console.error(`${i + 1} 段音频请求异常:`, error);
  371. audioSegments.push({
  372. url: null,
  373. text: segment.text,
  374. duration: 0,
  375. startIndex: segment.startIndex,
  376. endIndex: segment.endIndex,
  377. segmentIndex: i,
  378. error: true,
  379. isSegmented: segments.length > 1,
  380. originalText: text
  381. });
  382. }
  383. // 每个请求之间间隔400ms,避免请求过于频繁
  384. if (i < segments.length - 1) {
  385. await new Promise(resolve => setTimeout(resolve, 400));
  386. }
  387. }
  388. console.log(`分批次音频请求完成,成功 ${audioSegments.filter(s => !s.error).length}/${segments.length}`);
  389. return {
  390. audioSegments: audioSegments,
  391. totalDuration: totalDuration,
  392. originalText: text
  393. };
  394. },
  395. // 获取当前页面的音频内容
  396. async getCurrentPageAudio(autoPlay = false) {
  397. // 检查是否正在页面切换中,如果是则不加载音频
  398. if (this.isPageChanging) {
  399. console.log('页面切换中,跳过音频加载');
  400. return;
  401. }
  402. // 检查是否为卡片页面,如果是则不加载任何音频
  403. if (!this.isTextPage) {
  404. console.log('当前为卡片页面,不加载音频');
  405. // 清空音频状态
  406. this.currentPageAudios = [];
  407. this.hasAudioData = false;
  408. this.isAudioLoading = false;
  409. this.audioLoadFailed = false;
  410. this.currentAudioIndex = 0;
  411. this.currentTime = 0;
  412. this.totalTime = 0;
  413. this.currentHighlightIndex = -1;
  414. // 通知父组件音频状态变化
  415. this.$emit('audio-state-change', {
  416. hasAudioData: false,
  417. isLoading: false,
  418. currentHighlightIndex: -1
  419. });
  420. return;
  421. }
  422. // 检查会员限制
  423. if (this.isAudioDisabled) {
  424. return;
  425. }
  426. // 检查是否已经在加载中,防止重复加载(音色切换时除外)
  427. if (this.isAudioLoading && !this.isVoiceChanging) {
  428. console.log('音频已在加载中,跳过重复加载');
  429. return;
  430. }
  431. // 检查缓存中是否已有当前页面的音频数据
  432. const cacheKey = `${this.courseId}_${this.currentPage}_${this.localVoiceId}`;
  433. if (this.audioCache[cacheKey]) {
  434. console.log('从缓存加载音频数据:', cacheKey);
  435. // 从缓存加载音频数据
  436. this.currentPageAudios = this.audioCache[cacheKey].audios;
  437. this.totalTime = this.audioCache[cacheKey].totalDuration;
  438. this.currentAudioIndex = 0;
  439. this.isPlaying = false;
  440. this.currentTime = 0;
  441. this.hasAudioData = true;
  442. this.isAudioLoading = false;
  443. // 如果是课程切换后的自动加载,清除切换标识
  444. if (this.isJustSwitchedCourse) {
  445. this.isJustSwitchedCourse = false;
  446. }
  447. // 通知父组件音频状态变化
  448. this.$emit('audio-state-change', {
  449. hasAudioData: this.hasAudioData,
  450. isLoading: this.isAudioLoading,
  451. currentHighlightIndex: this.currentHighlightIndex
  452. });
  453. return;
  454. }
  455. // 开始加载状态
  456. this.isAudioLoading = true;
  457. this.hasAudioData = false;
  458. // 重置请求取消标识并生成新的请求ID
  459. this.shouldCancelRequest = false;
  460. this.currentRequestId = Date.now() + '_' + Math.random().toString(36).substr(2, 9);
  461. console.log('开始新的音频请求,ID:', this.currentRequestId);
  462. // 清空当前页面音频数组
  463. this.currentPageAudios = [];
  464. this.currentAudioIndex = 0;
  465. this.isPlaying = false;
  466. this.currentTime = 0;
  467. this.totalTime = 0;
  468. // 通知父组件开始加载
  469. this.$emit('audio-state-change', {
  470. hasAudioData: this.hasAudioData,
  471. isLoading: this.isAudioLoading,
  472. currentHighlightIndex: this.currentHighlightIndex
  473. });
  474. try {
  475. // 对着当前页面的每一个[]元素进行切割 如果是文本text类型则进行音频请求
  476. const currentPageData = this.bookPages[this.currentPage - 1];
  477. if (currentPageData) {
  478. // 收集所有text类型的元素
  479. const textItems = currentPageData.filter(item => item.type === 'text');
  480. if (textItems.length > 0) {
  481. let firstAudioPlayed = false; // 标记是否已播放第一个音频
  482. let loadedAudiosCount = 0; // 已加载的音频数量
  483. // 逐个处理文本项,支持长文本分割
  484. for (let index = 0; index < textItems.length; index++) {
  485. const item = textItems[index];
  486. try {
  487. console.log(`处理第 ${index + 1}/${textItems.length} 个文本项,长度: ${item.content.length}`);
  488. // 使用分批次请求音频
  489. console.log('🎵 请求音频 - 当前音色ID:', this.localVoiceId);
  490. const batchResult = await this.requestAudioInBatches(item.content, this.localVoiceId);
  491. // 检查请求是否被取消
  492. if (batchResult === null) {
  493. console.log('音频请求被取消,停止处理');
  494. return;
  495. }
  496. if (batchResult.audioSegments.length > 0) {
  497. // 同时保存到原始数据中以保持兼容性(使用第一段的URL)
  498. const firstValidSegment = batchResult.audioSegments.find(seg => !seg.error);
  499. if (firstValidSegment) {
  500. item.audioUrl = firstValidSegment.url;
  501. }
  502. // 将所有音频段添加到音频数组
  503. for (const segment of batchResult.audioSegments) {
  504. if (!segment.error) {
  505. const audioData = {
  506. url: segment.url,
  507. text: segment.text,
  508. duration: segment.duration,
  509. startIndex: segment.startIndex,
  510. endIndex: segment.endIndex,
  511. segmentIndex: segment.segmentIndex,
  512. originalTextIndex: index, // 标记属于哪个原始文本项
  513. isSegmented: batchResult.audioSegments.length > 1 // 标记是否为分段音频
  514. };
  515. this.currentPageAudios.push(audioData);
  516. loadedAudiosCount++;
  517. }
  518. }
  519. // 如果是第一个音频,立即开始播放
  520. if (!firstAudioPlayed && this.currentPageAudios.length > 0) {
  521. firstAudioPlayed = true;
  522. this.hasAudioData = true;
  523. this.currentAudioIndex = 0;
  524. console.log(`第一个音频时长: ${this.currentPageAudios[0].duration}`);
  525. // 通知父组件有音频数据了,但仍在加载中
  526. this.$emit('audio-state-change', {
  527. hasAudioData: this.hasAudioData,
  528. isLoading: this.isAudioLoading, // 保持加载状态
  529. currentHighlightIndex: this.currentHighlightIndex
  530. });
  531. // 立即创建音频实例并开始播放
  532. console.log('创建第一个音频实例并开始播放');
  533. this.createAudioInstance();
  534. // 等待音频实例准备好后开始播放(仅在非音色切换时或明确要求自动播放时)
  535. setTimeout(() => {
  536. if (this.currentAudio && !this.isPlaying && (!this.isVoiceChanging || autoPlay)) {
  537. if (autoPlay || !this.isVoiceChanging) {
  538. console.log('自动播放第一个音频,autoPlay:', autoPlay);
  539. this.currentAudio.play();
  540. // isPlaying状态会在onPlay事件中自动设置
  541. this.updateHighlightIndex();
  542. }
  543. }
  544. }, 100);
  545. }
  546. console.log(`文本项 ${index + 1} 处理完成,获得 ${batchResult.audioSegments.filter(s => !s.error).length} 个音频段`);
  547. } else {
  548. console.error(`文本项 ${index + 1} 音频请求全部失败`);
  549. }
  550. } catch (error) {
  551. console.error(`文本项 ${index + 1} 处理异常:`, error);
  552. }
  553. }
  554. console.log('所有音频请求完成,共获取', this.currentPageAudios.length, '个音频');
  555. // 如果有音频,重新计算精确的总时长
  556. if (this.currentPageAudios.length > 0) {
  557. await this.calculateTotalDuration();
  558. // 将音频数据保存到缓存中
  559. const cacheKey = `${this.courseId}_${this.currentPage}_${this.localVoiceId}`;
  560. this.audioCache[cacheKey] = {
  561. audios: [...this.currentPageAudios], // 深拷贝音频数组
  562. totalDuration: this.totalTime,
  563. voiceId: this.localVoiceId, // 保存音色ID用于验证
  564. timestamp: Date.now() // 保存时间戳
  565. };
  566. console.log('音频数据已缓存:', cacheKey, this.audioCache[cacheKey]);
  567. // 限制缓存大小
  568. this.limitCacheSize(10);
  569. }
  570. }
  571. }
  572. // 结束加载状态
  573. this.isAudioLoading = false;
  574. this.isVoiceChanging = false; // 清除音色切换加载状态
  575. // 如果是课程切换后的自动加载,清除切换标识
  576. if (this.isJustSwitchedCourse) {
  577. this.isJustSwitchedCourse = false;
  578. }
  579. // 设置音频数据状态和失败状态
  580. this.hasAudioData = this.currentPageAudios.length > 0;
  581. this.audioLoadFailed = !this.hasAudioData && this.isTextPage; // 如果是文本页面但没有音频数据,则认为获取失败
  582. // 通知父组件音频状态变化
  583. this.$emit('audio-state-change', {
  584. hasAudioData: this.hasAudioData,
  585. isLoading: this.isAudioLoading,
  586. audioLoadFailed: this.audioLoadFailed,
  587. currentHighlightIndex: this.currentHighlightIndex
  588. });
  589. } catch (error) {
  590. console.error('getCurrentPageAudio 方法执行异常:', error);
  591. // 确保在异常情况下重置加载状态
  592. this.isAudioLoading = false;
  593. this.isVoiceChanging = false;
  594. this.audioLoadFailed = true;
  595. this.hasAudioData = false;
  596. // 通知父组件音频加载失败
  597. this.$emit('audio-state-change', {
  598. hasAudioData: false,
  599. isLoading: false,
  600. audioLoadFailed: true,
  601. currentHighlightIndex: this.currentHighlightIndex
  602. });
  603. // 显示错误提示
  604. uni.showToast({
  605. title: '音频加载失败,请重试',
  606. icon: 'none',
  607. duration: 2000
  608. });
  609. }
  610. },
  611. // 重新获取音频
  612. retryGetAudio() {
  613. console.log('用户点击重新获取音频');
  614. // 检查是否为卡片页面
  615. if (!this.isTextPage) {
  616. console.log('卡片页面不支持音频功能');
  617. return;
  618. }
  619. // 重置失败状态
  620. this.audioLoadFailed = false;
  621. // 清除当前页面的音频缓存
  622. const pageKey = `${this.courseId}_${this.currentPage}_${this.localVoiceId}`;
  623. if (this.audioCache[pageKey]) {
  624. delete this.audioCache[pageKey];
  625. console.log('清除失败页面的音频缓存:', pageKey);
  626. }
  627. // 重新获取音频
  628. this.getCurrentPageAudio();
  629. },
  630. // 重置音频状态
  631. resetAudioState() {
  632. // 取消当前正在进行的音频请求
  633. this.shouldCancelRequest = true;
  634. console.log('页面切换:标记取消当前音频请求');
  635. // 立即停止并销毁当前音频实例,防止穿音
  636. if (this.currentAudio) {
  637. if (this.isPlaying) {
  638. this.currentAudio.pause();
  639. console.log('页面切换:立即停止音频播放');
  640. }
  641. this.currentAudio.destroy();
  642. this.currentAudio = null;
  643. console.log('页面切换:销毁音频实例');
  644. }
  645. // 重置播放状态
  646. this.currentAudioIndex = 0;
  647. this.isPlaying = false;
  648. this.currentTime = 0;
  649. this.totalTime = 0;
  650. this.sliderValue = 0;
  651. this.isAudioLoading = false;
  652. this.audioLoadFailed = false;
  653. this.currentHighlightIndex = -1;
  654. this.playSpeed = 1.0;
  655. // 如果是卡片页面,直接重置为初始状态
  656. if (!this.isTextPage) {
  657. console.log('卡片页面:重置音频状态');
  658. this.currentPageAudios = [];
  659. this.totalTime = 0;
  660. this.hasAudioData = false;
  661. } else {
  662. // 文本页面:检查是否有缓存的音频数据
  663. const pageKey = `${this.courseId}_${this.currentPage}_${this.localVoiceId}`;
  664. const cachedAudio = this.audioCache[pageKey];
  665. if (cachedAudio && cachedAudio.audios && cachedAudio.audios.length > 0) {
  666. // 有缓存:直接显示控制栏
  667. this.currentPageAudios = cachedAudio.audios;
  668. this.totalTime = cachedAudio.totalDuration || 0;
  669. this.hasAudioData = true;
  670. console.log('检测到缓存音频,显示控制栏');
  671. } else {
  672. // 没有缓存:重置为初始状态,但不立即加载(由checkAndLoadPreloadedAudio处理)
  673. this.currentPageAudios = [];
  674. this.totalTime = 0;
  675. this.hasAudioData = false;
  676. console.log('没有缓存音频,重置状态');
  677. }
  678. }
  679. // 通知父组件音频状态变化
  680. this.$emit('audio-state-change', {
  681. hasAudioData: this.hasAudioData,
  682. isLoading: this.isAudioLoading,
  683. currentHighlightIndex: this.currentHighlightIndex
  684. });
  685. },
  686. // 加载缓存的音频数据并显示播放控制栏
  687. loadCachedAudioData() {
  688. const cacheKey = `${this.courseId}_${this.currentPage}_${this.localVoiceId}`;
  689. const cachedData = this.audioCache[cacheKey];
  690. // 严格验证缓存数据
  691. if (!cachedData || !cachedData.audios || cachedData.audios.length === 0) {
  692. console.warn('缓存数据不存在或为空:', cacheKey);
  693. uni.showToast({
  694. title: '缓存音频数据不存在',
  695. icon: 'none'
  696. });
  697. return;
  698. }
  699. // 检查音色ID是否匹配
  700. if (cachedData.voiceId && cachedData.voiceId !== this.localVoiceId) {
  701. console.warn('缓存音色不匹配:', cachedData.voiceId, '!=', this.localVoiceId);
  702. uni.showToast({
  703. title: '音色已切换,请重新获取音频',
  704. icon: 'none'
  705. });
  706. return;
  707. }
  708. // 检查音频URL是否有效
  709. const firstAudio = cachedData.audios[0];
  710. if (!firstAudio || !firstAudio.url) {
  711. console.warn('缓存音频URL无效');
  712. uni.showToast({
  713. title: '缓存音频数据损坏',
  714. icon: 'none'
  715. });
  716. return;
  717. }
  718. console.log('手动加载缓存音频数据:', cacheKey);
  719. // 从缓存加载音频数据
  720. this.currentPageAudios = cachedData.audios;
  721. this.totalTime = cachedData.totalDuration || 0;
  722. this.currentAudioIndex = 0;
  723. this.isPlaying = false;
  724. this.currentTime = 0;
  725. this.hasAudioData = true;
  726. this.isAudioLoading = false;
  727. this.audioLoadFailed = false;
  728. this.currentHighlightIndex = -1;
  729. // 通知父组件音频状态变化
  730. this.$emit('audio-state-change', {
  731. hasAudioData: this.hasAudioData,
  732. isLoading: this.isAudioLoading,
  733. currentHighlightIndex: this.currentHighlightIndex
  734. });
  735. console.log('缓存音频加载完成,播放控制栏已显示');
  736. },
  737. // 手动获取音频
  738. async handleGetAudio() {
  739. // 检查会员限制
  740. if (this.isAudioDisabled) {
  741. return;
  742. }
  743. // 检查是否有音色ID
  744. if (!this.localVoiceId) {
  745. uni.showToast({
  746. title: '音色未加载,请稍后重试',
  747. icon: 'none'
  748. });
  749. return;
  750. }
  751. // 检查当前页面是否有文本内容
  752. if (!this.isTextPage) {
  753. uni.showToast({
  754. title: '当前页面没有文本内容',
  755. icon: 'none'
  756. });
  757. return;
  758. }
  759. // 检查是否正在加载
  760. if (this.isAudioLoading) {
  761. return;
  762. }
  763. // 调用获取音频方法
  764. await this.getCurrentPageAudio();
  765. },
  766. // 计算音频总时长
  767. async calculateTotalDuration() {
  768. let totalDuration = 0;
  769. for (let i = 0; i < this.currentPageAudios.length; i++) {
  770. const audio = this.currentPageAudios[i];
  771. // 使用API返回的准确时长信息
  772. if (audio.duration && audio.duration > 0) {
  773. console.log(`使用API返回的准确时长 ${i + 1}:`, audio.duration, '秒');
  774. totalDuration += audio.duration;
  775. } else {
  776. // 如果没有时长信息,使用文字长度估算(备用方案)
  777. const textLength = audio.text.length;
  778. // 假设较快语速每分钟约300个字符,即每秒约5个字符
  779. const estimatedDuration = Math.max(1, textLength / 5);
  780. audio.duration = estimatedDuration;
  781. totalDuration += estimatedDuration;
  782. console.log(`备用估算音频时长 ${i + 1}:`, estimatedDuration, '秒 (文字长度:', textLength, ')');
  783. }
  784. }
  785. this.totalTime = totalDuration;
  786. console.log('音频总时长:', totalDuration, '秒');
  787. },
  788. // 获取音频时长
  789. getAudioDuration(audioUrl) {
  790. return new Promise((resolve, reject) => {
  791. const audio = uni.createInnerAudioContext();
  792. audio.src = audioUrl;
  793. let resolved = false;
  794. // 监听音频加载完成事件
  795. audio.onCanplay(() => {
  796. console.log('音频可以播放,duration:', audio.duration);
  797. if (!resolved && audio.duration && audio.duration > 0) {
  798. resolved = true;
  799. resolve(audio.duration);
  800. audio.destroy();
  801. }
  802. });
  803. // 监听音频元数据加载完成事件
  804. audio.onLoadedmetadata = () => {
  805. console.log('音频元数据加载完成,duration:', audio.duration);
  806. if (!resolved && audio.duration && audio.duration > 0) {
  807. resolved = true;
  808. resolve(audio.duration);
  809. audio.destroy();
  810. }
  811. };
  812. // 监听音频时长更新事件
  813. audio.onDurationChange = () => {
  814. console.log('音频时长更新,duration:', audio.duration);
  815. if (!resolved && audio.duration && audio.duration > 0) {
  816. resolved = true;
  817. resolve(audio.duration);
  818. audio.destroy();
  819. }
  820. };
  821. // 移除onPlay監聽器,避免意外播放
  822. audio.onError((error) => {
  823. console.error('音频加载失败:', error);
  824. if (!resolved) {
  825. resolved = true;
  826. reject(error);
  827. audio.destroy();
  828. }
  829. });
  830. // 設置較長的超時時間,但不播放音頻
  831. setTimeout(() => {
  832. if (!resolved) {
  833. console.log('無法獲取音頻時長,使用默認值');
  834. resolved = true;
  835. reject(new Error('無法獲取音頻時長'));
  836. audio.destroy();
  837. }
  838. }, 1000);
  839. // 最終超時處理
  840. setTimeout(() => {
  841. if (!resolved) {
  842. console.warn('獲取音頻時長超時,使用默認值');
  843. resolved = true;
  844. reject(new Error('获取音频时长超时'));
  845. audio.destroy();
  846. }
  847. }, 5000);
  848. });
  849. },
  850. // 音频控制方法
  851. togglePlay() {
  852. // 检查会员限制
  853. if (this.isAudioDisabled) {
  854. return;
  855. }
  856. if (this.currentPageAudios.length === 0) {
  857. uni.showToast({
  858. title: '当前页面没有音频内容',
  859. icon: 'none'
  860. });
  861. return;
  862. }
  863. if (this.isPlaying) {
  864. this.pauseAudio();
  865. } else {
  866. this.playAudio();
  867. }
  868. },
  869. // 播放音频
  870. playAudio() {
  871. // 检查会员限制
  872. if (this.isAudioDisabled) {
  873. return;
  874. }
  875. if (this.currentPageAudios.length === 0) return;
  876. // 如果没有当前音频实例或者需要切换音频
  877. if (!this.currentAudio || this.currentAudio.src !== this.currentPageAudios[this.currentAudioIndex].url) {
  878. this.createAudioInstance();
  879. // 创建实例后稍等一下再播放,确保音频准备就绪
  880. setTimeout(() => {
  881. if (this.currentAudio) {
  882. this.currentAudio.play();
  883. // isPlaying状态会在onPlay事件中自动设置
  884. this.updateHighlightIndex();
  885. }
  886. }, 50);
  887. } else {
  888. // 音频实例已存在,直接播放
  889. this.currentAudio.play();
  890. // isPlaying状态会在onPlay事件中自动设置
  891. this.updateHighlightIndex();
  892. }
  893. },
  894. // 暂停音频
  895. pauseAudio() {
  896. if (this.currentAudio) {
  897. this.currentAudio.pause();
  898. }
  899. this.isPlaying = false;
  900. // 暂停时清除高亮
  901. this.currentHighlightIndex = -1;
  902. // 通知父组件高亮状态变化
  903. this.emitHighlightChange(-1);
  904. },
  905. // 播放指定的音频段落(通过文本内容匹配)
  906. playSpecificAudio(textContent) {
  907. console.log('尝试播放指定音频段落:', textContent);
  908. // 检查textContent是否有效
  909. if (!textContent || typeof textContent !== 'string') {
  910. console.log('无效的文本内容:', textContent);
  911. uni.showToast({
  912. title: '无效的文本内容',
  913. icon: 'none'
  914. });
  915. return false;
  916. }
  917. // 在当前页面音频数组中查找匹配的音频
  918. let audioIndex = this.currentPageAudios.findIndex(audio =>
  919. audio.text && audio.text.trim() === textContent.trim()
  920. );
  921. // 如果直接匹配失败,尝试匹配分段音频
  922. if (audioIndex === -1) {
  923. console.log('直接匹配失败,尝试匹配分段音频');
  924. // 查找包含该文本的分段音频
  925. audioIndex = this.currentPageAudios.findIndex(audio => {
  926. if (!audio.text) return false;
  927. // 检查是否为分段音频,且原始文本包含目标文本
  928. if (audio.isSegmented && audio.originalText) {
  929. return audio.originalText.trim() === textContent.trim();
  930. }
  931. // 检查目标文本是否包含在当前音频文本中,或者当前音频文本包含在目标文本中
  932. const audioText = audio.text.trim();
  933. const targetText = textContent.trim();
  934. return audioText.includes(targetText) || targetText.includes(audioText);
  935. });
  936. if (audioIndex !== -1) {
  937. console.log('找到分段音频匹配,索引:', audioIndex);
  938. }
  939. }
  940. if (audioIndex !== -1) {
  941. console.log('找到匹配的音频,索引:', audioIndex);
  942. // 停止当前播放的音频
  943. if (this.currentAudio) {
  944. this.currentAudio.pause();
  945. this.currentAudio.destroy();
  946. this.currentAudio = null;
  947. }
  948. // 设置新的音频索引
  949. this.currentAudioIndex = audioIndex;
  950. // 重置播放状态
  951. this.isPlaying = false;
  952. this.currentTime = 0;
  953. this.sliderValue = 0;
  954. // 更新高亮索引
  955. this.currentHighlightIndex = audioIndex;
  956. this.emitHighlightChange(audioIndex);
  957. // 创建新的音频实例并播放
  958. this.createAudioInstance();
  959. // 稍等一下再播放,确保音频准备就绪
  960. setTimeout(() => {
  961. if (this.currentAudio) {
  962. this.currentAudio.play();
  963. console.log('开始播放指定音频段落');
  964. }
  965. }, 100);
  966. return true; // 成功找到并播放
  967. } else {
  968. console.log('未找到匹配的音频段落:', textContent);
  969. uni.showToast({
  970. title: '未找到对应的音频',
  971. icon: 'none'
  972. });
  973. return false; // 未找到匹配的音频
  974. }
  975. },
  976. // 创建音频实例
  977. createAudioInstance() {
  978. // 销毁之前的音频实例
  979. if (this.currentAudio) {
  980. this.currentAudio.pause();
  981. this.currentAudio.destroy();
  982. this.currentAudio = null;
  983. }
  984. // 重置播放状态
  985. this.isPlaying = false;
  986. // 優先使用微信原生API以支持playbackRate
  987. let audio;
  988. if (typeof wx !== 'undefined' && wx.createInnerAudioContext) {
  989. console.log('使用微信原生音頻API');
  990. audio = wx.createInnerAudioContext();
  991. } else {
  992. console.log('使用uni-app音頻API');
  993. audio = uni.createInnerAudioContext();
  994. }
  995. const audioUrl = this.currentPageAudios[this.currentAudioIndex].url;
  996. audio.src = audioUrl;
  997. console.log('🎵 创建音频实例 - 当前音色ID:', this.voiceId);
  998. console.log('🎵 创建音频实例 - 音频URL:', audioUrl);
  999. console.log('🎵 创建音频实例 - 音频索引:', this.currentAudioIndex);
  1000. console.log('🎵 创建音频实例 - 音频文本:', this.currentPageAudios[this.currentAudioIndex].text?.substring(0, 50) + '...');
  1001. // 在音頻可以播放時檢測playbackRate支持
  1002. audio.onCanplay(() => {
  1003. console.log('🎵 音頻可以播放,開始檢測playbackRate支持');
  1004. this.checkPlaybackRateSupport(audio);
  1005. // 檢測完成後,設置用戶期望的播放速度
  1006. setTimeout(() => {
  1007. if (this.playbackRateSupported) {
  1008. console.log('設置播放速度:', this.playSpeed);
  1009. audio.playbackRate = this.playSpeed;
  1010. // 驗證設置結果
  1011. setTimeout(() => {
  1012. console.log('最終播放速度:', audio.playbackRate);
  1013. if (Math.abs(audio.playbackRate - this.playSpeed) > 0.01) {
  1014. console.log('⚠️ 播放速度設置可能未生效');
  1015. } else {
  1016. console.log('✅ 播放速度設置成功');
  1017. }
  1018. }, 50);
  1019. } else {
  1020. console.log('❌ 當前環境不支持播放速度控制');
  1021. }
  1022. }, 50);
  1023. });
  1024. // 音频事件监听
  1025. audio.onPlay(() => {
  1026. console.log('音频开始播放');
  1027. this.isPlaying = true;
  1028. });
  1029. audio.onPause(() => {
  1030. console.log('音频暂停');
  1031. this.isPlaying = false;
  1032. });
  1033. audio.onTimeUpdate(() => {
  1034. this.updateCurrentTime();
  1035. });
  1036. audio.onEnded(() => {
  1037. console.log('当前音频播放结束');
  1038. this.onAudioEnded();
  1039. });
  1040. audio.onError((error) => {
  1041. console.error('音频播放错误:', error);
  1042. this.isPlaying = false;
  1043. uni.showToast({
  1044. title: '音频播放失败',
  1045. icon: 'none'
  1046. });
  1047. });
  1048. this.currentAudio = audio;
  1049. },
  1050. // 更新当前播放时间
  1051. updateCurrentTime() {
  1052. if (!this.currentAudio) return;
  1053. let totalTime = 0;
  1054. // 计算之前音频的总时长
  1055. for (let i = 0; i < this.currentAudioIndex; i++) {
  1056. totalTime += this.currentPageAudios[i].duration;
  1057. }
  1058. // 加上当前音频的播放时间
  1059. totalTime += this.currentAudio.currentTime;
  1060. this.currentTime = totalTime;
  1061. // 如果不是正在拖動滑動條,則同步更新滑動條的值
  1062. if (!this.isDragging) {
  1063. this.sliderValue = this.currentTime;
  1064. }
  1065. // 更新当前高亮的文本索引
  1066. this.updateHighlightIndex();
  1067. },
  1068. // 更新高亮文本索引
  1069. updateHighlightIndex() {
  1070. if (!this.isPlaying || this.currentPageAudios.length === 0) {
  1071. this.currentHighlightIndex = -1;
  1072. this.emitHighlightChange(-1);
  1073. return;
  1074. }
  1075. // 获取当前播放的音频数据
  1076. const currentAudio = this.currentPageAudios[this.currentAudioIndex];
  1077. if (!currentAudio) {
  1078. this.currentHighlightIndex = -1;
  1079. this.emitHighlightChange(-1);
  1080. return;
  1081. }
  1082. // 如果是分段音频,需要计算正确的高亮索引
  1083. if (currentAudio.isSegmented && typeof currentAudio.originalTextIndex !== 'undefined') {
  1084. // 使用原始文本项的索引作为高亮索引
  1085. this.currentHighlightIndex = currentAudio.originalTextIndex;
  1086. console.log('分段音频高亮索引:', this.currentHighlightIndex, '原始文本索引:', currentAudio.originalTextIndex, '段索引:', currentAudio.segmentIndex);
  1087. } else {
  1088. // 非分段音频,使用音频索引
  1089. this.currentHighlightIndex = this.currentAudioIndex;
  1090. console.log('普通音频高亮索引:', this.currentHighlightIndex, '当前音频索引:', this.currentAudioIndex);
  1091. }
  1092. // 使用辅助方法发送高亮变化事件
  1093. this.emitHighlightChange(this.currentAudioIndex);
  1094. },
  1095. // 发送高亮变化事件的辅助方法
  1096. emitHighlightChange(highlightIndex = -1) {
  1097. if (highlightIndex === -1) {
  1098. // 清除高亮
  1099. this.$emit('highlight-change', -1);
  1100. return;
  1101. }
  1102. // 获取对应的音频数据
  1103. const audioData = this.currentPageAudios[highlightIndex];
  1104. if (!audioData) {
  1105. this.$emit('highlight-change', -1);
  1106. return;
  1107. }
  1108. // 发送详细的高亮信息
  1109. this.$emit('highlight-change', {
  1110. highlightIndex: audioData.originalTextIndex !== undefined ? audioData.originalTextIndex : highlightIndex,
  1111. isSegmented: audioData.isSegmented || false,
  1112. segmentIndex: audioData.segmentIndex || 0,
  1113. startIndex: audioData.startIndex || 0,
  1114. endIndex: audioData.endIndex || 0,
  1115. currentText: audioData.text || ''
  1116. });
  1117. },
  1118. // 音频播放结束处理
  1119. onAudioEnded() {
  1120. if (this.currentAudioIndex < this.currentPageAudios.length - 1) {
  1121. // 播放下一个音频
  1122. this.currentAudioIndex++;
  1123. this.playAudio();
  1124. } else {
  1125. // 所有音频播放完毕
  1126. if (this.isLoop) {
  1127. // 循环播放
  1128. this.currentAudioIndex = 0;
  1129. this.playAudio();
  1130. } else {
  1131. // 停止播放
  1132. this.isPlaying = false;
  1133. this.currentTime = this.totalTime;
  1134. this.currentHighlightIndex = -1;
  1135. this.$emit('highlight-change', -1);
  1136. }
  1137. }
  1138. },
  1139. toggleLoop() {
  1140. this.isLoop = !this.isLoop;
  1141. },
  1142. toggleSpeed() {
  1143. // 檢查是否支持播放速度控制
  1144. if (!this.playbackRateSupported) {
  1145. uni.showToast({
  1146. title: '當前設備不支持播放速度控制',
  1147. icon: 'none',
  1148. duration: 2000
  1149. });
  1150. return;
  1151. }
  1152. const currentIndex = this.speedOptions.indexOf(this.playSpeed);
  1153. const nextIndex = (currentIndex + 1) % this.speedOptions.length;
  1154. const oldSpeed = this.playSpeed;
  1155. this.playSpeed = this.speedOptions[nextIndex];
  1156. console.log(`播放速度切換: ${oldSpeed}x -> ${this.playSpeed}x`);
  1157. // 如果当前有音频在播放,更新播放速度
  1158. if (this.currentAudio) {
  1159. const wasPlaying = this.isPlaying;
  1160. const currentTime = this.currentAudio.currentTime;
  1161. // 設置新的播放速度
  1162. this.currentAudio.playbackRate = this.playSpeed;
  1163. // 如果正在播放,需要重啟播放才能使播放速度生效
  1164. if (wasPlaying) {
  1165. this.currentAudio.pause();
  1166. setTimeout(() => {
  1167. this.currentAudio.seek(currentTime);
  1168. this.currentAudio.play();
  1169. }, 50);
  1170. }
  1171. console.log('音頻實例播放速度已更新為:', this.currentAudio.playbackRate);
  1172. // 顯示速度變更提示
  1173. uni.showToast({
  1174. title: `播放速度: ${this.playSpeed}x`,
  1175. icon: 'none',
  1176. duration: 1000
  1177. });
  1178. }
  1179. },
  1180. // 滑動條值實時更新 (@input 事件)
  1181. onSliderInput(value) {
  1182. // 在拖動過程中實時更新顯示的時間,但不影響實際播放
  1183. if (this.isDragging) {
  1184. // 可以在這裡實時更新顯示時間,讓用戶看到拖動到的時間點
  1185. // 但不改變實際的 currentTime,避免影響播放邏輯
  1186. console.log('實時更新滑動條值:', value);
  1187. }
  1188. },
  1189. // 滑動條拖動過程中的處理 (@changing 事件)
  1190. onSliderChanging(value) {
  1191. // 第一次觸發 changing 事件時,暫停播放並標記為拖動狀態
  1192. if (!this.isDragging) {
  1193. if (this.isPlaying) {
  1194. this.pauseAudio();
  1195. console.log('開始拖動滑動條,暫停播放');
  1196. }
  1197. this.isDragging = true;
  1198. }
  1199. // 更新滑動條的值,但不改變實際播放位置
  1200. this.sliderValue = value;
  1201. console.log('拖動中,滑動條值:', value);
  1202. },
  1203. // 滑動條拖動結束的處理 (@change 事件)
  1204. onSliderChange(value) {
  1205. console.log('滑動條變化,跳轉到位置:', value, '是否為拖動:', this.isDragging);
  1206. // 如果不是拖動狀態(即單點),需要先暫停播放
  1207. if (!this.isDragging && this.isPlaying) {
  1208. this.pauseAudio();
  1209. console.log('單點滑動條,暫停播放');
  1210. }
  1211. // 重置拖動狀態
  1212. this.isDragging = false;
  1213. this.sliderValue = value;
  1214. // 跳轉到指定位置,但不自動恢復播放
  1215. this.seekToTime(value, false);
  1216. console.log('滑動條操作完成,保持暫停狀態,需要手動點擊播放');
  1217. },
  1218. // 跳轉到指定時間
  1219. seekToTime(targetTime, shouldResume = false) {
  1220. if (!this.currentPageAudios || this.currentPageAudios.length === 0) {
  1221. console.log('沒有音頻數據,無法跳轉');
  1222. return;
  1223. }
  1224. // 確保目標時間在有效範圍內
  1225. targetTime = Math.max(0, Math.min(targetTime, this.totalTime));
  1226. console.log('跳轉到時間:', targetTime, '秒', '總時長:', this.totalTime, '是否恢復播放:', shouldResume);
  1227. let accumulatedTime = 0;
  1228. let targetAudioIndex = -1;
  1229. let targetAudioTime = 0;
  1230. // 找到目標時間對應的音頻片段
  1231. for (let i = 0; i < this.currentPageAudios.length; i++) {
  1232. const audioDuration = this.currentPageAudios[i].duration || 0;
  1233. console.log(`音頻片段 ${i}: 時長=${audioDuration}, 累計時間=${accumulatedTime}, 範圍=[${accumulatedTime}, ${accumulatedTime + audioDuration}]`);
  1234. if (targetTime >= accumulatedTime && targetTime <= accumulatedTime + audioDuration) {
  1235. targetAudioIndex = i;
  1236. targetAudioTime = targetTime - accumulatedTime;
  1237. break;
  1238. }
  1239. accumulatedTime += audioDuration;
  1240. }
  1241. // 如果沒有找到合適的音頻片段,使用最後一個
  1242. if (targetAudioIndex === -1 && this.currentPageAudios.length > 0) {
  1243. targetAudioIndex = this.currentPageAudios.length - 1;
  1244. targetAudioTime = this.currentPageAudios[targetAudioIndex].duration || 0;
  1245. console.log('使用最後一個音頻片段作為目標');
  1246. }
  1247. console.log('目標音頻索引:', targetAudioIndex, '目標音頻時間:', targetAudioTime);
  1248. if (targetAudioIndex === -1) {
  1249. console.error('無法找到目標音頻片段');
  1250. return;
  1251. }
  1252. // 如果需要切換到不同的音頻片段
  1253. if (targetAudioIndex !== this.currentAudioIndex) {
  1254. console.log(`切換音頻片段: ${this.currentAudioIndex} -> ${targetAudioIndex}`);
  1255. this.currentAudioIndex = targetAudioIndex;
  1256. this.createAudioInstance();
  1257. // 等待音頻實例創建完成後再跳轉
  1258. this.waitForAudioReady(() => {
  1259. if (this.currentAudio) {
  1260. this.currentAudio.seek(targetAudioTime);
  1261. this.currentTime = targetTime;
  1262. console.log('切換音頻並跳轉到:', targetAudioTime, '秒');
  1263. // 如果拖動前正在播放,則恢復播放
  1264. if (shouldResume) {
  1265. this.currentAudio.play();
  1266. this.isPlaying = true;
  1267. console.log('恢復播放狀態');
  1268. }
  1269. }
  1270. });
  1271. } else {
  1272. // 在當前音頻片段內跳轉
  1273. if (this.currentAudio) {
  1274. this.currentAudio.seek(targetAudioTime);
  1275. this.currentTime = targetTime;
  1276. console.log('在當前音頻內跳轉到:', targetAudioTime, '秒');
  1277. // 如果拖動前正在播放,則恢復播放
  1278. if (shouldResume) {
  1279. this.currentAudio.play();
  1280. this.isPlaying = true;
  1281. console.log('恢復播放狀態');
  1282. }
  1283. }
  1284. }
  1285. },
  1286. // 等待音頻實例準備就緒
  1287. waitForAudioReady(callback, maxAttempts = 10, currentAttempt = 0) {
  1288. if (currentAttempt >= maxAttempts) {
  1289. console.error('音頻實例準備超時');
  1290. return;
  1291. }
  1292. if (this.currentAudio && this.currentAudio.src) {
  1293. // 音頻實例已準備好
  1294. setTimeout(callback, 50); // 稍微延遲確保完全準備好
  1295. } else {
  1296. // 繼續等待
  1297. setTimeout(() => {
  1298. this.waitForAudioReady(callback, maxAttempts, currentAttempt + 1);
  1299. }, 100);
  1300. }
  1301. },
  1302. // 初始檢測播放速度支持(不依賴音頻實例)
  1303. checkInitialPlaybackRateSupport() {
  1304. try {
  1305. const systemInfo = uni.getSystemInfoSync();
  1306. console.log('初始檢測 - 系統信息:', systemInfo);
  1307. // 檢查基礎庫版本 - playbackRate需要2.11.0及以上
  1308. const SDKVersion = systemInfo.SDKVersion || '0.0.0';
  1309. const versionArray = SDKVersion.split('.').map(v => parseInt(v));
  1310. const isVersionSupported = versionArray[0] > 2 ||
  1311. (versionArray[0] === 2 && versionArray[1] > 11) ||
  1312. (versionArray[0] === 2 && versionArray[1] === 11 && versionArray[2] >= 0);
  1313. if (!isVersionSupported) {
  1314. this.playbackRateSupported = false;
  1315. console.log(`初始檢測 - 基礎庫版本過低 (${SDKVersion}),需要2.11.0及以上才支持播放速度控制`);
  1316. return;
  1317. }
  1318. // Android 6以下版本不支持
  1319. if (systemInfo.platform === 'android') {
  1320. const androidVersion = systemInfo.system.match(/Android (\d+)/);
  1321. if (androidVersion && parseInt(androidVersion[1]) < 6) {
  1322. this.playbackRateSupported = false;
  1323. console.log('初始檢測 - Android版本過低,需要Android 6及以上才支持播放速度控制');
  1324. return;
  1325. }
  1326. }
  1327. // 檢查微信原生API是否可用
  1328. if (typeof wx === 'undefined' || !wx.createInnerAudioContext) {
  1329. console.log('初始檢測 - 微信原生API不可用,可能影響playbackRate支持');
  1330. } else {
  1331. console.log('初始檢測 - 微信原生API可用');
  1332. }
  1333. // 如果通過基本檢測,暫時設為支持,等音頻實例創建後再詳細檢測
  1334. this.playbackRateSupported = true;
  1335. console.log('初始檢測 - 基本條件滿足,等待音頻實例檢測');
  1336. } catch (error) {
  1337. console.error('初始檢測播放速度支持時出錯:', error);
  1338. this.playbackRateSupported = false;
  1339. }
  1340. },
  1341. // 檢查播放速度控制支持
  1342. checkPlaybackRateSupport(audio) {
  1343. try {
  1344. // 檢查基礎庫版本和平台支持
  1345. const systemInfo = uni.getSystemInfoSync();
  1346. console.log('系統信息:', systemInfo);
  1347. console.log('平台:', systemInfo.platform);
  1348. console.log('基礎庫版本:', systemInfo.SDKVersion);
  1349. console.log('系統版本:', systemInfo.system);
  1350. // 根據uni-app文檔,微信小程序需要基礎庫2.11.0+才支持playbackRate
  1351. const SDKVersion = systemInfo.SDKVersion || '0.0.0';
  1352. const versionArray = SDKVersion.split('.').map(v => parseInt(v));
  1353. const isVersionSupported = versionArray[0] > 2 ||
  1354. (versionArray[0] === 2 && versionArray[1] > 11) ||
  1355. (versionArray[0] === 2 && versionArray[1] === 11 && versionArray[2] >= 0);
  1356. console.log('基礎庫版本檢查:', {
  1357. version: SDKVersion,
  1358. parsed: versionArray,
  1359. supported: isVersionSupported
  1360. });
  1361. if (!isVersionSupported) {
  1362. this.playbackRateSupported = false;
  1363. console.log(`❌ 基礎庫版本不支持 (${SDKVersion}),微信小程序需要2.11.0+才支持playbackRate`);
  1364. return;
  1365. }
  1366. // Android平台需要6.0+版本支持
  1367. if (systemInfo.platform === 'android') {
  1368. const androidVersion = systemInfo.system.match(/Android (\d+)/);
  1369. console.log('Android版本檢查:', androidVersion);
  1370. if (androidVersion && parseInt(androidVersion[1]) < 6) {
  1371. this.playbackRateSupported = false;
  1372. console.log(`❌ Android版本不支持 (${androidVersion[1]}),需要Android 6+才支持playbackRate`);
  1373. return;
  1374. }
  1375. }
  1376. // 檢查音頻實例是否支持playbackRate
  1377. console.log('🔍 音頻實例檢查:');
  1378. console.log('- playbackRate初始值:', audio.playbackRate);
  1379. console.log('- playbackRate類型:', typeof audio.playbackRate);
  1380. // 嘗試設置並檢測是否真正支持
  1381. const testRate = 1.25;
  1382. const originalRate = audio.playbackRate || 1.0;
  1383. try {
  1384. audio.playbackRate = testRate;
  1385. console.log('- 設置測試速度:', testRate);
  1386. console.log('- 設置後的值:', audio.playbackRate);
  1387. // 檢查設置是否生效
  1388. if (Math.abs(audio.playbackRate - testRate) < 0.01) {
  1389. this.playbackRateSupported = true;
  1390. console.log('✅ playbackRate功能正常');
  1391. } else {
  1392. this.playbackRateSupported = false;
  1393. console.log('❌ playbackRate設置無效,可能不被支持');
  1394. }
  1395. } catch (error) {
  1396. this.playbackRateSupported = false;
  1397. console.log('❌ playbackRate設置出錯:', error);
  1398. }
  1399. } catch (error) {
  1400. console.error('檢查播放速度支持時出錯:', error);
  1401. this.playbackRateSupported = false;
  1402. }
  1403. },
  1404. formatTime(seconds) {
  1405. const mins = Math.floor(seconds / 60);
  1406. const secs = Math.floor(seconds % 60);
  1407. return `${mins.toString().padStart(2, '0')}:${secs.toString().padStart(2, '0')}`;
  1408. },
  1409. // 清理音频缓存
  1410. clearAudioCache() {
  1411. this.audioCache = {};
  1412. console.log('音频缓存已清理');
  1413. },
  1414. // 限制缓存大小,保留最近访问的页面
  1415. limitCacheSize(maxSize = 10) {
  1416. const cacheKeys = Object.keys(this.audioCache);
  1417. if (cacheKeys.length > maxSize) {
  1418. // 删除最旧的缓存项
  1419. const keysToDelete = cacheKeys.slice(0, cacheKeys.length - maxSize);
  1420. keysToDelete.forEach(key => {
  1421. delete this.audioCache[key];
  1422. });
  1423. console.log('缓存大小已限制,删除了', keysToDelete.length, '个缓存项');
  1424. }
  1425. },
  1426. // 自動加載第一頁音頻並播放
  1427. async autoLoadAndPlayFirstPage() {
  1428. try {
  1429. console.log('開始自動加載第一頁音頻');
  1430. // 確保當前是第一頁且是文字頁面
  1431. if (this.currentPage === 1 && this.isTextPage) {
  1432. console.log('當前是第一頁文字頁面,開始加載音頻');
  1433. // 加載音頻
  1434. await this.getCurrentPageAudio();
  1435. // 檢查是否成功加載音頻
  1436. if (this.currentPageAudios && this.currentPageAudios.length > 0) {
  1437. console.log('音頻加載成功,getCurrentPageAudio已經自動播放第一個音頻');
  1438. // getCurrentPageAudio方法已經處理了第一個音頻的播放,這裡不需要再次調用playAudio
  1439. } else {
  1440. console.log('第一頁沒有音頻數據');
  1441. }
  1442. } else {
  1443. console.log('當前頁面不是第一頁文字頁面,跳過自動播放');
  1444. }
  1445. } catch (error) {
  1446. console.error('自動加載和播放音頻失敗:', error);
  1447. }
  1448. },
  1449. // 清理音频资源
  1450. destroyAudio() {
  1451. if (this.currentAudio) {
  1452. this.currentAudio.destroy();
  1453. this.currentAudio = null;
  1454. }
  1455. this.isPlaying = false;
  1456. this.clearAudioCache();
  1457. },
  1458. // 课程切换时的完整数据清理(保留音色设置)
  1459. resetForCourseChange() {
  1460. console.log('课程切换:开始清理所有数据,保留音色设置');
  1461. // 1. 停止并销毁当前音频
  1462. if (this.isPlaying && this.currentAudio) {
  1463. this.pauseAudio();
  1464. }
  1465. if (this.currentAudio) {
  1466. this.currentAudio.destroy();
  1467. this.currentAudio = null;
  1468. }
  1469. // 2. 清空所有音频相关数据
  1470. this.currentPageAudios = [];
  1471. this.currentAudioIndex = 0;
  1472. this.currentTime = 0;
  1473. this.totalTime = 0;
  1474. this.sliderValue = 0;
  1475. this.isDragging = false;
  1476. this.isPlaying = false;
  1477. this.hasAudioData = false;
  1478. this.isAudioLoading = false;
  1479. this.audioLoadFailed = false;
  1480. this.currentHighlightIndex = -1;
  1481. // 3. 清空音频缓存(因为课程变了,所有缓存都无效)
  1482. this.clearAudioCache();
  1483. // 4. 重置预加载状态
  1484. this.isPreloading = false;
  1485. this.preloadProgress = 0;
  1486. this.preloadedPages = new Set();
  1487. // 5. 重置播放控制状态
  1488. this.isLoop = false;
  1489. this.playSpeed = 1.0;
  1490. this.playbackRateSupported = true;
  1491. // 6. 重置音色切换状态
  1492. this.isVoiceChanging = false;
  1493. // 7. 设置课程切换状态
  1494. this.isJustSwitchedCourse = true;
  1495. // 注意:不清空 voiceId,保留用户的音色选择
  1496. console.log('课程切换:数据清理完成,音色保持为:', this.voiceId);
  1497. // 7. 通知父组件状态变化
  1498. this.$emit('audio-state-change', {
  1499. hasAudioData: false,
  1500. isLoading: false,
  1501. audioLoadFailed: false,
  1502. currentHighlightIndex: -1
  1503. });
  1504. },
  1505. // 自动加载并播放音频(课程切换后调用)
  1506. async autoLoadAndPlayAudio() {
  1507. console.log('开始自动加载音频');
  1508. // 检查是否为文本页面
  1509. if (!this.isTextPage) {
  1510. console.log('当前不是文本页面,跳过音频加载');
  1511. return;
  1512. }
  1513. // 检查必要条件
  1514. if (!this.courseId || !this.currentPage) {
  1515. console.log('缺少必要参数,跳过音频加载');
  1516. return;
  1517. }
  1518. try {
  1519. // 设置加载状态
  1520. this.isAudioLoading = true;
  1521. // 开始加载音频
  1522. await this.getCurrentPageAudio();
  1523. console.log('音频自动加载完成,hasAudioData:', this.hasAudioData);
  1524. } catch (error) {
  1525. console.error('自动加载音频失败:', error);
  1526. this.isAudioLoading = false;
  1527. }
  1528. },
  1529. // 暂停音频(页面隐藏时调用)
  1530. pauseOnHide() {
  1531. this.pauseAudio();
  1532. },
  1533. // 处理音色切换(由父组件调用)
  1534. async handleVoiceChange(newVoiceId, options = {}) {
  1535. console.log('🎵 AudioControls: 开始处理音色切换', this.voiceId, '->', newVoiceId);
  1536. // 检查是否正在加载音频,如果是则阻止音色切换
  1537. if (this.isAudioLoading) {
  1538. console.log('🎵 音频正在加载中,阻止音色切换');
  1539. throw new Error('音频加载中,请稍后再试');
  1540. }
  1541. const { preloadAllPages = true } = options; // 默认预加载所有页面
  1542. try {
  1543. // 1. 停止当前播放的音频
  1544. if (this.isPlaying) {
  1545. this.pauseAudio();
  1546. }
  1547. // 2. 销毁当前音频实例
  1548. if (this.currentAudio) {
  1549. this.currentAudio.destroy();
  1550. this.currentAudio = null;
  1551. }
  1552. // 3. 清理所有音频缓存(因为音色变了,所有缓存都无效)
  1553. this.clearAudioCache();
  1554. console.log('🎵 AudioControls: 已清理所有音频缓存');
  1555. // 4. 重置音频状态
  1556. this.currentPageAudios = [];
  1557. this.currentAudioIndex = 0;
  1558. this.isPlaying = false;
  1559. this.currentTime = 0;
  1560. this.totalTime = 0;
  1561. this.hasAudioData = false;
  1562. this.audioLoadFailed = false;
  1563. this.currentHighlightIndex = -1;
  1564. // 5. 设置音色切换加载状态
  1565. this.isVoiceChanging = true;
  1566. this.isAudioLoading = true;
  1567. // 6. 更新本地音色ID(不直接修改prop)
  1568. this.localVoiceId = newVoiceId;
  1569. // 7. 通知父组件开始加载状态
  1570. this.$emit('audio-state-change', {
  1571. hasAudioData: false,
  1572. isLoading: true,
  1573. currentHighlightIndex: -1
  1574. });
  1575. // 8. 如果当前是文本页面,优先获取当前页面音频
  1576. if (this.isTextPage && this.courseId && this.currentPage) {
  1577. console.log('🎵 AudioControls: 优先获取当前页面音频,使用新音色:', newVoiceId);
  1578. await this.getCurrentPageAudio();
  1579. console.log('🎵 AudioControls: 当前页面音频获取完成');
  1580. } else {
  1581. // 如果不是文本页面,直接清除加载状态
  1582. this.isAudioLoading = false;
  1583. console.log('🎵 AudioControls: 非文本页面,直接清除加载状态');
  1584. }
  1585. // 9. 清除音色切换加载状态
  1586. this.isVoiceChanging = false;
  1587. // 10. 如果需要预加载其他页面,启动预加载
  1588. if (preloadAllPages) {
  1589. console.log('🎵 AudioControls: 开始预加载其他页面音频,使用新音色:', newVoiceId);
  1590. // 延迟启动预加载,确保当前页面音频加载完成
  1591. setTimeout(() => {
  1592. this.preloadAllPagesAudio();
  1593. }, 1000);
  1594. }
  1595. // 11. 通知父组件最终状态
  1596. this.$emit('audio-state-change', {
  1597. hasAudioData: this.hasAudioData,
  1598. isLoading: this.isAudioLoading,
  1599. currentHighlightIndex: this.currentHighlightIndex
  1600. });
  1601. // 12. 通知父组件音色切换完成
  1602. this.$emit('voice-change-complete', {
  1603. voiceId: newVoiceId,
  1604. hasAudioData: this.hasAudioData,
  1605. preloadAllPages: preloadAllPages
  1606. });
  1607. console.log('🎵 AudioControls: 音色切换处理完成');
  1608. } catch (error) {
  1609. console.error('🎵 AudioControls: 音色切换处理失败:', error);
  1610. // 清除加载状态
  1611. this.isVoiceChanging = false;
  1612. this.isAudioLoading = false;
  1613. // 通知父组件状态变化
  1614. this.$emit('audio-state-change', {
  1615. hasAudioData: false,
  1616. isLoading: false,
  1617. currentHighlightIndex: -1
  1618. });
  1619. this.$emit('voice-change-error', error);
  1620. }
  1621. },
  1622. // 预加载所有页面音频(音色切换时使用)
  1623. async preloadAllPagesAudio() {
  1624. if (this.isPreloading) {
  1625. console.log('已在预加载中,跳过');
  1626. return;
  1627. }
  1628. try {
  1629. console.log('开始预加载所有页面音频');
  1630. this.isPreloading = true;
  1631. this.preloadProgress = 0;
  1632. // 获取所有文本页面
  1633. const allTextPages = [];
  1634. for (let i = 0; i < this.bookPages.length; i++) {
  1635. const pageData = this.bookPages[i];
  1636. const hasTextContent = pageData && pageData.some(item => item.type === 'text');
  1637. if (hasTextContent && i !== this.currentPage - 1) { // 排除当前页面,因为已经加载过了
  1638. allTextPages.push({
  1639. pageIndex: i + 1,
  1640. pageData: pageData
  1641. });
  1642. }
  1643. }
  1644. if (allTextPages.length === 0) {
  1645. console.log('没有需要预加载的页面');
  1646. this.isPreloading = false;
  1647. return;
  1648. }
  1649. console.log(`需要预加载 ${allTextPages.length} 个页面的音频`);
  1650. // 逐页预加载音频
  1651. for (let i = 0; i < allTextPages.length; i++) {
  1652. const pageInfo = allTextPages[i];
  1653. try {
  1654. console.log(`预加载第 ${pageInfo.pageIndex} 页音频 (${i + 1}/${allTextPages.length})`);
  1655. await this.preloadPageAudio(pageInfo.pageIndex, pageInfo.pageData);
  1656. // 更新进度
  1657. this.preloadProgress = Math.round(((i + 1) / allTextPages.length) * 100);
  1658. console.log(`预加载进度: ${this.preloadProgress}%`);
  1659. // 添加小延迟,避免请求过于频繁
  1660. if (i < allTextPages.length - 1) {
  1661. await new Promise(resolve => setTimeout(resolve, 200));
  1662. }
  1663. } catch (error) {
  1664. console.error(`预加载第 ${pageInfo.pageIndex} 页音频失败:`, error);
  1665. // 继续预加载其他页面,不因单页失败而中断
  1666. }
  1667. }
  1668. console.log('所有页面音频预加载完成');
  1669. } catch (error) {
  1670. console.error('预加载所有页面音频失败:', error);
  1671. } finally {
  1672. this.isPreloading = false;
  1673. this.preloadProgress = 100;
  1674. console.log('音色切换后的音频预加载任务结束');
  1675. }
  1676. },
  1677. // 开始预加载音频(由父组件调用)
  1678. async startPreloadAudio() {
  1679. if (this.isPreloading) {
  1680. console.log('已在预加载中,跳过');
  1681. return;
  1682. }
  1683. try {
  1684. console.log('开始预加载音频数据');
  1685. this.isPreloading = true;
  1686. this.preloadProgress = 0;
  1687. // 获取需要预加载的页面列表(当前页面后的几页)
  1688. const preloadPages = this.getPreloadPageList();
  1689. if (preloadPages.length === 0) {
  1690. console.log('没有需要预加载的页面');
  1691. this.isPreloading = false;
  1692. return;
  1693. }
  1694. console.log('需要预加载的页面:', preloadPages);
  1695. // 逐个预加载页面音频
  1696. for (let i = 0; i < preloadPages.length; i++) {
  1697. const pageInfo = preloadPages[i];
  1698. try {
  1699. console.log(`开始预加载第${pageInfo.pageIndex + 1}页音频`);
  1700. await this.preloadPageAudio(pageInfo.pageIndex, pageInfo.pageData);
  1701. // 更新预加载进度
  1702. this.preloadProgress = Math.round(((i + 1) / preloadPages.length) * 100);
  1703. console.log(`预加载进度: ${this.preloadProgress}%`);
  1704. // 延迟一下,避免请求过于频繁
  1705. await new Promise(resolve => setTimeout(resolve, 600));
  1706. } catch (error) {
  1707. console.error(`预加载第${pageInfo.pageIndex + 1}页音频失败:`, error);
  1708. // 继续预加载其他页面
  1709. }
  1710. }
  1711. console.log('音频预加载完成');
  1712. } catch (error) {
  1713. console.error('预加载音频失败:', error);
  1714. } finally {
  1715. this.isPreloading = false;
  1716. this.preloadProgress = 100;
  1717. }
  1718. },
  1719. // 获取需要预加载的页面列表
  1720. getPreloadPageList() {
  1721. const preloadPages = [];
  1722. const maxPreloadPages = 3; // 优化:最多预加载3页,减少服务器压力
  1723. // 从当前页面的下一页开始预加载
  1724. for (let i = this.currentPage; i < Math.min(this.currentPage + maxPreloadPages, this.bookPages.length); i++) {
  1725. const pageData = this.bookPages[i];
  1726. // 检查页面是否需要会员且用户非会员,如果是则跳过
  1727. const pageRequiresMember = this.pagePay[i] === 'Y';
  1728. if (pageRequiresMember && !this.isMember) {
  1729. console.log(`${i + 1}页需要会员,跳过预加载`);
  1730. continue;
  1731. }
  1732. // 检查页面是否有文本内容且未缓存
  1733. if (pageData && pageData.length > 0) {
  1734. const hasTextContent = pageData.some(item => item.type === 'text' && item.content);
  1735. const cacheKey = `${this.courseId}_${i + 1}_${this.voiceId}`;
  1736. const isAlreadyCached = this.audioCache[cacheKey];
  1737. if (hasTextContent && !isAlreadyCached) {
  1738. preloadPages.push({
  1739. pageIndex: i,
  1740. pageData: pageData
  1741. });
  1742. }
  1743. }
  1744. }
  1745. return preloadPages;
  1746. },
  1747. // 预加载单个页面的音频
  1748. async preloadPageAudio(pageIndex, pageData) {
  1749. const cacheKey = `${this.courseId}_${pageIndex + 1}_${this.voiceId}`;
  1750. // 检查是否已经缓存
  1751. if (this.audioCache[cacheKey]) {
  1752. console.log(`${pageIndex + 1}页音频已缓存,跳过`);
  1753. return;
  1754. }
  1755. // 收集页面中的文本内容
  1756. const textItems = pageData.filter(item => item.type === 'text' && item.content);
  1757. if (textItems.length === 0) {
  1758. console.log(`${pageIndex + 1}页没有文本内容,跳过`);
  1759. return;
  1760. }
  1761. console.log(`${pageIndex + 1}页有${textItems.length}个文本段落需要预加载`);
  1762. const audioArray = [];
  1763. let totalDuration = 0;
  1764. // 逐个处理文本项,支持长文本分割
  1765. for (let i = 0; i < textItems.length; i++) {
  1766. const item = textItems[i];
  1767. try {
  1768. console.log(`${pageIndex + 1}页处理第 ${i + 1}/${textItems.length} 个文本项,长度: ${item.content.length}`);
  1769. // 使用分批次请求音频
  1770. const batchResult = await this.requestAudioInBatches(item.content, this.localVoiceId);
  1771. // 检查请求是否被取消
  1772. if (batchResult === null) {
  1773. console.log(`${pageIndex + 1}页预加载被取消`);
  1774. return;
  1775. }
  1776. if (batchResult.audioSegments.length > 0) {
  1777. // 将所有音频段添加到音频数组
  1778. for (const segment of batchResult.audioSegments) {
  1779. if (!segment.error) {
  1780. audioArray.push({
  1781. url: segment.url,
  1782. text: segment.text,
  1783. duration: segment.duration,
  1784. startIndex: segment.startIndex,
  1785. endIndex: segment.endIndex,
  1786. segmentIndex: segment.segmentIndex,
  1787. originalTextIndex: i, // 标记属于哪个原始文本项
  1788. isSegmented: batchResult.audioSegments.length > 1 // 标记是否为分段音频
  1789. });
  1790. totalDuration += segment.duration;
  1791. }
  1792. }
  1793. console.log(`${pageIndex + 1}页第${i + 1}个文本项预加载完成,获得 ${batchResult.audioSegments.filter(s => !s.error).length} 个音频段`);
  1794. } else {
  1795. console.error(`${pageIndex + 1}页第${i + 1}个文本项音频预加载全部失败`);
  1796. }
  1797. } catch (error) {
  1798. console.error(`${pageIndex + 1}页第${i + 1}个文本项处理异常:`, error);
  1799. }
  1800. // 每个文本项处理之间间隔600ms,避免请求过于频繁
  1801. if (i < textItems.length - 1) {
  1802. await new Promise(resolve => setTimeout(resolve, 600));
  1803. }
  1804. }
  1805. // 保存到缓存
  1806. if (audioArray.length > 0) {
  1807. this.audioCache[cacheKey] = {
  1808. audios: audioArray,
  1809. totalDuration: totalDuration,
  1810. voiceId: this.localVoiceId, // 保存音色ID用于验证
  1811. timestamp: Date.now() // 保存时间戳
  1812. };
  1813. console.log(`${pageIndex + 1}页音频预加载完成,共${audioArray.length}个音频,总时长: ${totalDuration}`);
  1814. // 限制缓存大小
  1815. this.limitCacheSize(10);
  1816. }
  1817. },
  1818. // 检查指定页面是否有音频缓存
  1819. checkAudioCache(pageNumber) {
  1820. const cacheKey = `${this.courseId}_${pageNumber}_${this.localVoiceId}`;
  1821. const cachedData = this.audioCache[cacheKey];
  1822. if (cachedData && cachedData.audios && cachedData.audios.length > 0) {
  1823. console.log(`${pageNumber}页音频已缓存,共${cachedData.audios.length}个音频`);
  1824. return true;
  1825. }
  1826. console.log(`${pageNumber}页音频未缓存`);
  1827. return false;
  1828. },
  1829. // 自动播放已缓存的音频
  1830. async autoPlayCachedAudio() {
  1831. try {
  1832. // 如果正在音色切换中,不自动播放
  1833. if (this.isVoiceChanging) {
  1834. console.log('音色切换中,跳过自动播放');
  1835. return;
  1836. }
  1837. const cacheKey = `${this.courseId}_${this.currentPage}_${this.voiceId}`;
  1838. const cachedData = this.audioCache[cacheKey];
  1839. if (!cachedData || !cachedData.audios || cachedData.audios.length === 0) {
  1840. console.log('当前页面没有缓存的音频');
  1841. return;
  1842. }
  1843. console.log(`开始自动播放第${this.currentPage}页的缓存音频`);
  1844. // 停止当前播放的音频
  1845. this.pauseAudio();
  1846. // 设置当前页面的音频数据
  1847. this.currentPageAudios = cachedData.audios;
  1848. this.totalDuration = cachedData.totalDuration;
  1849. // 重置播放状态
  1850. this.currentAudioIndex = 0;
  1851. this.currentTime = 0;
  1852. this.isPlaying = false;
  1853. // 延迟一下再开始播放,确保UI更新完成
  1854. setTimeout(() => {
  1855. this.playAudio();
  1856. }, 300);
  1857. } catch (error) {
  1858. console.error('自动播放缓存音频失败:', error);
  1859. }
  1860. }
  1861. },
  1862. mounted() {
  1863. // 初始檢測播放速度支持
  1864. this.checkInitialPlaybackRateSupport();
  1865. },
  1866. // 自动播放预加载的音频
  1867. async autoPlayPreloadedAudio() {
  1868. try {
  1869. // 如果正在音色切换中,不自动播放
  1870. if (this.isVoiceChanging) {
  1871. console.log('音色切换中,跳过预加载音频自动播放');
  1872. return;
  1873. }
  1874. // 检查是否有音频数据
  1875. if (!this.hasAudioData || this.currentPageAudios.length === 0) {
  1876. console.log('没有可播放的音频数据');
  1877. return;
  1878. }
  1879. // 检查第一个音频是否有效
  1880. const firstAudio = this.currentPageAudios[0];
  1881. if (!firstAudio || !firstAudio.url) {
  1882. console.log('第一个音频无效');
  1883. return;
  1884. }
  1885. console.log('开始自动播放预加载音频');
  1886. // 重置播放状态
  1887. this.currentAudioIndex = 0;
  1888. this.currentTime = 0;
  1889. this.sliderValue = 0;
  1890. this.currentHighlightIndex = 0;
  1891. // 创建音频实例
  1892. this.createAudioInstance();
  1893. // 稍等一下再播放,确保音频准备就绪
  1894. setTimeout(() => {
  1895. if (this.currentAudio && !this.isPlaying) {
  1896. this.currentAudio.play();
  1897. console.log('预加载音频自动播放成功');
  1898. }
  1899. }, 200);
  1900. } catch (error) {
  1901. console.error('自动播放预加载音频失败:', error);
  1902. }
  1903. },
  1904. beforeDestroy() {
  1905. // 清理页面切换防抖定时器
  1906. if (this.pageChangeTimer) {
  1907. clearTimeout(this.pageChangeTimer);
  1908. this.pageChangeTimer = null;
  1909. }
  1910. // 清理音频资源
  1911. this.destroyAudio();
  1912. console.log('AudioControls组件已销毁,清理所有资源');
  1913. }
  1914. }
  1915. </script>
  1916. <style lang="scss" scoped>
  1917. /* 音频控制栏样式 */
  1918. .audio-controls-wrapper {
  1919. position: relative;
  1920. z-index: 10;
  1921. }
  1922. .audio-controls {
  1923. background: #fff;
  1924. padding: 20rpx 40rpx;
  1925. border-bottom: 1rpx solid #eee;
  1926. transition: transform 0.3s ease;
  1927. position: relative;
  1928. z-index: 10;
  1929. &.audio-hidden {
  1930. transform: translateY(100%);
  1931. }
  1932. }
  1933. .audio-time {
  1934. display: flex;
  1935. align-items: center;
  1936. margin-bottom: 20rpx;
  1937. }
  1938. .time-text {
  1939. font-size: 28rpx;
  1940. color: #999;
  1941. min-width: 80rpx;
  1942. }
  1943. .progress-container {
  1944. flex: 1;
  1945. margin: 0 20rpx;
  1946. }
  1947. .audio-controls-row {
  1948. display: flex;
  1949. align-items: center;
  1950. justify-content: space-between;
  1951. }
  1952. .control-btn {
  1953. display: flex;
  1954. align-items: center;
  1955. padding: 10rpx;
  1956. gap: 8rpx;
  1957. }
  1958. .control-btn.disabled {
  1959. pointer-events: none;
  1960. opacity: 0.6;
  1961. }
  1962. .control-text {
  1963. font-size: 28rpx;
  1964. color: #4A4A4A;
  1965. }
  1966. .play-btn {
  1967. display: flex;
  1968. align-items: center;
  1969. justify-content: center;
  1970. padding: 10rpx;
  1971. }
  1972. /* 音频加载状态样式 */
  1973. .audio-loading-container {
  1974. background: #fff;
  1975. padding: 40rpx;
  1976. border-bottom: 1rpx solid #eee;
  1977. display: flex;
  1978. flex-direction: column;
  1979. align-items: center;
  1980. justify-content: center;
  1981. gap: 20rpx;
  1982. position: relative;
  1983. z-index: 10;
  1984. }
  1985. /* 加载指示器样式 */
  1986. .loading-indicator {
  1987. display: flex;
  1988. align-items: center;
  1989. justify-content: center;
  1990. gap: 10rpx;
  1991. padding: 10rpx 20rpx;
  1992. background: rgba(6, 218, 220, 0.1);
  1993. border-radius: 20rpx;
  1994. margin-bottom: 10rpx;
  1995. }
  1996. .loading-indicator-text {
  1997. font-size: 24rpx;
  1998. color: #06DADC;
  1999. }
  2000. .loading-text {
  2001. font-size: 28rpx;
  2002. color: #999;
  2003. }
  2004. /* 音色切换加载状态特殊样式 */
  2005. .voice-changing {
  2006. background: linear-gradient(135deg, #fff5f0 0%, #ffe7d9 100%);
  2007. border: 2rpx solid #ff6b35;
  2008. }
  2009. .voice-changing-text {
  2010. color: #ff6b35;
  2011. font-weight: 500;
  2012. }
  2013. /* 预加载状态特殊样式 */
  2014. .preloading {
  2015. background: linear-gradient(135deg, #f0f9ff 0%, #e0f2fe 100%);
  2016. border: 2rpx solid #06DADC;
  2017. }
  2018. .preloading .loading-text {
  2019. color: #06DADC;
  2020. font-weight: 500;
  2021. }
  2022. /* 课程切换状态特殊样式 */
  2023. .course-switching {
  2024. background: linear-gradient(135deg, #f6ffed 0%, #d9f7be 100%);
  2025. border: 2rpx solid #52c41a;
  2026. }
  2027. .course-switching .loading-text {
  2028. color: #52c41a;
  2029. font-weight: 500;
  2030. }
  2031. /* 获取音频按钮样式 */
  2032. .audio-get-button-container {
  2033. background: rgba(255, 255, 255, 0.95);
  2034. backdrop-filter: blur(10px);
  2035. padding: 30rpx;
  2036. border-radius: 20rpx;
  2037. border: 2rpx solid #E5E5E5;
  2038. transition: all 0.3s ease;
  2039. position: relative;
  2040. z-index: 10;
  2041. }
  2042. .get-audio-btn {
  2043. display: flex;
  2044. align-items: center;
  2045. justify-content: center;
  2046. gap: 16rpx;
  2047. padding: 20rpx 40rpx;
  2048. background: linear-gradient(135deg, #06DADC 0%, #04B8BA 100%);
  2049. border-radius: 50rpx;
  2050. box-shadow: 0 8rpx 20rpx rgba(6, 218, 220, 0.3);
  2051. transition: all 0.3s ease;
  2052. }
  2053. .get-audio-btn:active {
  2054. transform: scale(0.95);
  2055. box-shadow: 0 4rpx 10rpx rgba(6, 218, 220, 0.2);
  2056. }
  2057. /* 音频预加载提示样式 */
  2058. .audio-preloaded-container {
  2059. display: flex;
  2060. justify-content: center;
  2061. align-items: center;
  2062. padding: 20rpx;
  2063. transition: all 0.3s ease;
  2064. position: relative;
  2065. z-index: 10;
  2066. }
  2067. .preloaded-tip {
  2068. display: flex;
  2069. align-items: center;
  2070. justify-content: center;
  2071. gap: 16rpx;
  2072. padding: 20rpx 40rpx;
  2073. background: linear-gradient(135deg, #52c41a 0%, #389e0d 100%);
  2074. border-radius: 50rpx;
  2075. box-shadow: 0 8rpx 20rpx rgba(82, 196, 26, 0.3);
  2076. transition: all 0.3s ease;
  2077. }
  2078. .preloaded-text {
  2079. color: #ffffff;
  2080. font-size: 28rpx;
  2081. font-weight: 500;
  2082. }
  2083. /* 音频获取失败样式 */
  2084. .audio-failed-container {
  2085. display: flex;
  2086. flex-direction: column;
  2087. align-items: center;
  2088. justify-content: center;
  2089. padding: 20rpx;
  2090. gap: 20rpx;
  2091. }
  2092. .failed-tip {
  2093. display: flex;
  2094. align-items: center;
  2095. justify-content: center;
  2096. gap: 16rpx;
  2097. padding: 20rpx 40rpx;
  2098. background: linear-gradient(135deg, #ff4d4f 0%, #cf1322 100%);
  2099. border-radius: 50rpx;
  2100. box-shadow: 0 8rpx 20rpx rgba(255, 77, 79, 0.3);
  2101. }
  2102. .failed-text {
  2103. color: #ffffff;
  2104. font-size: 28rpx;
  2105. font-weight: 500;
  2106. }
  2107. .retry-btn {
  2108. display: flex;
  2109. align-items: center;
  2110. justify-content: center;
  2111. gap: 12rpx;
  2112. padding: 16rpx 32rpx;
  2113. background: linear-gradient(135deg, #06DADC 0%, #05B8BA 100%);
  2114. border-radius: 40rpx;
  2115. box-shadow: 0 6rpx 16rpx rgba(6, 218, 220, 0.3);
  2116. transition: all 0.3s ease;
  2117. }
  2118. .retry-btn:active {
  2119. transform: scale(0.95);
  2120. box-shadow: 0 4rpx 12rpx rgba(6, 218, 220, 0.4);
  2121. }
  2122. .retry-text {
  2123. color: #ffffff;
  2124. font-size: 26rpx;
  2125. font-weight: 500;
  2126. }
  2127. .get-audio-text {
  2128. font-size: 32rpx;
  2129. color: #FFFFFF;
  2130. font-weight: 500;
  2131. }
  2132. /* 会员限制容器样式 */
  2133. .member-restricted-container {
  2134. height: 0;
  2135. overflow: hidden;
  2136. opacity: 0;
  2137. pointer-events: none;
  2138. }
  2139. </style>