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

1181 lines
38 KiB

  1. <template>
  2. <view class="audio-controls-wrapper">
  3. <!-- 获取音频按钮 -->
  4. <view v-if="!hasAudioData && !isAudioLoading && isTextPage" class="audio-get-button-container">
  5. <view class="get-audio-btn" @click="handleGetAudio">
  6. <uv-icon name="play-circle" size="24" color="#06DADC"></uv-icon>
  7. <text class="get-audio-text">获取第{{currentPage}}页音频</text>
  8. </view>
  9. </view>
  10. <!-- 音频加载状态 -->
  11. <view v-else-if="isAudioLoading && isTextPage" class="audio-loading-container">
  12. <uv-loading-icon mode="spinner" size="30" color="#06DADC"></uv-loading-icon>
  13. <text class="loading-text">正在加载第{{currentPage}}页音频...</text>
  14. </view>
  15. <!-- 正常音频控制栏 -->
  16. <view v-else-if="hasAudioData" class="audio-controls" :class="{ 'audio-hidden': !isTextPage }">
  17. <view class="audio-time">
  18. <text class="time-text">{{ formatTime(currentTime) }}</text>
  19. <view class="progress-container">
  20. <uv-slider
  21. v-model="sliderValue"
  22. :min="0"
  23. :max="totalTime"
  24. :step="0.1"
  25. activeColor="#06DADC"
  26. backgroundColor="#e0e0e0"
  27. :blockSize="16"
  28. blockColor="#ffffff"
  29. :disabled="!hasAudioData || totalTime <= 0"
  30. @input="onSliderInput"
  31. @changing="onSliderChanging"
  32. @change="onSliderChange"
  33. :customStyle="{ flex: 1, margin: '0 10px' }"
  34. />
  35. </view>
  36. <text class="time-text">{{ formatTime(totalTime) }}</text>
  37. </view>
  38. <view class="audio-controls-row">
  39. <view class="control-btn" @click="toggleLoop">
  40. <uv-icon name="reload" size="20" :color="isLoop ? '#06DADC' : '#999'"></uv-icon>
  41. <text class="control-text">循环</text>
  42. </view>
  43. <view class="control-btn" @click="$emit('previous-page')">
  44. <text class="control-text">上一页</text>
  45. </view>
  46. <view class="play-btn" @click="togglePlay">
  47. <uv-icon :name="isPlaying ? 'pause-circle-fill' : 'play-circle-fill'" size="40" color="#666"></uv-icon>
  48. </view>
  49. <view class="control-btn" @click="$emit('next-page')">
  50. <text class="control-text">下一页</text>
  51. </view>
  52. <view class="control-btn" @click="toggleSpeed" :class="{ 'disabled': !playbackRateSupported }">
  53. <text class="control-text" :style="{ opacity: playbackRateSupported ? 1 : 0.5 }">
  54. {{ playbackRateSupported ? playSpeed + 'x' : '不支持' }}
  55. </text>
  56. </view>
  57. </view>
  58. </view>
  59. </view>
  60. </template>
  61. <script>
  62. import config from '@/mixins/config.js'
  63. export default {
  64. name: 'AudioControls',
  65. mixins: [config],
  66. props: {
  67. // 基础数据
  68. currentPage: {
  69. type: Number,
  70. default: 1
  71. },
  72. courseId: {
  73. type: String,
  74. default: ''
  75. },
  76. voiceId: {
  77. type: String,
  78. default: ''
  79. },
  80. bookPages: {
  81. type: Array,
  82. default: () => []
  83. },
  84. isTextPage: {
  85. type: Boolean,
  86. default: false
  87. }
  88. },
  89. data() {
  90. return {
  91. // 音频控制相关数据
  92. isPlaying: false,
  93. currentTime: 0,
  94. totalTime: 0,
  95. sliderValue: 0, // 滑動條的值
  96. isDragging: false, // 是否正在拖動滑動條
  97. isLoop: false,
  98. playSpeed: 1.0,
  99. speedOptions: [0.5, 0.8, 1.0, 1.25, 1.5, 2.0], // 根據uni-app文檔的官方支持值
  100. playbackRateSupported: true, // 播放速度控制是否支持
  101. // 音频数组管理
  102. currentPageAudios: [], // 当前页面的音频数组
  103. currentAudioIndex: 0, // 当前播放的音频索引
  104. audioContext: null, // 音频上下文
  105. currentAudio: null, // 当前音频实例
  106. // 音频缓存管理
  107. audioCache: {}, // 页面音频缓存 {pageIndex: {audios: [], totalDuration: 0}}
  108. // 音频加载状态
  109. isAudioLoading: false, // 音频是否正在加载
  110. hasAudioData: false, // 当前页面是否已有音频数据
  111. // 文本高亮相关
  112. currentHighlightIndex: -1, // 当前高亮的文本索引
  113. }
  114. },
  115. computed: {
  116. // 计算音频播放进度百分比
  117. progressPercent() {
  118. return this.totalTime > 0 ? (this.currentTime / this.totalTime) * 100 : 0;
  119. }
  120. },
  121. watch: {
  122. // 监听页面变化,重置音频状态
  123. currentPage: {
  124. handler(newPage, oldPage) {
  125. if (newPage !== oldPage) {
  126. this.resetAudioState();
  127. }
  128. },
  129. immediate: false
  130. },
  131. // 监听音色变化,清除缓存
  132. voiceId: {
  133. handler(newVoiceId, oldVoiceId) {
  134. if (newVoiceId !== oldVoiceId && oldVoiceId) {
  135. this.clearAudioCache();
  136. this.resetAudioState();
  137. }
  138. }
  139. }
  140. },
  141. methods: {
  142. // 获取当前页面的音频内容
  143. async getCurrentPageAudio() {
  144. // 检查缓存中是否已有当前页面的音频数据
  145. const cacheKey = `${this.courseId}_${this.currentPage}`;
  146. if (this.audioCache[cacheKey]) {
  147. console.log('从缓存加载音频数据:', cacheKey);
  148. // 从缓存加载音频数据
  149. this.currentPageAudios = this.audioCache[cacheKey].audios;
  150. this.totalTime = this.audioCache[cacheKey].totalDuration;
  151. this.currentAudioIndex = 0;
  152. this.isPlaying = false;
  153. this.currentTime = 0;
  154. return;
  155. }
  156. // 开始加载状态
  157. this.isAudioLoading = true;
  158. // 清空当前页面音频数组
  159. this.currentPageAudios = [];
  160. this.currentAudioIndex = 0;
  161. this.isPlaying = false;
  162. this.currentTime = 0;
  163. this.totalTime = 0;
  164. // 对着当前页面的每一个[]元素进行切割 如果是文本text类型则进行音频请求
  165. const currentPageData = this.bookPages[this.currentPage - 1];
  166. if (currentPageData) {
  167. // 收集所有text类型的元素
  168. const textItems = currentPageData.filter(item => item.type === 'text');
  169. if (textItems.length > 0) {
  170. // 并行发送所有音频请求
  171. const audioPromises = textItems.map(async (item, index) => {
  172. try {
  173. // 进行音频请求 - 修正字段名:使用content而不是text
  174. const radioRes = await this.$api.music.textToVoice({
  175. text: item.content,
  176. voiceType: this.voiceId,
  177. });
  178. console.log(`音频请求响应 ${index + 1}:`, radioRes);
  179. if(radioRes.code === 200){
  180. // 新格式:API直接返回音頻URL
  181. const audioUrl = radioRes.result.audio;
  182. // 檢查API是否返回時長信息
  183. const duration = radioRes.result.duration || 0;
  184. // 同时保存到原始数据中以保持兼容性
  185. item.audioUrl = audioUrl;
  186. console.log(`音频URL设置成功 ${index + 1}:`, audioUrl, '时长:', duration);
  187. return {
  188. url: audioUrl,
  189. text: item.content,
  190. duration: duration, // 優先使用API返回的時長
  191. index: index // 保持原始顺序
  192. };
  193. } else {
  194. console.error(`音频请求失败 ${index + 1}:`, radioRes);
  195. return null;
  196. }
  197. } catch (error) {
  198. console.error(`音频请求异常 ${index + 1}:`, error);
  199. return null;
  200. }
  201. });
  202. // 等待所有音频请求完成
  203. const audioResults = await Promise.all(audioPromises);
  204. // 按原始顺序添加到音频数组中,过滤掉失败的请求
  205. this.currentPageAudios = audioResults
  206. .filter(result => result !== null)
  207. .sort((a, b) => a.index - b.index)
  208. .map(result => ({
  209. url: result.url,
  210. text: result.text,
  211. duration: result.duration
  212. }));
  213. console.log('所有音频请求完成,共获取', this.currentPageAudios.length, '个音频');
  214. }
  215. // 如果有音频,计算总时长
  216. if (this.currentPageAudios.length > 0) {
  217. await this.calculateTotalDuration();
  218. // 将音频数据保存到缓存中
  219. const cacheKey = `${this.courseId}_${this.currentPage}`;
  220. this.audioCache[cacheKey] = {
  221. audios: [...this.currentPageAudios], // 深拷贝音频数组
  222. totalDuration: this.totalTime
  223. };
  224. console.log('音频数据已缓存:', cacheKey, this.audioCache[cacheKey]);
  225. // 限制缓存大小
  226. this.limitCacheSize(10);
  227. }
  228. }
  229. // 结束加载状态
  230. this.isAudioLoading = false;
  231. // 设置音频数据状态
  232. this.hasAudioData = this.currentPageAudios.length > 0;
  233. // 通知父组件音频状态变化
  234. this.$emit('audio-state-change', {
  235. hasAudioData: this.hasAudioData,
  236. isLoading: this.isAudioLoading,
  237. currentHighlightIndex: this.currentHighlightIndex
  238. });
  239. },
  240. // 重置音频状态
  241. resetAudioState() {
  242. // 检查当前页面是否已有缓存的音频数据
  243. const pageKey = `${this.courseId}_${this.currentPage}`;
  244. const cachedAudio = this.audioCache[pageKey];
  245. if (cachedAudio && cachedAudio.audios && cachedAudio.audios.length > 0) {
  246. // 如果有缓存的音频数据,恢复音频状态
  247. this.currentPageAudios = cachedAudio.audios;
  248. this.totalTime = cachedAudio.totalDuration || 0;
  249. this.hasAudioData = true;
  250. } else {
  251. // 如果没有缓存的音频数据,重置为初始状态
  252. this.currentPageAudios = [];
  253. this.totalTime = 0;
  254. this.hasAudioData = false;
  255. }
  256. // 重置播放状态,翻頁時統一設置播放速度為1.0
  257. this.currentAudioIndex = 0;
  258. this.isPlaying = false;
  259. this.currentTime = 0;
  260. this.isAudioLoading = false;
  261. this.currentHighlightIndex = -1;
  262. // 翻頁時強制設置播放速度為1.0(適應微信小程序特性)
  263. this.playSpeed = 1.0;
  264. // 通知父组件音频状态变化
  265. this.$emit('audio-state-change', {
  266. hasAudioData: this.hasAudioData,
  267. isLoading: this.isAudioLoading,
  268. currentHighlightIndex: this.currentHighlightIndex
  269. });
  270. },
  271. // 手动获取音频
  272. async handleGetAudio() {
  273. // 检查是否有音色ID
  274. if (!this.voiceId) {
  275. uni.showToast({
  276. title: '音色未加载,请稍后重试',
  277. icon: 'none'
  278. });
  279. return;
  280. }
  281. // 检查当前页面是否有文本内容
  282. if (!this.isTextPage) {
  283. uni.showToast({
  284. title: '当前页面没有文本内容',
  285. icon: 'none'
  286. });
  287. return;
  288. }
  289. // 检查是否正在加载
  290. if (this.isAudioLoading) {
  291. return;
  292. }
  293. // 调用获取音频方法
  294. await this.getCurrentPageAudio();
  295. },
  296. // 计算音频总时长
  297. async calculateTotalDuration() {
  298. let totalDuration = 0;
  299. for (let i = 0; i < this.currentPageAudios.length; i++) {
  300. const audio = this.currentPageAudios[i];
  301. // 優先使用API返回的時長信息
  302. if (audio.duration && audio.duration > 0) {
  303. console.log(`使用API返回的時長 ${i + 1}:`, audio.duration, '秒');
  304. totalDuration += audio.duration;
  305. continue;
  306. }
  307. // 如果沒有API時長信息,嘗試獲取音頻時長
  308. try {
  309. const duration = await this.getAudioDuration(audio.url);
  310. audio.duration = duration;
  311. totalDuration += duration;
  312. console.log(`獲取到音頻時長 ${i + 1}:`, duration, '秒');
  313. } catch (error) {
  314. console.error('获取音频时长失败:', error);
  315. // 如果无法获取时长,根據文字長度估算(更精確的估算)
  316. const textLength = audio.text.length;
  317. // 假設每分鐘可以讀150-200個字符,這裡用180作為平均值
  318. const estimatedDuration = Math.max(2, textLength / 3); // 每3個字符約1秒
  319. audio.duration = estimatedDuration;
  320. totalDuration += estimatedDuration;
  321. console.log(`估算音頻時長 ${i + 1}:`, estimatedDuration, '秒 (文字長度:', textLength, ')');
  322. }
  323. }
  324. this.totalTime = totalDuration;
  325. console.log('音频总时长:', totalDuration, '秒');
  326. },
  327. // 获取音频时长
  328. getAudioDuration(audioUrl) {
  329. return new Promise((resolve, reject) => {
  330. const audio = uni.createInnerAudioContext();
  331. audio.src = audioUrl;
  332. let resolved = false;
  333. // 监听音频加载完成事件
  334. audio.onCanplay(() => {
  335. console.log('音频可以播放,duration:', audio.duration);
  336. if (!resolved && audio.duration && audio.duration > 0) {
  337. resolved = true;
  338. resolve(audio.duration);
  339. audio.destroy();
  340. }
  341. });
  342. // 监听音频元数据加载完成事件
  343. audio.onLoadedmetadata = () => {
  344. console.log('音频元数据加载完成,duration:', audio.duration);
  345. if (!resolved && audio.duration && audio.duration > 0) {
  346. resolved = true;
  347. resolve(audio.duration);
  348. audio.destroy();
  349. }
  350. };
  351. // 监听音频时长更新事件
  352. audio.onDurationChange = () => {
  353. console.log('音频时长更新,duration:', audio.duration);
  354. if (!resolved && audio.duration && audio.duration > 0) {
  355. resolved = true;
  356. resolve(audio.duration);
  357. audio.destroy();
  358. }
  359. };
  360. // 如果以上方法都無法獲取時長,嘗試播放一小段來獲取時長
  361. audio.onPlay(() => {
  362. console.log('音频开始播放,duration:', audio.duration);
  363. if (!resolved) {
  364. setTimeout(() => {
  365. if (!resolved && audio.duration && audio.duration > 0) {
  366. resolved = true;
  367. resolve(audio.duration);
  368. audio.destroy();
  369. }
  370. }, 100); // 播放100ms後檢查時長
  371. }
  372. });
  373. audio.onError((error) => {
  374. console.error('音频加载失败:', error);
  375. if (!resolved) {
  376. resolved = true;
  377. reject(error);
  378. audio.destroy();
  379. }
  380. });
  381. // 設置較長的超時時間,並在超時前嘗試播放
  382. setTimeout(() => {
  383. if (!resolved) {
  384. console.log('嘗試播放音頻以獲取時長');
  385. audio.play();
  386. }
  387. }, 1000);
  388. // 最終超時處理
  389. setTimeout(() => {
  390. if (!resolved) {
  391. console.warn('獲取音頻時長超時,使用默認值');
  392. resolved = true;
  393. reject(new Error('获取音频时长超时'));
  394. audio.destroy();
  395. }
  396. }, 5000);
  397. });
  398. },
  399. // 音频控制方法
  400. togglePlay() {
  401. if (this.currentPageAudios.length === 0) {
  402. uni.showToast({
  403. title: '当前页面没有音频内容',
  404. icon: 'none'
  405. });
  406. return;
  407. }
  408. if (this.isPlaying) {
  409. this.pauseAudio();
  410. } else {
  411. this.playAudio();
  412. }
  413. },
  414. // 播放音频
  415. playAudio() {
  416. if (this.currentPageAudios.length === 0) return;
  417. // 如果没有当前音频实例或者需要切换音频
  418. if (!this.currentAudio || this.currentAudio.src !== this.currentPageAudios[this.currentAudioIndex].url) {
  419. this.createAudioInstance();
  420. }
  421. this.currentAudio.play();
  422. this.isPlaying = true;
  423. // 更新高亮状态
  424. this.updateHighlightIndex();
  425. },
  426. // 暂停音频
  427. pauseAudio() {
  428. if (this.currentAudio) {
  429. this.currentAudio.pause();
  430. }
  431. this.isPlaying = false;
  432. // 暂停时清除高亮
  433. this.currentHighlightIndex = -1;
  434. // 通知父组件高亮状态变化
  435. this.$emit('highlight-change', -1);
  436. },
  437. // 创建音频实例
  438. createAudioInstance() {
  439. // 销毁之前的音频实例
  440. if (this.currentAudio) {
  441. this.currentAudio.destroy();
  442. }
  443. // 優先使用微信原生API以支持playbackRate
  444. let audio;
  445. if (typeof wx !== 'undefined' && wx.createInnerAudioContext) {
  446. console.log('使用微信原生音頻API');
  447. audio = wx.createInnerAudioContext();
  448. } else {
  449. console.log('使用uni-app音頻API');
  450. audio = uni.createInnerAudioContext();
  451. }
  452. audio.src = this.currentPageAudios[this.currentAudioIndex].url;
  453. // 在音頻可以播放時檢測playbackRate支持
  454. audio.onCanplay(() => {
  455. console.log('🎵 音頻可以播放,開始檢測playbackRate支持');
  456. this.checkPlaybackRateSupport(audio);
  457. // 檢測完成後,設置用戶期望的播放速度
  458. setTimeout(() => {
  459. if (this.playbackRateSupported) {
  460. console.log('設置播放速度:', this.playSpeed);
  461. audio.playbackRate = this.playSpeed;
  462. // 驗證設置結果
  463. setTimeout(() => {
  464. console.log('最終播放速度:', audio.playbackRate);
  465. if (Math.abs(audio.playbackRate - this.playSpeed) > 0.01) {
  466. console.log('⚠️ 播放速度設置可能未生效');
  467. } else {
  468. console.log('✅ 播放速度設置成功');
  469. }
  470. }, 50);
  471. } else {
  472. console.log('❌ 當前環境不支持播放速度控制');
  473. }
  474. }, 50);
  475. });
  476. // 音频事件监听
  477. audio.onPlay(() => {
  478. console.log('音频开始播放');
  479. this.isPlaying = true;
  480. });
  481. audio.onPause(() => {
  482. console.log('音频暂停');
  483. this.isPlaying = false;
  484. });
  485. audio.onTimeUpdate(() => {
  486. this.updateCurrentTime();
  487. });
  488. audio.onEnded(() => {
  489. console.log('当前音频播放结束');
  490. this.onAudioEnded();
  491. });
  492. audio.onError((error) => {
  493. console.error('音频播放错误:', error);
  494. this.isPlaying = false;
  495. uni.showToast({
  496. title: '音频播放失败',
  497. icon: 'none'
  498. });
  499. });
  500. this.currentAudio = audio;
  501. },
  502. // 更新当前播放时间
  503. updateCurrentTime() {
  504. if (!this.currentAudio) return;
  505. let totalTime = 0;
  506. // 计算之前音频的总时长
  507. for (let i = 0; i < this.currentAudioIndex; i++) {
  508. totalTime += this.currentPageAudios[i].duration;
  509. }
  510. // 加上当前音频的播放时间
  511. totalTime += this.currentAudio.currentTime;
  512. this.currentTime = totalTime;
  513. // 如果不是正在拖動滑動條,則同步更新滑動條的值
  514. if (!this.isDragging) {
  515. this.sliderValue = this.currentTime;
  516. }
  517. // 更新当前高亮的文本索引
  518. this.updateHighlightIndex();
  519. },
  520. // 更新高亮文本索引
  521. updateHighlightIndex() {
  522. if (!this.isPlaying || this.currentPageAudios.length === 0) {
  523. this.currentHighlightIndex = -1;
  524. this.$emit('highlight-change', -1);
  525. return;
  526. }
  527. // 根据当前播放的音频索引设置高亮
  528. this.currentHighlightIndex = this.currentAudioIndex;
  529. console.log('更新高亮索引:', this.currentHighlightIndex, '当前音频索引:', this.currentAudioIndex);
  530. // 通知父组件高亮状态变化
  531. this.$emit('highlight-change', this.currentHighlightIndex);
  532. },
  533. // 音频播放结束处理
  534. onAudioEnded() {
  535. if (this.currentAudioIndex < this.currentPageAudios.length - 1) {
  536. // 播放下一个音频
  537. this.currentAudioIndex++;
  538. this.playAudio();
  539. } else {
  540. // 所有音频播放完毕
  541. if (this.isLoop) {
  542. // 循环播放
  543. this.currentAudioIndex = 0;
  544. this.playAudio();
  545. } else {
  546. // 停止播放
  547. this.isPlaying = false;
  548. this.currentTime = this.totalTime;
  549. this.currentHighlightIndex = -1;
  550. this.$emit('highlight-change', -1);
  551. }
  552. }
  553. },
  554. toggleLoop() {
  555. this.isLoop = !this.isLoop;
  556. },
  557. toggleSpeed() {
  558. // 檢查是否支持播放速度控制
  559. if (!this.playbackRateSupported) {
  560. uni.showToast({
  561. title: '當前設備不支持播放速度控制',
  562. icon: 'none',
  563. duration: 2000
  564. });
  565. return;
  566. }
  567. const currentIndex = this.speedOptions.indexOf(this.playSpeed);
  568. const nextIndex = (currentIndex + 1) % this.speedOptions.length;
  569. const oldSpeed = this.playSpeed;
  570. this.playSpeed = this.speedOptions[nextIndex];
  571. console.log(`播放速度切換: ${oldSpeed}x -> ${this.playSpeed}x`);
  572. // 如果当前有音频在播放,更新播放速度
  573. if (this.currentAudio) {
  574. const wasPlaying = this.isPlaying;
  575. const currentTime = this.currentAudio.currentTime;
  576. // 設置新的播放速度
  577. this.currentAudio.playbackRate = this.playSpeed;
  578. // 如果正在播放,需要重啟播放才能使播放速度生效
  579. if (wasPlaying) {
  580. this.currentAudio.pause();
  581. setTimeout(() => {
  582. this.currentAudio.seek(currentTime);
  583. this.currentAudio.play();
  584. }, 50);
  585. }
  586. console.log('音頻實例播放速度已更新為:', this.currentAudio.playbackRate);
  587. // 顯示速度變更提示
  588. uni.showToast({
  589. title: `播放速度: ${this.playSpeed}x`,
  590. icon: 'none',
  591. duration: 1000
  592. });
  593. }
  594. },
  595. // 滑動條值實時更新 (@input 事件)
  596. onSliderInput(value) {
  597. // 在拖動過程中實時更新顯示的時間,但不影響實際播放
  598. if (this.isDragging) {
  599. // 可以在這裡實時更新顯示時間,讓用戶看到拖動到的時間點
  600. // 但不改變實際的 currentTime,避免影響播放邏輯
  601. console.log('實時更新滑動條值:', value);
  602. }
  603. },
  604. // 滑動條拖動過程中的處理 (@changing 事件)
  605. onSliderChanging(value) {
  606. // 第一次觸發 changing 事件時,暫停播放並標記為拖動狀態
  607. if (!this.isDragging) {
  608. if (this.isPlaying) {
  609. this.pauseAudio();
  610. console.log('開始拖動滑動條,暫停播放');
  611. }
  612. this.isDragging = true;
  613. }
  614. // 更新滑動條的值,但不改變實際播放位置
  615. this.sliderValue = value;
  616. console.log('拖動中,滑動條值:', value);
  617. },
  618. // 滑動條拖動結束的處理 (@change 事件)
  619. onSliderChange(value) {
  620. console.log('滑動條變化,跳轉到位置:', value, '是否為拖動:', this.isDragging);
  621. // 如果不是拖動狀態(即單點),需要先暫停播放
  622. if (!this.isDragging && this.isPlaying) {
  623. this.pauseAudio();
  624. console.log('單點滑動條,暫停播放');
  625. }
  626. // 重置拖動狀態
  627. this.isDragging = false;
  628. this.sliderValue = value;
  629. // 跳轉到指定位置,但不自動恢復播放
  630. this.seekToTime(value, false);
  631. console.log('滑動條操作完成,保持暫停狀態,需要手動點擊播放');
  632. },
  633. // 跳轉到指定時間
  634. seekToTime(targetTime, shouldResume = false) {
  635. if (!this.currentPageAudios || this.currentPageAudios.length === 0) {
  636. console.log('沒有音頻數據,無法跳轉');
  637. return;
  638. }
  639. // 確保目標時間在有效範圍內
  640. targetTime = Math.max(0, Math.min(targetTime, this.totalTime));
  641. console.log('跳轉到時間:', targetTime, '秒', '總時長:', this.totalTime, '是否恢復播放:', shouldResume);
  642. let accumulatedTime = 0;
  643. let targetAudioIndex = -1;
  644. let targetAudioTime = 0;
  645. // 找到目標時間對應的音頻片段
  646. for (let i = 0; i < this.currentPageAudios.length; i++) {
  647. const audioDuration = this.currentPageAudios[i].duration || 0;
  648. console.log(`音頻片段 ${i}: 時長=${audioDuration}, 累計時間=${accumulatedTime}, 範圍=[${accumulatedTime}, ${accumulatedTime + audioDuration}]`);
  649. if (targetTime >= accumulatedTime && targetTime <= accumulatedTime + audioDuration) {
  650. targetAudioIndex = i;
  651. targetAudioTime = targetTime - accumulatedTime;
  652. break;
  653. }
  654. accumulatedTime += audioDuration;
  655. }
  656. // 如果沒有找到合適的音頻片段,使用最後一個
  657. if (targetAudioIndex === -1 && this.currentPageAudios.length > 0) {
  658. targetAudioIndex = this.currentPageAudios.length - 1;
  659. targetAudioTime = this.currentPageAudios[targetAudioIndex].duration || 0;
  660. console.log('使用最後一個音頻片段作為目標');
  661. }
  662. console.log('目標音頻索引:', targetAudioIndex, '目標音頻時間:', targetAudioTime);
  663. if (targetAudioIndex === -1) {
  664. console.error('無法找到目標音頻片段');
  665. return;
  666. }
  667. // 如果需要切換到不同的音頻片段
  668. if (targetAudioIndex !== this.currentAudioIndex) {
  669. console.log(`切換音頻片段: ${this.currentAudioIndex} -> ${targetAudioIndex}`);
  670. this.currentAudioIndex = targetAudioIndex;
  671. this.createAudioInstance();
  672. // 等待音頻實例創建完成後再跳轉
  673. this.waitForAudioReady(() => {
  674. if (this.currentAudio) {
  675. this.currentAudio.seek(targetAudioTime);
  676. this.currentTime = targetTime;
  677. console.log('切換音頻並跳轉到:', targetAudioTime, '秒');
  678. // 如果拖動前正在播放,則恢復播放
  679. if (shouldResume) {
  680. this.currentAudio.play();
  681. this.isPlaying = true;
  682. console.log('恢復播放狀態');
  683. }
  684. }
  685. });
  686. } else {
  687. // 在當前音頻片段內跳轉
  688. if (this.currentAudio) {
  689. this.currentAudio.seek(targetAudioTime);
  690. this.currentTime = targetTime;
  691. console.log('在當前音頻內跳轉到:', targetAudioTime, '秒');
  692. // 如果拖動前正在播放,則恢復播放
  693. if (shouldResume) {
  694. this.currentAudio.play();
  695. this.isPlaying = true;
  696. console.log('恢復播放狀態');
  697. }
  698. }
  699. }
  700. },
  701. // 等待音頻實例準備就緒
  702. waitForAudioReady(callback, maxAttempts = 10, currentAttempt = 0) {
  703. if (currentAttempt >= maxAttempts) {
  704. console.error('音頻實例準備超時');
  705. return;
  706. }
  707. if (this.currentAudio && this.currentAudio.src) {
  708. // 音頻實例已準備好
  709. setTimeout(callback, 50); // 稍微延遲確保完全準備好
  710. } else {
  711. // 繼續等待
  712. setTimeout(() => {
  713. this.waitForAudioReady(callback, maxAttempts, currentAttempt + 1);
  714. }, 100);
  715. }
  716. },
  717. // 初始檢測播放速度支持(不依賴音頻實例)
  718. checkInitialPlaybackRateSupport() {
  719. try {
  720. const systemInfo = uni.getSystemInfoSync();
  721. console.log('初始檢測 - 系統信息:', systemInfo);
  722. // 檢查基礎庫版本 - playbackRate需要2.11.0及以上
  723. const SDKVersion = systemInfo.SDKVersion || '0.0.0';
  724. const versionArray = SDKVersion.split('.').map(v => parseInt(v));
  725. const isVersionSupported = versionArray[0] > 2 ||
  726. (versionArray[0] === 2 && versionArray[1] > 11) ||
  727. (versionArray[0] === 2 && versionArray[1] === 11 && versionArray[2] >= 0);
  728. if (!isVersionSupported) {
  729. this.playbackRateSupported = false;
  730. console.log(`初始檢測 - 基礎庫版本過低 (${SDKVersion}),需要2.11.0及以上才支持播放速度控制`);
  731. return;
  732. }
  733. // Android 6以下版本不支持
  734. if (systemInfo.platform === 'android') {
  735. const androidVersion = systemInfo.system.match(/Android (\d+)/);
  736. if (androidVersion && parseInt(androidVersion[1]) < 6) {
  737. this.playbackRateSupported = false;
  738. console.log('初始檢測 - Android版本過低,需要Android 6及以上才支持播放速度控制');
  739. return;
  740. }
  741. }
  742. // 檢查微信原生API是否可用
  743. if (typeof wx === 'undefined' || !wx.createInnerAudioContext) {
  744. console.log('初始檢測 - 微信原生API不可用,可能影響playbackRate支持');
  745. } else {
  746. console.log('初始檢測 - 微信原生API可用');
  747. }
  748. // 如果通過基本檢測,暫時設為支持,等音頻實例創建後再詳細檢測
  749. this.playbackRateSupported = true;
  750. console.log('初始檢測 - 基本條件滿足,等待音頻實例檢測');
  751. } catch (error) {
  752. console.error('初始檢測播放速度支持時出錯:', error);
  753. this.playbackRateSupported = false;
  754. }
  755. },
  756. // 檢查播放速度控制支持
  757. checkPlaybackRateSupport(audio) {
  758. try {
  759. // 檢查基礎庫版本和平台支持
  760. const systemInfo = uni.getSystemInfoSync();
  761. console.log('系統信息:', systemInfo);
  762. console.log('平台:', systemInfo.platform);
  763. console.log('基礎庫版本:', systemInfo.SDKVersion);
  764. console.log('系統版本:', systemInfo.system);
  765. // 根據uni-app文檔,微信小程序需要基礎庫2.11.0+才支持playbackRate
  766. const SDKVersion = systemInfo.SDKVersion || '0.0.0';
  767. const versionArray = SDKVersion.split('.').map(v => parseInt(v));
  768. const isVersionSupported = versionArray[0] > 2 ||
  769. (versionArray[0] === 2 && versionArray[1] > 11) ||
  770. (versionArray[0] === 2 && versionArray[1] === 11 && versionArray[2] >= 0);
  771. console.log('基礎庫版本檢查:', {
  772. version: SDKVersion,
  773. parsed: versionArray,
  774. supported: isVersionSupported
  775. });
  776. if (!isVersionSupported) {
  777. this.playbackRateSupported = false;
  778. console.log(`❌ 基礎庫版本不支持 (${SDKVersion}),微信小程序需要2.11.0+才支持playbackRate`);
  779. return;
  780. }
  781. // Android平台需要6.0+版本支持
  782. if (systemInfo.platform === 'android') {
  783. const androidVersion = systemInfo.system.match(/Android (\d+)/);
  784. console.log('Android版本檢查:', androidVersion);
  785. if (androidVersion && parseInt(androidVersion[1]) < 6) {
  786. this.playbackRateSupported = false;
  787. console.log(`❌ Android版本不支持 (${androidVersion[1]}),需要Android 6+才支持playbackRate`);
  788. return;
  789. }
  790. }
  791. // 檢查音頻實例是否支持playbackRate
  792. console.log('🔍 音頻實例檢查:');
  793. console.log('- playbackRate初始值:', audio.playbackRate);
  794. console.log('- playbackRate類型:', typeof audio.playbackRate);
  795. // 嘗試設置並檢測是否真正支持
  796. const testRate = 1.25;
  797. const originalRate = audio.playbackRate || 1.0;
  798. try {
  799. audio.playbackRate = testRate;
  800. console.log('- 設置測試速度:', testRate);
  801. console.log('- 設置後的值:', audio.playbackRate);
  802. // 檢查設置是否生效
  803. if (Math.abs(audio.playbackRate - testRate) < 0.01) {
  804. this.playbackRateSupported = true;
  805. console.log('✅ playbackRate功能正常');
  806. } else {
  807. this.playbackRateSupported = false;
  808. console.log('❌ playbackRate設置無效,可能不被支持');
  809. }
  810. } catch (error) {
  811. this.playbackRateSupported = false;
  812. console.log('❌ playbackRate設置出錯:', error);
  813. }
  814. } catch (error) {
  815. console.error('檢查播放速度支持時出錯:', error);
  816. this.playbackRateSupported = false;
  817. }
  818. },
  819. formatTime(seconds) {
  820. const mins = Math.floor(seconds / 60);
  821. const secs = Math.floor(seconds % 60);
  822. return `${mins.toString().padStart(2, '0')}:${secs.toString().padStart(2, '0')}`;
  823. },
  824. // 清理音频缓存
  825. clearAudioCache() {
  826. this.audioCache = {};
  827. console.log('音频缓存已清理');
  828. },
  829. // 限制缓存大小,保留最近访问的页面
  830. limitCacheSize(maxSize = 10) {
  831. const cacheKeys = Object.keys(this.audioCache);
  832. if (cacheKeys.length > maxSize) {
  833. // 删除最旧的缓存项
  834. const keysToDelete = cacheKeys.slice(0, cacheKeys.length - maxSize);
  835. keysToDelete.forEach(key => {
  836. delete this.audioCache[key];
  837. });
  838. console.log('缓存大小已限制,删除了', keysToDelete.length, '个缓存项');
  839. }
  840. },
  841. // 自動加載第一頁音頻並播放
  842. async autoLoadAndPlayFirstPage() {
  843. try {
  844. console.log('開始自動加載第一頁音頻');
  845. // 確保當前是第一頁且是文字頁面
  846. if (this.currentPage === 1 && this.isTextPage) {
  847. console.log('當前是第一頁文字頁面,開始加載音頻');
  848. // 加載音頻
  849. await this.getCurrentPageAudio();
  850. // 檢查是否成功加載音頻
  851. if (this.currentPageAudios && this.currentPageAudios.length > 0) {
  852. console.log('音頻加載成功,開始自動播放');
  853. // 稍微延遲一下確保音頻完全準備好
  854. setTimeout(() => {
  855. this.playAudio();
  856. }, 500);
  857. } else {
  858. console.log('第一頁沒有音頻數據');
  859. }
  860. } else {
  861. console.log('當前頁面不是第一頁文字頁面,跳過自動播放');
  862. }
  863. } catch (error) {
  864. console.error('自動加載和播放音頻失敗:', error);
  865. }
  866. },
  867. // 清理音频资源
  868. destroyAudio() {
  869. if (this.currentAudio) {
  870. this.currentAudio.destroy();
  871. this.currentAudio = null;
  872. }
  873. this.isPlaying = false;
  874. this.clearAudioCache();
  875. },
  876. // 暂停音频(页面隐藏时调用)
  877. pauseOnHide() {
  878. this.pauseAudio();
  879. }
  880. },
  881. mounted() {
  882. // 初始檢測播放速度支持
  883. this.checkInitialPlaybackRateSupport();
  884. // 监听音色切换事件
  885. uni.$on('selectVoice', (voiceId) => {
  886. console.log('音色切換:', this.voiceId, '->', voiceId);
  887. // 音色切換時清除所有音頻緩存,因為不同音色的音頻文件不同
  888. this.clearAudioCache();
  889. // 停止當前播放的音頻
  890. if (this.currentAudio) {
  891. this.currentAudio.destroy();
  892. this.currentAudio = null;
  893. }
  894. // 重置音頻狀態
  895. this.currentPageAudios = [];
  896. this.totalTime = 0;
  897. this.hasAudioData = false;
  898. this.isPlaying = false;
  899. this.currentTime = 0;
  900. this.currentAudioIndex = 0;
  901. this.currentHighlightIndex = -1;
  902. this.sliderValue = 0;
  903. this.isDragging = false;
  904. // 通知父组件音频状态变化
  905. this.$emit('audio-state-change', {
  906. hasAudioData: this.hasAudioData,
  907. isLoading: this.isAudioLoading,
  908. currentHighlightIndex: this.currentHighlightIndex
  909. });
  910. // 重新請求當前頁面的音頻數據
  911. if (this.isTextPage) {
  912. console.log('音色切換後重新獲取音頻數據');
  913. this.handleGetAudio();
  914. }
  915. });
  916. },
  917. beforeDestroy() {
  918. // 清理事件监听
  919. uni.$off('selectVoice');
  920. // 清理音频资源
  921. this.destroyAudio();
  922. }
  923. }
  924. </script>
  925. <style lang="scss" scoped>
  926. /* 音频控制栏样式 */
  927. .audio-controls-wrapper {
  928. position: relative;
  929. z-index: 10;
  930. }
  931. .audio-controls {
  932. background: #fff;
  933. padding: 20rpx 40rpx;
  934. border-bottom: 1rpx solid #eee;
  935. transition: transform 0.3s ease;
  936. position: relative;
  937. z-index: 10;
  938. &.audio-hidden {
  939. transform: translateY(100%);
  940. }
  941. }
  942. .audio-time {
  943. display: flex;
  944. align-items: center;
  945. margin-bottom: 20rpx;
  946. }
  947. .time-text {
  948. font-size: 28rpx;
  949. color: #999;
  950. min-width: 80rpx;
  951. }
  952. .progress-container {
  953. flex: 1;
  954. margin: 0 20rpx;
  955. }
  956. .audio-controls-row {
  957. display: flex;
  958. align-items: center;
  959. justify-content: space-between;
  960. }
  961. .control-btn {
  962. display: flex;
  963. align-items: center;
  964. padding: 10rpx;
  965. gap: 8rpx;
  966. }
  967. .control-btn.disabled {
  968. pointer-events: none;
  969. opacity: 0.6;
  970. }
  971. .control-text {
  972. font-size: 28rpx;
  973. color: #4A4A4A;
  974. }
  975. .play-btn {
  976. display: flex;
  977. align-items: center;
  978. justify-content: center;
  979. padding: 10rpx;
  980. }
  981. /* 音频加载状态样式 */
  982. .audio-loading-container {
  983. background: #fff;
  984. padding: 40rpx;
  985. border-bottom: 1rpx solid #eee;
  986. display: flex;
  987. flex-direction: column;
  988. align-items: center;
  989. justify-content: center;
  990. gap: 20rpx;
  991. position: relative;
  992. z-index: 10;
  993. }
  994. .loading-text {
  995. font-size: 28rpx;
  996. color: #999;
  997. }
  998. /* 获取音频按钮样式 */
  999. .audio-get-button-container {
  1000. background: rgba(255, 255, 255, 0.95);
  1001. backdrop-filter: blur(10px);
  1002. padding: 30rpx;
  1003. border-radius: 20rpx;
  1004. border: 2rpx solid #E5E5E5;
  1005. transition: all 0.3s ease;
  1006. position: relative;
  1007. z-index: 10;
  1008. }
  1009. .get-audio-btn {
  1010. display: flex;
  1011. align-items: center;
  1012. justify-content: center;
  1013. gap: 16rpx;
  1014. padding: 20rpx 40rpx;
  1015. background: linear-gradient(135deg, #06DADC 0%, #04B8BA 100%);
  1016. border-radius: 50rpx;
  1017. box-shadow: 0 8rpx 20rpx rgba(6, 218, 220, 0.3);
  1018. transition: all 0.3s ease;
  1019. }
  1020. .get-audio-btn:active {
  1021. transform: scale(0.95);
  1022. box-shadow: 0 4rpx 10rpx rgba(6, 218, 220, 0.2);
  1023. }
  1024. .get-audio-text {
  1025. font-size: 32rpx;
  1026. color: #FFFFFF;
  1027. font-weight: 500;
  1028. }
  1029. </style>