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

2453 lines
82 KiB

1 month ago
1 month ago
2 weeks 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="book-container">
  3. <!-- 条件编译 -->
  4. <!-- #ifndef H5 -->
  5. <uv-status-bar></uv-status-bar>
  6. <!-- 自定义顶部导航栏 -->
  7. <view class="custom-navbar" :class="{ 'navbar-hidden': !showNavbar }">
  8. <uv-status-bar></uv-status-bar>
  9. <view class="navbar-content">
  10. <view class="navbar-left" @click="goBack">
  11. <uv-icon name="arrow-left" size="20" color="#262626"></uv-icon>
  12. </view>
  13. <view class="navbar-title">{{ currentPageTitle }}</view>
  14. </view>
  15. </view>
  16. <!-- #endif -->
  17. <!-- Swiper内容区域 -->
  18. <swiper
  19. class="content-swiper"
  20. :current="currentPage - 1"
  21. @change="onSwiperChange"
  22. >
  23. <swiper-item
  24. v-for="(page, index) in bookPages"
  25. :key="index"
  26. class="swiper-item"
  27. >
  28. <scroll-view scroll-y style="height: 100vh;">
  29. <view scroll-y class="content-area" @click="toggleNavbar">
  30. <view class="title">{{ currentPageTitle }}</view>
  31. <!-- 会员限制页面 -->
  32. <view v-if="!isMember && pagePay[index] === 'Y'" class="member-content" >
  33. <text class="member-title">{{ pageTitles[index] }}</text>
  34. <view class="member-button" @click.stop="unlockBook">
  35. <text class="member-button-text">升级会员解锁</text>
  36. </view>
  37. </view>
  38. <!-- 图片卡片页面 -->
  39. <view class="card-content" v-else-if="pageTypes[index] === '1'">
  40. <view class="card-line">
  41. <image :src="configParamContent('highlight_icon')" class="card-line-image" mode="aspectFill" />
  42. <text class="card-line-text">划线重点</text>
  43. </view>
  44. <view v-for="(item, itemIndex) in page" :key="itemIndex">
  45. <image class="card-image" v-if="item && item.type === 'image'" :src="item.imageUrl" mode="aspectFill"></image>
  46. <view class="english-text-container clickable-text" v-else-if="item && item.type === 'text' && item.language === 'en' && item.content" @click.stop="handleTextClick(item.content, item, index)" >
  47. <text
  48. v-for="(token, tokenIndex) in splitEnglishSentence(item.content)"
  49. :key="tokenIndex"
  50. :class="['english-token', { 'clickable-word': token.isWord && findWordDefinition(token.text) }]"
  51. @click.stop="token.isWord && findWordDefinition(token.text) ? handleWordClick(token.text) : null"
  52. user-select
  53. :style="item.style"
  54. >{{ token.text }}</text>
  55. </view>
  56. <view v-else-if="item && item.type === 'text' && item.language === 'zh' && item.content" @click.stop="handleTextClick(item.content, item, index)">
  57. <text
  58. v-for="(segment, segmentIndex) in processChineseText(item.content)"
  59. :key="segmentIndex"
  60. :class="['chinese-segment', { 'clickable-keyword': segment.isKeyword }]"
  61. @click.stop="segment.isKeyword ? handleChineseKeywordClick(segment.keywordData) : null"
  62. user-select
  63. >{{ segment.text }}</text>
  64. </view>
  65. </view>
  66. </view>
  67. <view v-else>
  68. <view v-for="(item, itemIndex) in page" :key="itemIndex">
  69. <!-- 文本页面 -->
  70. <view v-if="item && item.type === 'text' && item.content" class="text-content" >
  71. <view :class="{ 'lead-text': isTextHighlighted(page, itemIndex) }" @click.stop="handleTextClick(item.content, item, index)">
  72. <text
  73. v-if="!item.isLead"
  74. class="content-text clickable-text"
  75. :style="item.style"
  76. user-select
  77. >
  78. {{ item.content }}
  79. </text>
  80. <view v-else-if="item.isLead" class="content-text clickable-text lead-text" :style="item.style" user-select>{{ item.content }}</view>
  81. </view>
  82. </view>
  83. <!-- 文本页面 -->
  84. <view v-else-if="item.type === 'image'" class="image-container">
  85. <image class="content-image" :src="item.imageUrl" mode="widthFix"></image>
  86. </view>
  87. <!-- 视频页面 -->
  88. <view v-else-if="item.type === 'video'" class="video-content" @click.stop>
  89. <!-- 视频加载状态 -->
  90. <view v-if="videoLoading" class="video-loading">
  91. <text class="loading-text">视频加载中...</text>
  92. </view>
  93. <!-- 视频播放器 -->
  94. <video
  95. v-else
  96. :src="item.url"
  97. class="video-player"
  98. controls
  99. :poster="item.coverUrl"
  100. @loadstart="onVideoLoadStart"
  101. @loadeddata="onVideoLoadStart"
  102. @error="onVideoError"
  103. ></video>
  104. </view>
  105. </view>
  106. </view>
  107. </view>
  108. </scroll-view>
  109. </swiper-item>
  110. </swiper>
  111. <!-- 自定义底部控制栏 -->
  112. <CustomTabbar
  113. :show-navbar="showNavbar"
  114. :current-page="currentPage"
  115. :course-id="courseId"
  116. :voice-id="voiceId"
  117. :book-pages="bookPages"
  118. :is-text-page="isTextPage"
  119. :should-load-audio="shouldLoadAudio"
  120. :is-member="isMember"
  121. :current-page-requires-member="currentPageRequiresMember"
  122. :page-pay="pagePay"
  123. :is-word-audio-playing="isWordAudioPlaying"
  124. @toggle-course-popup="toggleCoursePopup"
  125. @toggle-sound="toggleSound"
  126. @go-to-page="goToPage"
  127. @previous-page="previousPage"
  128. @next-page="nextPage"
  129. @audio-state-change="onAudioStateChange"
  130. @highlight-change="onHighlightChange"
  131. @voice-change-complete="onVoiceChangeComplete"
  132. @voice-change-error="onVoiceChangeError"
  133. ref="customTabbar"
  134. />
  135. <!-- 课程选择弹出窗 -->
  136. <CoursePopup
  137. :style="{zIndex: 10000}"
  138. :course-list="courseList"
  139. :current-course="currentCourse"
  140. :is-reversed="isReversed"
  141. @toggle-sort="toggleSort"
  142. @select-course="selectCourse"
  143. ref="coursePopup"
  144. />
  145. <!-- 释义弹出窗 -->
  146. <MeaningPopup
  147. :style="{zIndex: 10000}"
  148. :current-word-meaning="currentWordMeaning"
  149. @close-meaning-popup="closeMeaningPopup"
  150. @repeat-word-audio="repeatWordAudio"
  151. ref="meaningPopup"
  152. />
  153. </view>
  154. </template>
  155. <script>
  156. import AudioControls from './AudioControls.vue'
  157. import CustomTabbar from './components/CustomTabbar.vue'
  158. import CoursePopup from './components/CoursePopup.vue'
  159. import MeaningPopup from './components/MeaningPopup.vue'
  160. export default {
  161. components: {
  162. AudioControls,
  163. CustomTabbar,
  164. CoursePopup,
  165. MeaningPopup
  166. },
  167. data() {
  168. return {
  169. isMember: false,
  170. memberId: '',
  171. voiceId: null,
  172. courseId: '',
  173. showNavbar: true,
  174. currentPage: 1,
  175. currentCourse: 1, // 当前课程索引
  176. currentWordMeaning: null, // 当前显示的单词释义
  177. isReversed: false, // 是否倒序显示
  178. // 文本高亮相关
  179. currentHighlightIndex: -1, // 当前高亮的文本索引
  180. wordAudioCache: {}, // 單詞語音緩存
  181. currentWordAudio: null, // 當前播放的單詞音頻實例
  182. isWordAudioPlaying: false, // 是否有单词音频正在播放
  183. // 音频状态相关
  184. isAudioLoading: false, // 音频是否正在加载
  185. hasAudioData: false, // 是否有音频数据
  186. audioLoadFailed: false, // 音频加载是否失败
  187. // 视频状态相关
  188. videoLoading: false, // 视频是否正在加载
  189. courseIdList: [],
  190. bookTitle: '',
  191. courseList: [
  192. ],
  193. // 二维数组 代表每个页面
  194. bookPages: [
  195. ],
  196. // 存储每个页面的标题
  197. pageTitles: [],
  198. // 存储每个页面的type信息
  199. pageTypes: [],
  200. // 存储每个页面的单词释义数据
  201. pageWords: [],
  202. // 存储每个页面的付费状态
  203. pagePay: [],
  204. }
  205. },
  206. computed: {
  207. displayCourseList() {
  208. return this.isReversed ? [...this.courseList].reverse() : this.courseList;
  209. },
  210. // 判断当前页面是否为文字类型
  211. isTextPage() {
  212. // 如果是卡片页面(type为'1'),不显示音频控制栏
  213. if (this.currentPageType === '1') {
  214. return false;
  215. }
  216. const currentPageData = this.bookPages[this.currentPage - 1];
  217. // currentPageData是一个数组 其中的一个元素的type是text就会返回true
  218. return currentPageData && currentPageData.some(item => item.type === 'text');
  219. },
  220. // 判断当前页面是否需要加载音频(包括文本页面和卡片页面)
  221. shouldLoadAudio() {
  222. // 文本页面需要加载音频
  223. if (this.isTextPage) {
  224. return true;
  225. }
  226. // 卡片页面(type为'1')也需要加载音频以支持点击播放
  227. if (this.currentPageType === '1') {
  228. return true;
  229. }
  230. return false;
  231. },
  232. // 动态页面标题
  233. currentPageTitle() {
  234. return this.pageTitles[this.currentPage - 1] || this.bookTitle;
  235. },
  236. // 当前页面类型
  237. currentPageType() {
  238. return this.pageTypes[this.currentPage - 1] || '';
  239. },
  240. // 当前页面的单词释义数据
  241. currentPageWords() {
  242. return this.pageWords[this.currentPage - 1] || [];
  243. },
  244. // 当前页面是否需要会员
  245. currentPageRequiresMember() {
  246. return this.pagePay[this.currentPage - 1] === 'Y';
  247. }
  248. },
  249. methods: {
  250. // 视频事件处理方法
  251. onVideoLoadStart() {
  252. console.log("视频开始加载📺📺📺📺📺📺📺📺📺📺");
  253. this.videoLoading = true;
  254. },
  255. onVideoCanPlay() {
  256. console.log("视频可以播放");
  257. this.videoLoading = false;
  258. },
  259. onVideoError() {
  260. this.videoLoading = false;
  261. uni.showToast({
  262. title: '视频加载失败',
  263. icon: 'none',
  264. duration: 2000
  265. });
  266. },
  267. // 獲取用戶會員信息 判斷是否和傳參傳過來的會員id相同
  268. async getMemberInfo(){
  269. const memberRes = await this.$api.member.getUserMemberInfo()
  270. if (memberRes.code === 200) {
  271. this.isMember = memberRes.result.map(item => item.memberId).includes(this.memberId)
  272. console.log('isMember:', this.isMember);
  273. }
  274. },
  275. // 处理AudioControls组件的事件
  276. onAudioStateChange(audioState) {
  277. // 更新高亮状态
  278. this.currentHighlightIndex = audioState.currentHighlightIndex;
  279. // 更新音频加载状态(用于控制UI显示)
  280. if (audioState.hasOwnProperty('isLoading')) {
  281. this.isAudioLoading = audioState.isLoading;
  282. }
  283. // 更新音频数据状态
  284. if (audioState.hasOwnProperty('hasAudioData')) {
  285. this.hasAudioData = audioState.hasAudioData;
  286. }
  287. // 更新音频加载失败状态
  288. if (audioState.hasOwnProperty('audioLoadFailed')) {
  289. this.audioLoadFailed = audioState.audioLoadFailed;
  290. }
  291. },
  292. // 处理音色切换完成事件
  293. onVoiceChangeComplete(data) {
  294. console.log('音色切换完成:', data);
  295. // 可以在这里添加一些UI反馈,比如显示切换成功的提示
  296. if (data.hasAudioData) {
  297. console.log('新音色当前页面音频已加载完成');
  298. } else {
  299. console.log('当前页面没有音频数据');
  300. }
  301. // 如果启用了预加载所有页面
  302. if (data.preloadAllPages) {
  303. console.log('正在后台预加载所有页面的新音色音频...');
  304. // 可以显示一个提示,告诉用户正在后台加载
  305. uni.showToast({
  306. title: '正在加载新音色...',
  307. icon: 'loading',
  308. duration: 2000
  309. });
  310. }
  311. },
  312. // 处理音色切换错误事件
  313. onVoiceChangeError(error) {
  314. console.error('音色切换失败:', error);
  315. // 可以在这里显示错误提示给用户
  316. uni.showToast({
  317. title: '音色切换失败,请重试',
  318. icon: 'none',
  319. duration: 2000
  320. });
  321. },
  322. // 处理文本点击事件
  323. handleTextClick(textContent, item, pageIndex) {
  324. // console.log('🎯 ===== 文本点击事件开始 =====');
  325. // console.log('📝 点击文本:', textContent);
  326. // console.log('📄 textContent类型:', typeof textContent);
  327. // console.log('❓ textContent是否为undefined:', textContent === undefined);
  328. // console.log('📦 完整item对象:', item);
  329. // console.log('📝 item.content:', item ? item.content : 'item为空');
  330. // console.log('📖 当前页面索引:', this.currentPage);
  331. // console.log('👆 点击的页面索引:', pageIndex);
  332. // console.log('📊 当前页面类型:', this.currentPageType);
  333. // console.log('📄 是否为文本页面:', this.isTextPage);
  334. // console.log('📋 当前页面数据:', this.bookPages[this.currentPage - 1]);
  335. // console.log('📏 页面数据长度:', this.bookPages[this.currentPage - 1] ? this.bookPages[this.currentPage - 1].length : '页面不存在');
  336. // 检查音频播放状态
  337. // console.log('🎵 ===== 音频状态检查 =====');
  338. // console.log(' isWordAudioPlaying:', this.isWordAudioPlaying);
  339. // console.log(' currentWordAudio存在:', !!this.currentWordAudio);
  340. // console.log(' currentWordMeaning存在:', !!this.currentWordMeaning);
  341. if (this.isWordAudioPlaying) {
  342. // console.log('⚠️ 检测到单词音频正在播放状态,这可能会阻止句子音频播放');
  343. // console.log('🔄 尝试重置音频播放状态...');
  344. this.isWordAudioPlaying = false;
  345. // console.log('✅ 音频播放状态已重置');
  346. }
  347. // 检查是否点击的是当前页面
  348. if (pageIndex !== undefined && pageIndex !== this.currentPage - 1) {
  349. console.warn('⚠️ 点击的不是当前页面,忽略点击事件');
  350. // console.log(` 期望页面: ${this.currentPage - 1}, 点击页面: ${pageIndex}`);
  351. return;
  352. }
  353. // 验证参数有效性
  354. if (!item) {
  355. console.error('❌ handleTextClick: item参数为空');
  356. uni.showToast({
  357. title: '数据错误,请刷新页面',
  358. icon: 'none'
  359. });
  360. return;
  361. }
  362. // 如果textContent为undefined,尝试从item中获取
  363. if (!textContent && item && item.content) {
  364. textContent = item.content;
  365. // console.log('🔄 从item中获取到content:', textContent);
  366. }
  367. // 最终验证textContent
  368. if (!textContent || typeof textContent !== 'string' || textContent.trim() === '') {
  369. console.error('❌ handleTextClick: 无效的文本内容', textContent);
  370. // console.log(' textContent:', textContent);
  371. // console.log(' 类型:', typeof textContent);
  372. // console.log(' 是否为空字符串:', textContent === '');
  373. // console.log(' trim后是否为空:', textContent && textContent.trim() === '');
  374. uni.showToast({
  375. title: '文本内容无效',
  376. icon: 'none'
  377. });
  378. return;
  379. }
  380. // console.log('✅ 文本内容验证通过:', textContent);
  381. // console.log(' 文本长度:', textContent.length);
  382. // console.log(' 文本预览:', textContent.substring(0, 50) + (textContent.length > 50 ? '...' : ''));
  383. // 检查是否有音频控制组件的引用
  384. // console.log('🔍 检查音频控制组件引用...');
  385. // console.log(' customTabbar存在:', !!this.$refs.customTabbar);
  386. if (!this.$refs.customTabbar) {
  387. console.error('❌ customTabbar引用不存在');
  388. uni.showToast({
  389. title: '音频控制组件未准备好',
  390. icon: 'none'
  391. });
  392. return;
  393. }
  394. // console.log(' audioControls存在:', !!this.$refs.customTabbar.$refs.audioControls);
  395. if (!this.$refs.customTabbar.$refs.audioControls) {
  396. console.error('❌ audioControls引用不存在');
  397. uni.showToast({
  398. title: '音频控制组件未准备好',
  399. icon: 'none'
  400. });
  401. return;
  402. }
  403. // 检查当前页面是否为文本页面或卡片页面
  404. // 卡片页面(type为'1')现在也支持整句音频播放
  405. console.log('🔍 检查页面类型支持...');
  406. console.log(' isTextPage:', this.isTextPage);
  407. console.log(' currentPageType:', this.currentPageType);
  408. console.log(' 当前页面索引:', this.currentPage);
  409. console.log(' 页面类型数组:', this.pageTypes);
  410. // 特别针对划线重点页面的调试
  411. if (this.currentPageType === '1') {
  412. console.log('📝 当前是划线重点页面,点击的文本:', textContent);
  413. console.log('📝 文本长度:', textContent.length);
  414. console.log('📝 文本前50字符:', textContent.substring(0, 50));
  415. }
  416. if (!this.isTextPage && this.currentPageType !== '1') {
  417. console.warn('⚠️ 当前页面不是文本页面或卡片页面');
  418. console.log(` isTextPage: ${this.isTextPage}, currentPageType: ${this.currentPageType}`);
  419. uni.showToast({
  420. title: '当前页面不支持音频播放',
  421. icon: 'none'
  422. });
  423. return;
  424. }
  425. console.log('✅ 页面类型检查通过,准备播放音频');
  426. // 获取音频控制组件实例
  427. const audioControls = this.$refs.customTabbar.$refs.audioControls;
  428. // 检查音频是否正在加载中
  429. if (audioControls.isAudioLoading) {
  430. console.log('⏳ 音频正在加载中,等待加载完成后再播放');
  431. uni.showToast({
  432. title: '音频正在加载中,请稍后再试',
  433. icon: 'loading',
  434. duration: 1500
  435. });
  436. // 等待音频加载完成后自动播放
  437. const checkAndPlay = () => {
  438. if (!audioControls.isAudioLoading && audioControls.currentPageAudios.length > 0) {
  439. console.log('✅ 音频加载完成,开始播放指定音频');
  440. const success = audioControls.playSpecificAudio(textContent);
  441. if (!success) {
  442. console.error('❌ 音频加载完成后播放失败');
  443. }
  444. } else if (!audioControls.isAudioLoading) {
  445. console.error('❌ 音频加载完成但没有音频数据');
  446. uni.showToast({
  447. title: '当前页面没有音频内容',
  448. icon: 'none'
  449. });
  450. } else {
  451. // 继续等待
  452. setTimeout(checkAndPlay, 500);
  453. }
  454. };
  455. // 延迟检查,给音频加载一些时间
  456. setTimeout(checkAndPlay, 1000);
  457. return;
  458. }
  459. // 检查是否有音频数据
  460. if (!audioControls.currentPageAudios || audioControls.currentPageAudios.length === 0) {
  461. console.warn('⚠️ 当前页面没有音频数据,尝试重新加载');
  462. uni.showToast({
  463. title: '正在重新加载音频...',
  464. icon: 'loading'
  465. });
  466. // 尝试重新加载音频
  467. audioControls.getCurrentPageAudio();
  468. // 等待重新加载完成后播放
  469. const retryPlay = () => {
  470. if (!audioControls.isAudioLoading && audioControls.currentPageAudios.length > 0) {
  471. console.log('✅ 音频重新加载完成,开始播放');
  472. const success = audioControls.playSpecificAudio(textContent);
  473. if (!success) {
  474. console.error('❌ 音频重新加载后播放失败');
  475. }
  476. } else if (!audioControls.isAudioLoading) {
  477. console.error('❌ 音频重新加载失败');
  478. uni.showToast({
  479. title: '音频加载失败,请检查网络连接',
  480. icon: 'none'
  481. });
  482. } else {
  483. // 继续等待
  484. setTimeout(retryPlay, 500);
  485. }
  486. };
  487. setTimeout(retryPlay, 1500);
  488. return;
  489. }
  490. // 调用AudioControls组件的播放指定音频方法
  491. console.log('🚀 调用 playSpecificAudio,音频数据长度:', audioControls.currentPageAudios.length);
  492. const success = audioControls.playSpecificAudio(textContent);
  493. // console.log('🎵 playSpecificAudio 返回结果:', success);
  494. if (success) {
  495. // console.log('✅ 成功播放指定音频段落');
  496. } else {
  497. console.error('❌ 播放指定音频段落失败');
  498. // console.log('💡 失败可能原因:');
  499. // console.log(' 1. 文本内容与音频数据不匹配');
  500. // console.log(' 2. 音频数据尚未加载完成');
  501. // console.log(' 3. 音频文件路径错误或文件损坏');
  502. // console.log(' 4. 网络连接问题');
  503. }
  504. // console.log('🎯 ===== 文本点击事件结束 =====');
  505. },
  506. onHighlightChange(highlightData) {
  507. // 兼容旧格式(直接传递索引)和新格式(传递对象)
  508. if (typeof highlightData === 'number') {
  509. // 旧格式:直接是索引
  510. this.currentHighlightIndex = highlightData;
  511. } else if (typeof highlightData === 'object' && highlightData !== null) {
  512. // 新格式:包含详细信息的对象
  513. this.currentHighlightIndex = highlightData.highlightIndex;
  514. // 可以在这里处理分段音频的额外信息
  515. if (highlightData.isSegmented) {
  516. // console.log('分段音频高亮:', {
  517. // highlightIndex: highlightData.highlightIndex,
  518. // segmentIndex: highlightData.segmentIndex,
  519. // startIndex: highlightData.startIndex,
  520. // endIndex: highlightData.endIndex,
  521. // currentText: highlightData.currentText
  522. // });
  523. }
  524. } else {
  525. // 清除高亮
  526. this.currentHighlightIndex = -1;
  527. }
  528. },
  529. // 获取音色列表 拿第一个做默认的音色id
  530. async getVoiceList() {
  531. const voiceRes = await this.$api.music.list()
  532. if(voiceRes.code === 200){
  533. // console.log('音色列表API返回:', voiceRes.result);
  534. // console.log('第一个音色数据:', voiceRes.result[0]);
  535. this.voiceId = Number(voiceRes.result[0].voiceType)
  536. // console.log('获取默认音色ID:', this.voiceId, '类型:', typeof this.voiceId);
  537. }
  538. },
  539. toggleNavbar() {
  540. this.showNavbar = !this.showNavbar
  541. },
  542. goBack() {
  543. uni.navigateBack()
  544. },
  545. toggleCoursePopup() {
  546. if (this.$refs.coursePopup) {
  547. this.$refs.coursePopup.open()
  548. }
  549. // console.log('123123123');
  550. },
  551. toggleSort() {
  552. this.isReversed = !this.isReversed
  553. },
  554. selectCourse(courseId) {
  555. this.currentCourse = courseId
  556. // 这里可以添加切换课程的逻辑
  557. // console.log('选择课程:', courseId)
  558. this.getCourseList(courseId)
  559. },
  560. showWordMeaning() {
  561. if (this.$refs.meaningPopup) {
  562. this.$refs.meaningPopup.open()
  563. }
  564. },
  565. closeMeaningPopup() {
  566. console.log('🔄 ===== 关闭重点单词弹窗 =====');
  567. console.log('📱 清理弹窗数据...');
  568. this.currentWordMeaning = null;
  569. // 重置音频播放状态,确保后续句子点击能正常播放
  570. console.log('🎵 检查音频播放状态...');
  571. console.log(' 当前 isWordAudioPlaying:', this.isWordAudioPlaying);
  572. console.log(' 当前 currentWordAudio:', !!this.currentWordAudio);
  573. if (this.isWordAudioPlaying) {
  574. console.log('🛑 重置音频播放状态');
  575. this.isWordAudioPlaying = false;
  576. // 如果有正在播放的音频,停止它
  577. if (this.currentWordAudio) {
  578. console.log('🛑 停止当前播放的单词音频');
  579. try {
  580. this.currentWordAudio.pause();
  581. this.currentWordAudio.destroy();
  582. console.log('✅ 单词音频已停止并销毁');
  583. } catch (error) {
  584. console.log('⚠️ 停止单词音频时出现异常:', error);
  585. }
  586. this.currentWordAudio = null;
  587. }
  588. }
  589. console.log('✅ 弹窗关闭完成,音频状态已重置');
  590. console.log('🏁 ===== 关闭重点单词弹窗结束 =====');
  591. },
  592. // 将英文句子分割成单词数组
  593. splitEnglishSentence(sentence) {
  594. // 使用正则表达式分割句子,保留标点符号
  595. const tokens = sentence.match(/\b\w+\b|[^\w\s]/g) || [];
  596. return tokens.map((token, index) => ({
  597. text: token,
  598. index: index,
  599. isWord: /\b\w+\b/.test(token), // 判断是否为单词
  600. hasDefinition: false // 是否有释义,稍后会设置
  601. }));
  602. },
  603. // 查找单词释义
  604. findWordDefinition(word) {
  605. const currentPageWords = this.pageWords[this.currentPage - 1] || [];
  606. // 不区分大小写匹配
  607. return currentPageWords.find(wordData =>
  608. wordData.word.toLowerCase() === word.toLowerCase()
  609. );
  610. },
  611. // 处理中文文本,标记重点词汇
  612. processChineseText(text) {
  613. const currentPageWords = this.pageWords[this.currentPage - 1] || [];
  614. if (!text || currentPageWords.length === 0) {
  615. return [{ text: text, isKeyword: false, keywordData: null }];
  616. }
  617. // 创建一个数组来存储处理后的文本片段
  618. const segments = [];
  619. let currentIndex = 0;
  620. // 按照重点词汇的长度排序,优先匹配较长的词汇
  621. const sortedWords = [...currentPageWords].sort((a, b) => b.word.length - a.word.length);
  622. while (currentIndex < text.length) {
  623. let matched = false;
  624. // 尝试匹配重点词汇
  625. for (const wordData of sortedWords) {
  626. const keyword = wordData.word;
  627. if (text.substr(currentIndex, keyword.length) === keyword) {
  628. // 找到匹配的重点词汇
  629. segments.push({
  630. text: keyword,
  631. isKeyword: true,
  632. keywordData: wordData
  633. });
  634. currentIndex += keyword.length;
  635. matched = true;
  636. break;
  637. }
  638. }
  639. if (!matched) {
  640. // 没有匹配到重点词汇,添加单个字符
  641. segments.push({
  642. text: text[currentIndex],
  643. isKeyword: false,
  644. keywordData: null
  645. });
  646. currentIndex++;
  647. }
  648. }
  649. // 合并相邻的非重点词汇片段
  650. const mergedSegments = [];
  651. let currentSegment = null;
  652. for (const segment of segments) {
  653. if (segment.isKeyword) {
  654. // 如果当前有未完成的非重点词汇片段,先添加它
  655. if (currentSegment) {
  656. mergedSegments.push(currentSegment);
  657. currentSegment = null;
  658. }
  659. // 添加重点词汇
  660. mergedSegments.push(segment);
  661. } else {
  662. // 非重点词汇,合并到当前片段
  663. if (currentSegment) {
  664. currentSegment.text += segment.text;
  665. } else {
  666. currentSegment = { ...segment };
  667. }
  668. }
  669. }
  670. // 添加最后的非重点词汇片段
  671. if (currentSegment) {
  672. mergedSegments.push(currentSegment);
  673. }
  674. return mergedSegments;
  675. },
  676. async playWordAudio(word) {
  677. try {
  678. console.log('🎵 ===== 开始播放单词语音 =====');
  679. console.log('📝 目标单词:', word);
  680. console.log('🎧 当前音频实例状态:', this.currentWordAudio ? '存在' : '不存在');
  681. console.log('🔊 voiceId:', this.voiceId, '类型:', typeof this.voiceId);
  682. // 检查是否有音频正在播放,如果有则停止
  683. if (this.isWordAudioPlaying) {
  684. console.log('⚠️ 有音频正在播放,将停止当前播放');
  685. this.isWordAudioPlaying = false;
  686. }
  687. // 🎯 全局音频管理:停止主音频播放,确保只有一个音频播放
  688. if (this.$refs.customTabbar && this.$refs.customTabbar.$refs.audioControls) {
  689. const audioControls = this.$refs.customTabbar.$refs.audioControls;
  690. if (audioControls.isPlaying) {
  691. console.log('🔄 停止主音频播放,准备播放单词音频');
  692. audioControls.pauseAudio();
  693. }
  694. }
  695. // 停止當前播放的單詞語音
  696. if (this.currentWordAudio) {
  697. console.log('🛑 停止当前播放的音频');
  698. try {
  699. // 先暂停,再销毁,确保清理完整
  700. this.currentWordAudio.pause();
  701. this.currentWordAudio.destroy();
  702. console.log('✅ 音频已暂停并销毁');
  703. } catch (error) {
  704. console.log('⚠️ 清理音频时出现异常:', error);
  705. }
  706. this.currentWordAudio = null;
  707. this.isWordAudioPlaying = false;
  708. console.log('✅ currentWordAudio 已重置,播放状态已清除');
  709. } else {
  710. console.log('ℹ️ 没有正在播放的音频需要停止');
  711. }
  712. // 🎯 确保音色ID已加载完成后再获取音频
  713. if (!this.voiceId || this.voiceId === '' || this.voiceId === null || this.voiceId === undefined) {
  714. console.log('⚠️ 音色ID未加载,无法获取单词音频');
  715. console.log(' voiceId:', this.voiceId);
  716. console.log(' 类型:', typeof this.voiceId);
  717. uni.showToast({
  718. title: '音色未加载,请稍后重试',
  719. icon: 'none',
  720. duration: 2000
  721. });
  722. return;
  723. }
  724. console.log('✅ 单词音频音色ID验证通过:', this.voiceId);
  725. // 調用語音轉換API
  726. console.log('🌐 准备调用 textToVoice API');
  727. console.log('📋 API参数:', { text: word, voiceType: this.voiceId });
  728. const audioRes = await this.$api.music.textToVoice({
  729. text: word,
  730. voiceType: this.voiceId
  731. });
  732. console.log('📡 API响应:', audioRes);
  733. console.log('✅ API调用完成');
  734. // 檢查響應並播放音頻
  735. if (audioRes && audioRes.result && audioRes.result.url) {
  736. console.log('🎵 API响应有效,准备播放音频');
  737. // 新格式:使用返回的url字段
  738. const audioUrl = audioRes.result.url;
  739. console.log('🔗 音频URL:', audioUrl);
  740. // 創建並播放音頻
  741. console.log('🎧 创建音频实例');
  742. const audio = uni.createInnerAudioContext();
  743. audio.src = audioUrl;
  744. console.log('✅ 音频实例创建完成,设置URL');
  745. // 添加音频加载完成监听器
  746. audio.onCanplay(() => {
  747. console.log('🎵 音频已加载完成,可以播放');
  748. });
  749. audio.onPlay(() => {
  750. console.log('🎵 音频开始播放:', word);
  751. console.log('🎧 当前音频实例已激活');
  752. this.isWordAudioPlaying = true;
  753. console.log('✅ 播放状态已设置为 true');
  754. });
  755. audio.onEnded(() => {
  756. console.log('✅ 音频播放完成:', word);
  757. console.log('🧹 准备清理音频实例');
  758. this.isWordAudioPlaying = false;
  759. console.log('🔄 播放状态已重置为 false');
  760. audio.destroy();
  761. if (this.currentWordAudio === audio) {
  762. this.currentWordAudio = null;
  763. console.log('🔄 currentWordAudio 已重置为 null');
  764. } else {
  765. console.log('⚠️ 音频实例不匹配,可能已被替换');
  766. }
  767. });
  768. audio.onError((error) => {
  769. console.error('❌ 音频播放失败:', error);
  770. console.log('🔍 错误详情:', JSON.stringify(error));
  771. console.log('🧹 准备清理失败的音频实例');
  772. this.isWordAudioPlaying = false;
  773. console.log('🔄 播放状态已重置为 false (错误)');
  774. try {
  775. audio.destroy();
  776. console.log('✅ 失败的音频实例已销毁');
  777. } catch (destroyError) {
  778. console.error('⚠️ 销毁音频实例时出错:', destroyError);
  779. }
  780. if (this.currentWordAudio === audio) {
  781. this.currentWordAudio = null;
  782. console.log('🔄 currentWordAudio 已重置为 null (错误)');
  783. } else {
  784. console.log('⚠️ 音频实例不匹配,可能已被替换');
  785. }
  786. uni.showToast({
  787. title: '語音播放失敗',
  788. icon: 'none'
  789. });
  790. });
  791. // 保存當前音頻實例並播放
  792. console.log('💾 保存音频实例到 currentWordAudio');
  793. this.currentWordAudio = audio;
  794. // 添加一个小延迟确保音频实例完全准备好
  795. console.log('⏱️ 等待音频实例准备...');
  796. setTimeout(() => {
  797. if (this.currentWordAudio === audio) {
  798. console.log('🚀 开始播放音频');
  799. try {
  800. audio.play();
  801. console.log('✅ 音频播放命令已发送');
  802. } catch (playError) {
  803. console.error('❌ 播放命令失败:', playError);
  804. uni.showToast({
  805. title: '音频播放失败',
  806. icon: 'none'
  807. });
  808. }
  809. } else {
  810. console.log('⚠️ 音频实例已被替换,取消播放');
  811. }
  812. }, 100);
  813. } else {
  814. console.error('❌ API响应无效:', audioRes);
  815. console.log('🔍 响应结构检查:');
  816. console.log(' audioRes存在:', !!audioRes);
  817. console.log(' audioRes.result存在:', !!(audioRes && audioRes.result));
  818. console.log(' audioRes.result.url存在:', !!(audioRes && audioRes.result && audioRes.result.url));
  819. uni.showToast({
  820. title: '語音播放失敗',
  821. icon: 'none'
  822. });
  823. }
  824. } catch (error) {
  825. console.error('❌ 播放单词语音异常:', error);
  826. console.log('🔍 异常详情:', error.message || error);
  827. uni.showToast({
  828. title: '語音播放失敗',
  829. icon: 'none'
  830. });
  831. }
  832. console.log('🏁 ===== playWordAudio 方法结束 =====');
  833. },
  834. // 重複播放單詞語音(用於釋義彈窗中的揚聲器圖標)
  835. repeatWordAudio() {
  836. if (this.currentWordMeaning && this.currentWordMeaning.word) {
  837. // 将单词和解释合并后播放音频
  838. const combinedText = `${this.currentWordMeaning.word}${this.currentWordMeaning.meaning || ''}`;
  839. console.log('重複播放合併文本:', combinedText);
  840. this.playWordAudio(combinedText);
  841. } else {
  842. console.warn('沒有當前單詞可以播放');
  843. }
  844. },
  845. // 处理单词点击事件
  846. handleWordClick(word) {
  847. console.log('🎯 ===== 单词点击事件开始 =====');
  848. console.log('📝 点击的单词:', word);
  849. console.log('🔍 开始查找单词释义...');
  850. const definition = this.findWordDefinition(word);
  851. console.log('📖 查找结果:', definition ? '找到释义' : '未找到释义');
  852. console.log('📋 完整释义数据:', definition);
  853. if (definition) {
  854. console.log('✅ 找到单词释义,准备设置弹窗数据');
  855. this.currentWordMeaning = {
  856. word: definition.word,
  857. phonetic: definition.soundmark || '',
  858. partOfSpeech: '', // 可以根据需要添加词性
  859. meaning: definition.paraphrase || '',
  860. knowledgeGain: definition.knowledge || '',
  861. image: definition.image || ''
  862. };
  863. console.log('📋 弹窗数据已设置:', this.currentWordMeaning);
  864. // 将单词和解释合并后播放音频
  865. const combinedText = `${word}${definition.paraphrase || ''}`;
  866. console.log('🎵 准备播放合并文本:', combinedText);
  867. this.playWordAudio(combinedText);
  868. console.log('📱 显示单词释义弹窗');
  869. this.showWordMeaning();
  870. } else {
  871. console.log('⚠️ 未找到单词释义:', word);
  872. console.log('🎵 只播放单词本身');
  873. // 如果没有释义,只播放单词
  874. if (word) {
  875. this.playWordAudio(word);
  876. } else {
  877. console.log('❌ 单词为空,无法播放');
  878. }
  879. }
  880. console.log('🏁 ===== handleWordClick 方法结束 =====');
  881. },
  882. // 处理中文重点词汇点击事件
  883. handleChineseKeywordClick(keywordData) {
  884. console.log('🎯 ===== 中文重点词汇点击事件开始 =====');
  885. console.log('📝 点击的词汇数据:', keywordData);
  886. console.log('📖 词汇:', keywordData ? keywordData.word : '无');
  887. if (keywordData) {
  888. console.log('✅ 词汇数据有效,准备设置弹窗数据');
  889. this.currentWordMeaning = {
  890. word: keywordData.word,
  891. phonetic: keywordData.soundmark || '',
  892. partOfSpeech: '', // 可以根据需要添加词性
  893. meaning: keywordData.paraphrase || '',
  894. knowledgeGain: keywordData.knowledge || ''
  895. };
  896. console.log('📋 弹窗数据已设置:', this.currentWordMeaning);
  897. // 将词汇和解释合并后播放音频
  898. const combinedText = `${keywordData.word}${keywordData.paraphrase || ''}`;
  899. console.log('🎵 准备播放中文合并文本:', combinedText);
  900. this.playWordAudio(combinedText);
  901. console.log('📱 显示词汇释义弹窗');
  902. this.showWordMeaning();
  903. } else {
  904. console.log('❌ 未找到中文词汇释义数据');
  905. }
  906. console.log('🏁 ===== handleChineseKeywordClick 方法结束 =====');
  907. },
  908. // 计算音频总时长
  909. async calculateTotalDuration() {
  910. let totalDuration = 0;
  911. for (let i = 0; i < this.currentPageAudios.length; i++) {
  912. const audio = this.currentPageAudios[i];
  913. // 優先使用API返回的時長信息
  914. if (audio.duration && audio.duration > 0) {
  915. console.log(`使用API返回的時長 ${i + 1}:`, audio.duration, '秒');
  916. totalDuration += audio.duration;
  917. continue;
  918. }
  919. // 如果沒有API時長信息,嘗試獲取音頻時長
  920. try {
  921. const duration = await this.getAudioDuration(audio.url);
  922. audio.duration = duration;
  923. totalDuration += duration;
  924. console.log(`獲取到音頻時長 ${i + 1}:`, duration, '秒');
  925. } catch (error) {
  926. console.error('获取音频时长失败:', error);
  927. // 如果无法获取时长,根據文字長度估算(更精確的估算)
  928. const textLength = audio.text.length;
  929. // 假設每分鐘可以讀150-200個字符,這裡用180作為平均值
  930. const estimatedDuration = Math.max(2, textLength / 3); // 每3個字符約1秒
  931. audio.duration = estimatedDuration;
  932. totalDuration += estimatedDuration;
  933. console.log(`估算音頻時長 ${i + 1}:`, estimatedDuration, '秒 (文字長度:', textLength, ')');
  934. }
  935. }
  936. this.totalTime = totalDuration;
  937. console.log('音频总时长:', totalDuration, '秒');
  938. },
  939. // 获取音频时长
  940. getAudioDuration(audioUrl) {
  941. return new Promise((resolve, reject) => {
  942. const audio = uni.createInnerAudioContext();
  943. audio.src = audioUrl;
  944. let resolved = false;
  945. // 监听音频加载完成事件
  946. audio.onCanplay(() => {
  947. console.log('音频可以播放,duration:', audio.duration);
  948. if (!resolved && audio.duration && audio.duration > 0) {
  949. resolved = true;
  950. resolve(audio.duration);
  951. audio.destroy();
  952. }
  953. });
  954. // 监听音频元数据加载完成事件
  955. audio.onLoadedmetadata = () => {
  956. console.log('音频元数据加载完成,duration:', audio.duration);
  957. if (!resolved && audio.duration && audio.duration > 0) {
  958. resolved = true;
  959. resolve(audio.duration);
  960. audio.destroy();
  961. }
  962. };
  963. // 监听音频时长更新事件
  964. audio.onDurationChange = () => {
  965. console.log('音频时长更新,duration:', audio.duration);
  966. if (!resolved && audio.duration && audio.duration > 0) {
  967. resolved = true;
  968. resolve(audio.duration);
  969. audio.destroy();
  970. }
  971. };
  972. // 如果以上方法都無法獲取時長,嘗試播放一小段來獲取時長
  973. audio.onPlay(() => {
  974. console.log('音频开始播放,duration:', audio.duration);
  975. if (!resolved) {
  976. setTimeout(() => {
  977. if (!resolved && audio.duration && audio.duration > 0) {
  978. resolved = true;
  979. resolve(audio.duration);
  980. audio.destroy();
  981. }
  982. }, 100); // 播放100ms後檢查時長
  983. }
  984. });
  985. audio.onError((error) => {
  986. console.error('音频加载失败:', error);
  987. if (!resolved) {
  988. resolved = true;
  989. reject(error);
  990. audio.destroy();
  991. }
  992. });
  993. // 設置較長的超時時間,並在超時前嘗試播放
  994. setTimeout(() => {
  995. if (!resolved) {
  996. console.log('嘗試播放音頻以獲取時長');
  997. audio.play();
  998. }
  999. }, 1000);
  1000. // 最終超時處理
  1001. setTimeout(() => {
  1002. if (!resolved) {
  1003. console.warn('獲取音頻時長超時,使用默認值');
  1004. resolved = true;
  1005. reject(new Error('获取音频时长超时'));
  1006. audio.destroy();
  1007. }
  1008. }, 5000);
  1009. });
  1010. },
  1011. // 音频控制方法
  1012. togglePlay() {
  1013. if (this.currentPageAudios.length === 0) {
  1014. uni.showToast({
  1015. title: '当前页面没有音频内容',
  1016. icon: 'none'
  1017. });
  1018. return;
  1019. }
  1020. if (this.isPlaying) {
  1021. this.pauseAudio();
  1022. } else {
  1023. this.playAudio();
  1024. }
  1025. },
  1026. // 播放音频
  1027. playAudio() {
  1028. if (this.currentPageAudios.length === 0) return;
  1029. // 如果没有当前音频实例或者需要切换音频
  1030. if (!this.currentAudio || this.currentAudio.src !== this.currentPageAudios[this.currentAudioIndex].url) {
  1031. this.createAudioInstance();
  1032. }
  1033. this.currentAudio.play();
  1034. this.isPlaying = true;
  1035. // 更新高亮状态
  1036. this.updateHighlightIndex();
  1037. },
  1038. // 暂停音频
  1039. pauseAudio() {
  1040. if (this.currentAudio) {
  1041. this.currentAudio.pause();
  1042. }
  1043. this.isPlaying = false;
  1044. // 暂停时清除高亮
  1045. this.currentHighlightIndex = -1;
  1046. },
  1047. // 创建音频实例
  1048. createAudioInstance() {
  1049. // 销毁之前的音频实例
  1050. if (this.currentAudio) {
  1051. console.log('销毁原来的音频');
  1052. this.currentAudio.destroy();
  1053. }
  1054. // 優先使用微信原生API以支持playbackRate
  1055. let audio;
  1056. if (typeof wx !== 'undefined' && wx.createInnerAudioContext) {
  1057. console.log('使用微信原生音頻API');
  1058. audio = wx.createInnerAudioContext();
  1059. } else {
  1060. console.log('使用uni-app音頻API');
  1061. audio = uni.createInnerAudioContext();
  1062. }
  1063. audio.src = this.currentPageAudios[this.currentAudioIndex].url;
  1064. // 在音頻可以播放時檢測playbackRate支持
  1065. audio.onCanplay(() => {
  1066. console.log('🎵 音頻可以播放,開始檢測playbackRate支持');
  1067. this.checkPlaybackRateSupport(audio);
  1068. // 檢測完成後,設置用戶期望的播放速度
  1069. setTimeout(() => {
  1070. if (this.playbackRateSupported) {
  1071. console.log('設置播放速度:', this.playSpeed);
  1072. audio.playbackRate = this.playSpeed;
  1073. // 驗證設置結果
  1074. setTimeout(() => {
  1075. console.log('最終播放速度:', audio.playbackRate);
  1076. if (Math.abs(audio.playbackRate - this.playSpeed) > 0.01) {
  1077. console.log('⚠️ 播放速度設置可能未生效');
  1078. } else {
  1079. console.log('✅ 播放速度設置成功');
  1080. }
  1081. }, 50);
  1082. } else {
  1083. console.log('❌ 當前環境不支持播放速度控制');
  1084. }
  1085. }, 50);
  1086. });
  1087. // 音频事件监听
  1088. audio.onPlay(() => {
  1089. console.log('音频开始播放');
  1090. this.isPlaying = true;
  1091. });
  1092. audio.onPause(() => {
  1093. console.log('音频暂停');
  1094. this.isPlaying = false;
  1095. });
  1096. audio.onTimeUpdate(() => {
  1097. this.updateCurrentTime();
  1098. });
  1099. audio.onEnded(() => {
  1100. console.log('当前音频播放结束');
  1101. this.onAudioEnded();
  1102. });
  1103. audio.onError((error) => {
  1104. console.error('音频播放错误:', error);
  1105. this.isPlaying = false;
  1106. uni.showToast({
  1107. title: '音频播放失败',
  1108. icon: 'none'
  1109. });
  1110. });
  1111. this.currentAudio = audio;
  1112. },
  1113. // 更新当前播放时间
  1114. updateCurrentTime() {
  1115. if (!this.currentAudio) return;
  1116. let totalTime = 0;
  1117. // 计算之前音频的总时长
  1118. for (let i = 0; i < this.currentAudioIndex; i++) {
  1119. totalTime += this.currentPageAudios[i].duration;
  1120. }
  1121. // 加上当前音频的播放时间
  1122. totalTime += this.currentAudio.currentTime;
  1123. this.currentTime = totalTime;
  1124. // 如果不是正在拖動滑動條,則同步更新滑動條的值
  1125. if (!this.isDragging) {
  1126. this.sliderValue = this.currentTime;
  1127. }
  1128. // 更新当前高亮的文本索引
  1129. this.updateHighlightIndex();
  1130. },
  1131. // 更新高亮文本索引
  1132. updateHighlightIndex() {
  1133. if (!this.isPlaying || this.currentPageAudios.length === 0) {
  1134. this.currentHighlightIndex = -1;
  1135. return;
  1136. }
  1137. // 根据当前播放的音频索引设置高亮
  1138. this.currentHighlightIndex = this.currentAudioIndex;
  1139. console.log('更新高亮索引:', this.currentHighlightIndex, '当前音频索引:', this.currentAudioIndex);
  1140. },
  1141. // 判断当前文本是否应该高亮
  1142. isTextHighlighted(page, index) {
  1143. // 只有当前页面且是文本类型才可能高亮
  1144. if (page !== this.bookPages[this.currentPage - 1]) return false;
  1145. // 计算当前页面中text类型元素的索引
  1146. let textIndex = 0;
  1147. for (let i = 0; i <= index; i++) {
  1148. if (page[i].type === 'text') {
  1149. if (i === index) {
  1150. const shouldHighlight = textIndex === this.currentHighlightIndex;
  1151. if (shouldHighlight) {
  1152. console.log('高亮文本:', textIndex, '当前高亮索引:', this.currentHighlightIndex);
  1153. }
  1154. return shouldHighlight;
  1155. }
  1156. textIndex++;
  1157. }
  1158. }
  1159. return false;
  1160. },
  1161. // 音频播放结束处理
  1162. onAudioEnded() {
  1163. if (this.currentAudioIndex < this.currentPageAudios.length - 1) {
  1164. // 播放下一个音频
  1165. this.currentAudioIndex++;
  1166. this.playAudio();
  1167. } else {
  1168. // 所有音频播放完毕
  1169. if (this.isLoop) {
  1170. // 循环播放
  1171. this.currentAudioIndex = 0;
  1172. this.playAudio();
  1173. } else {
  1174. // 停止播放
  1175. this.isPlaying = false;
  1176. this.currentTime = this.totalTime;
  1177. this.currentHighlightIndex = -1;
  1178. }
  1179. }
  1180. },
  1181. toggleLoop() {
  1182. this.isLoop = !this.isLoop;
  1183. },
  1184. toggleSpeed() {
  1185. // 檢查是否支持播放速度控制
  1186. if (!this.playbackRateSupported) {
  1187. uni.showToast({
  1188. title: '當前設備不支持播放速度控制',
  1189. icon: 'none',
  1190. duration: 2000
  1191. });
  1192. return;
  1193. }
  1194. const currentIndex = this.speedOptions.indexOf(this.playSpeed);
  1195. const nextIndex = (currentIndex + 1) % this.speedOptions.length;
  1196. const oldSpeed = this.playSpeed;
  1197. this.playSpeed = this.speedOptions[nextIndex];
  1198. console.log(`播放速度切換: ${oldSpeed}x -> ${this.playSpeed}x`);
  1199. // 如果当前有音频在播放,更新播放速度
  1200. if (this.currentAudio) {
  1201. const wasPlaying = this.isPlaying;
  1202. const currentTime = this.currentAudio.currentTime;
  1203. // 設置新的播放速度
  1204. this.currentAudio.playbackRate = this.playSpeed;
  1205. // 如果正在播放,需要重啟播放才能使播放速度生效
  1206. if (wasPlaying) {
  1207. this.currentAudio.pause();
  1208. setTimeout(() => {
  1209. this.currentAudio.seek(currentTime);
  1210. this.currentAudio.play();
  1211. }, 50);
  1212. }
  1213. console.log('音頻實例播放速度已更新為:', this.currentAudio.playbackRate);
  1214. // 顯示速度變更提示
  1215. uni.showToast({
  1216. title: `播放速度: ${this.playSpeed}x`,
  1217. icon: 'none',
  1218. duration: 1000
  1219. });
  1220. }
  1221. },
  1222. // 滑動條值實時更新 (@input 事件)
  1223. onSliderInput(value) {
  1224. // 在拖動過程中實時更新顯示的時間,但不影響實際播放
  1225. if (this.isDragging) {
  1226. // 可以在這裡實時更新顯示時間,讓用戶看到拖動到的時間點
  1227. // 但不改變實際的 currentTime,避免影響播放邏輯
  1228. console.log('實時更新滑動條值:', value);
  1229. }
  1230. },
  1231. // 滑動條拖動過程中的處理 (@changing 事件)
  1232. onSliderChanging(value) {
  1233. // 第一次觸發 changing 事件時,暫停播放並標記為拖動狀態
  1234. if (!this.isDragging) {
  1235. if (this.isPlaying) {
  1236. this.pauseAudio();
  1237. console.log('開始拖動滑動條,暫停播放');
  1238. }
  1239. this.isDragging = true;
  1240. }
  1241. // 更新滑動條的值,但不改變實際播放位置
  1242. this.sliderValue = value;
  1243. console.log('拖動中,滑動條值:', value);
  1244. },
  1245. // 滑動條拖動結束的處理 (@change 事件)
  1246. onSliderChange(value) {
  1247. console.log('滑動條變化,跳轉到位置:', value, '是否為拖動:', this.isDragging);
  1248. // 如果不是拖動狀態(即單點),需要先暫停播放
  1249. if (!this.isDragging && this.isPlaying) {
  1250. this.pauseAudio();
  1251. console.log('單點滑動條,暫停播放');
  1252. }
  1253. // 重置拖動狀態
  1254. this.isDragging = false;
  1255. this.sliderValue = value;
  1256. // 跳轉到指定位置,但不自動恢復播放
  1257. this.seekToTime(value, false);
  1258. console.log('滑動條操作完成,保持暫停狀態,需要手動點擊播放');
  1259. },
  1260. // 跳轉到指定時間
  1261. seekToTime(targetTime, shouldResume = false) {
  1262. if (!this.currentPageAudios || this.currentPageAudios.length === 0) {
  1263. console.log('沒有音頻數據,無法跳轉');
  1264. return;
  1265. }
  1266. // 確保目標時間在有效範圍內
  1267. targetTime = Math.max(0, Math.min(targetTime, this.totalTime));
  1268. console.log('跳轉到時間:', targetTime, '秒', '總時長:', this.totalTime, '是否恢復播放:', shouldResume);
  1269. let accumulatedTime = 0;
  1270. let targetAudioIndex = -1;
  1271. let targetAudioTime = 0;
  1272. // 找到目標時間對應的音頻片段
  1273. for (let i = 0; i < this.currentPageAudios.length; i++) {
  1274. const audioDuration = this.currentPageAudios[i].duration || 0;
  1275. console.log(`音頻片段 ${i}: 時長=${audioDuration}, 累計時間=${accumulatedTime}, 範圍=[${accumulatedTime}, ${accumulatedTime + audioDuration}]`);
  1276. if (targetTime >= accumulatedTime && targetTime <= accumulatedTime + audioDuration) {
  1277. targetAudioIndex = i;
  1278. targetAudioTime = targetTime - accumulatedTime;
  1279. break;
  1280. }
  1281. accumulatedTime += audioDuration;
  1282. }
  1283. // 如果沒有找到合適的音頻片段,使用最後一個
  1284. if (targetAudioIndex === -1 && this.currentPageAudios.length > 0) {
  1285. targetAudioIndex = this.currentPageAudios.length - 1;
  1286. targetAudioTime = this.currentPageAudios[targetAudioIndex].duration || 0;
  1287. console.log('使用最後一個音頻片段作為目標');
  1288. }
  1289. console.log('目標音頻索引:', targetAudioIndex, '目標音頻時間:', targetAudioTime);
  1290. if (targetAudioIndex === -1) {
  1291. console.error('無法找到目標音頻片段');
  1292. return;
  1293. }
  1294. // 如果需要切換到不同的音頻片段
  1295. if (targetAudioIndex !== this.currentAudioIndex) {
  1296. console.log(`切換音頻片段: ${this.currentAudioIndex} -> ${targetAudioIndex}`);
  1297. this.currentAudioIndex = targetAudioIndex;
  1298. this.createAudioInstance();
  1299. // 等待音頻實例創建完成後再跳轉
  1300. this.waitForAudioReady(() => {
  1301. if (this.currentAudio) {
  1302. this.currentAudio.seek(targetAudioTime);
  1303. this.currentTime = targetTime;
  1304. console.log('切換音頻並跳轉到:', targetAudioTime, '秒');
  1305. // 如果拖動前正在播放,則恢復播放
  1306. if (shouldResume) {
  1307. this.currentAudio.play();
  1308. this.isPlaying = true;
  1309. console.log('恢復播放狀態');
  1310. }
  1311. }
  1312. });
  1313. } else {
  1314. // 在當前音頻片段內跳轉
  1315. if (this.currentAudio) {
  1316. this.currentAudio.seek(targetAudioTime);
  1317. this.currentTime = targetTime;
  1318. console.log('在當前音頻內跳轉到:', targetAudioTime, '秒');
  1319. // 如果拖動前正在播放,則恢復播放
  1320. if (shouldResume) {
  1321. this.currentAudio.play();
  1322. this.isPlaying = true;
  1323. console.log('恢復播放狀態');
  1324. }
  1325. }
  1326. }
  1327. },
  1328. // 等待音頻實例準備就緒
  1329. waitForAudioReady(callback, maxAttempts = 10, currentAttempt = 0) {
  1330. if (currentAttempt >= maxAttempts) {
  1331. console.error('音頻實例準備超時');
  1332. return;
  1333. }
  1334. if (this.currentAudio && this.currentAudio.src) {
  1335. // 音頻實例已準備好
  1336. setTimeout(callback, 50); // 稍微延遲確保完全準備好
  1337. } else {
  1338. // 繼續等待
  1339. setTimeout(() => {
  1340. this.waitForAudioReady(callback, maxAttempts, currentAttempt + 1);
  1341. }, 100);
  1342. }
  1343. },
  1344. // 初始檢測播放速度支持(不依賴音頻實例)
  1345. checkInitialPlaybackRateSupport() {
  1346. try {
  1347. const systemInfo = uni.getSystemInfoSync();
  1348. console.log('初始檢測 - 系統信息:', systemInfo);
  1349. // 檢查基礎庫版本 - playbackRate需要2.11.0及以上
  1350. const SDKVersion = systemInfo.SDKVersion || '0.0.0';
  1351. const versionArray = SDKVersion.split('.').map(v => parseInt(v));
  1352. const isVersionSupported = versionArray[0] > 2 ||
  1353. (versionArray[0] === 2 && versionArray[1] > 11) ||
  1354. (versionArray[0] === 2 && versionArray[1] === 11 && versionArray[2] >= 0);
  1355. if (!isVersionSupported) {
  1356. this.playbackRateSupported = false;
  1357. console.log(`初始檢測 - 基礎庫版本過低 (${SDKVersion}),需要2.11.0及以上才支持播放速度控制`);
  1358. return;
  1359. }
  1360. // Android 6以下版本不支持
  1361. if (systemInfo.platform === 'android') {
  1362. const androidVersion = systemInfo.system.match(/Android (\d+)/);
  1363. if (androidVersion && parseInt(androidVersion[1]) < 6) {
  1364. this.playbackRateSupported = false;
  1365. console.log('初始檢測 - Android版本過低,需要Android 6及以上才支持播放速度控制');
  1366. return;
  1367. }
  1368. }
  1369. // 檢查微信原生API是否可用
  1370. if (typeof wx === 'undefined' || !wx.createInnerAudioContext) {
  1371. console.log('初始檢測 - 微信原生API不可用,可能影響playbackRate支持');
  1372. } else {
  1373. console.log('初始檢測 - 微信原生API可用');
  1374. }
  1375. // 如果通過基本檢測,暫時設為支持,等音頻實例創建後再詳細檢測
  1376. this.playbackRateSupported = true;
  1377. console.log('初始檢測 - 基本條件滿足,等待音頻實例檢測');
  1378. } catch (error) {
  1379. console.error('初始檢測播放速度支持時出錯:', error);
  1380. this.playbackRateSupported = false;
  1381. }
  1382. },
  1383. // 檢查播放速度控制支持
  1384. checkPlaybackRateSupport(audio) {
  1385. try {
  1386. // 檢查基礎庫版本和平台支持
  1387. const systemInfo = uni.getSystemInfoSync();
  1388. console.log('系統信息:', systemInfo);
  1389. console.log('平台:', systemInfo.platform);
  1390. console.log('基礎庫版本:', systemInfo.SDKVersion);
  1391. console.log('系統版本:', systemInfo.system);
  1392. // 根據uni-app文檔,微信小程序需要基礎庫2.11.0+才支持playbackRate
  1393. const SDKVersion = systemInfo.SDKVersion || '0.0.0';
  1394. const versionArray = SDKVersion.split('.').map(v => parseInt(v));
  1395. const isVersionSupported = versionArray[0] > 2 ||
  1396. (versionArray[0] === 2 && versionArray[1] > 11) ||
  1397. (versionArray[0] === 2 && versionArray[1] === 11 && versionArray[2] >= 0);
  1398. console.log('基礎庫版本檢查:', {
  1399. version: SDKVersion,
  1400. parsed: versionArray,
  1401. supported: isVersionSupported
  1402. });
  1403. if (!isVersionSupported) {
  1404. this.playbackRateSupported = false;
  1405. console.log(`❌ 基礎庫版本不支持 (${SDKVersion}),微信小程序需要2.11.0+才支持playbackRate`);
  1406. return;
  1407. }
  1408. // Android平台需要6.0+版本支持
  1409. if (systemInfo.platform === 'android') {
  1410. const androidVersion = systemInfo.system.match(/Android (\d+)/);
  1411. console.log('Android版本檢查:', androidVersion);
  1412. if (androidVersion && parseInt(androidVersion[1]) < 6) {
  1413. this.playbackRateSupported = false;
  1414. console.log(`❌ Android版本不支持 (${androidVersion[1]}),需要Android 6+才支持playbackRate`);
  1415. return;
  1416. }
  1417. }
  1418. // 檢查音頻實例是否支持playbackRate
  1419. console.log('🔍 音頻實例檢查:');
  1420. console.log('- playbackRate初始值:', audio.playbackRate);
  1421. console.log('- playbackRate類型:', typeof audio.playbackRate);
  1422. // 嘗試設置並檢測是否真正支持
  1423. const testRate = 1.25;
  1424. const originalRate = audio.playbackRate || 1.0;
  1425. try {
  1426. audio.playbackRate = testRate;
  1427. console.log('- 設置測試速度:', testRate);
  1428. console.log('- 設置後的值:', audio.playbackRate);
  1429. // 檢查設置是否生效
  1430. if (Math.abs(audio.playbackRate - testRate) < 0.01) {
  1431. this.playbackRateSupported = true;
  1432. console.log('✅ playbackRate功能正常');
  1433. } else {
  1434. this.playbackRateSupported = false;
  1435. console.log('❌ playbackRate設置無效,可能不被支持');
  1436. }
  1437. } catch (error) {
  1438. this.playbackRateSupported = false;
  1439. console.log('❌ playbackRate設置出錯:', error);
  1440. }
  1441. } catch (error) {
  1442. console.error('檢查播放速度支持時出錯:', error);
  1443. this.playbackRateSupported = false;
  1444. }
  1445. },
  1446. // 上一个音频
  1447. previousAudio() {
  1448. // 如果正在加载音频,禁止切换
  1449. if (this.isAudioLoading) {
  1450. return;
  1451. }
  1452. if (this.currentPageAudios.length === 0) return;
  1453. if (this.currentAudioIndex > 0) {
  1454. this.currentAudioIndex--;
  1455. if (this.isPlaying) {
  1456. this.playAudio();
  1457. }
  1458. }
  1459. },
  1460. // 下一个音频
  1461. nextAudio() {
  1462. // 如果正在加载音频,禁止切换
  1463. if (this.isAudioLoading) {
  1464. return;
  1465. }
  1466. if (this.currentPageAudios.length === 0) return;
  1467. if (this.currentAudioIndex < this.currentPageAudios.length - 1) {
  1468. this.currentAudioIndex++;
  1469. if (this.isPlaying) {
  1470. this.playAudio();
  1471. }
  1472. }
  1473. },
  1474. async previousPage() {
  1475. if (this.currentPage > 1) {
  1476. this.currentPage--;
  1477. // 获取对应页面的数据(如果还没有获取过)
  1478. if (this.courseIdList[this.currentPage - 1] && this.bookPages[this.currentPage - 1].length === 0) {
  1479. await this.getBookPages(this.courseIdList[this.currentPage - 1]);
  1480. }
  1481. }
  1482. },
  1483. async nextPage() {
  1484. if (this.currentPage < this.bookPages.length) {
  1485. this.currentPage++;
  1486. // 获取对应页面的数据(如果还没有获取过)
  1487. if (this.courseIdList[this.currentPage - 1] && this.bookPages[this.currentPage - 1].length === 0) {
  1488. await this.getBookPages(this.courseIdList[this.currentPage - 1]);
  1489. }
  1490. }
  1491. },
  1492. toggleSound() {
  1493. // 检查是否正在加载音频,如果是则阻止音色切换
  1494. if (this.isAudioLoading) {
  1495. uni.showToast({
  1496. title: '音频加载中,请稍后再试',
  1497. icon: 'none',
  1498. duration: 2000
  1499. });
  1500. return;
  1501. }
  1502. // 检查AudioControls组件是否正在加载音频
  1503. if (this.$refs.customTabbar && this.$refs.customTabbar.$refs.audioControls && this.$refs.customTabbar.$refs.audioControls.isAudioLoading) {
  1504. uni.showToast({
  1505. title: '音频加载中,请稍后再试',
  1506. icon: 'none',
  1507. duration: 2000
  1508. });
  1509. return;
  1510. }
  1511. console.log('音色切换')
  1512. uni.navigateTo({
  1513. url: '/subPages/home/music?voiceId=' + this.voiceId
  1514. })
  1515. },
  1516. unlockBook() {
  1517. console.log('解锁全书')
  1518. // 这里可以跳转到会员页面或者调用解锁接口
  1519. uni.navigateTo({
  1520. url: '/subPages/member/recharge'
  1521. })
  1522. },
  1523. async goToPage(page) {
  1524. this.currentPage = page
  1525. console.log('跳转到页面:', page)
  1526. // 获取对应页面的数据(如果还没有获取过)
  1527. if (this.courseIdList[this.currentPage - 1] && this.bookPages[this.currentPage - 1].length === 0) {
  1528. await this.getBookPages(this.courseIdList[this.currentPage - 1]);
  1529. }
  1530. },
  1531. async onSwiperChange(e) {
  1532. this.currentPage = e.detail.current + 1
  1533. // 获取对应页面的数据(如果还没有获取过)
  1534. if (this.courseIdList[this.currentPage - 1] && this.bookPages[this.currentPage - 1].length === 0) {
  1535. await this.getBookPages(this.courseIdList[this.currentPage - 1]);
  1536. }
  1537. },
  1538. async getCourseList(id) {
  1539. const res = await this.$api.book.coursePage({
  1540. id: id
  1541. })
  1542. if (res.code === 200) {
  1543. // 课程切换时,先清理音频控制组件的所有数据
  1544. if (this.$refs.customTabbar && this.$refs.customTabbar.$refs.audioControls) {
  1545. this.$refs.customTabbar.$refs.audioControls.resetForCourseChange();
  1546. }
  1547. // 清空当前页面相关数据
  1548. this.currentPage = 1; // 重置到第一页
  1549. this.currentCourse = 1; // 重置当前课程索引
  1550. this.currentWordMeaning = null; // 清空单词释义
  1551. this.currentWordAudio = null; // 清空单词音频
  1552. this.currentHighlightIndex = -1; // 清空高亮索引
  1553. // 清理单词音频缓存
  1554. this.clearWordAudioCache();
  1555. // 重新初始化课程数据
  1556. this.courseIdList = res.result.map(item => item.id)
  1557. // 初始化二维数组 换一种方式
  1558. this.bookPages = this.courseIdList.map(() => [])
  1559. // 初始化标题数组
  1560. this.pageTitles = this.courseIdList.map(() => '')
  1561. // 初始化页面类型数组
  1562. this.pageTypes = this.courseIdList.map(() => '')
  1563. // 初始化页面单词数组
  1564. this.pageWords = this.courseIdList.map(() => [])
  1565. console.log('课程切换完成,courseId:', id, '总页数:', this.courseIdList.length);
  1566. // 初始化第一页
  1567. if (this.courseIdList.length > 0) {
  1568. await this.getBookPages(this.courseIdList[0])
  1569. // 课程切换后,确保音频控件能正确加载新课程的音频
  1570. console.log('课程切换完成,准备加载音频,当前页面类型:', this.isTextPage);
  1571. // 使用$nextTick确保DOM和数据都已更新
  1572. this.$nextTick(async () => {
  1573. if (this.$refs.customTabbar && this.$refs.customTabbar.$refs.audioControls) {
  1574. console.log('开始自动加载课程音频');
  1575. try {
  1576. // 直接调用getCurrentPageAudio方法,更可靠
  1577. await this.$refs.customTabbar.$refs.audioControls.getCurrentPageAudio();
  1578. console.log('课程切换后音频加载完成');
  1579. } catch (error) {
  1580. console.error('课程切换后音频加载失败:', error);
  1581. }
  1582. }
  1583. });
  1584. // 预加载后续几页的内容(异步执行,不阻塞当前页面显示)
  1585. this.preloadNextPages()
  1586. }
  1587. }
  1588. },
  1589. async getBookPages(id) {
  1590. const res = await this.$api.book.coursesPageDetail({
  1591. id: id
  1592. })
  1593. if (res.code === 200) {
  1594. // 使用$set确保响应式更新
  1595. const rawPageData = JSON.parse(res.result.content)
  1596. console.log('获取到的原始页面数据:', rawPageData)
  1597. // 过滤掉无效的数据项
  1598. const filteredPageData = rawPageData.filter(item => {
  1599. return item && typeof item === 'object' && (item.type || item.content)
  1600. })
  1601. console.log('过滤后的页面数据:', filteredPageData)
  1602. // 确保当前页面存在
  1603. if (this.currentPage - 1 < this.bookPages.length) {
  1604. this.$set(this.bookPages, this.currentPage - 1, filteredPageData)
  1605. // 保存页面标题
  1606. this.$set(this.pageTitles, this.currentPage - 1, res.result.title || '')
  1607. // 保存页面类型
  1608. this.$set(this.pageTypes, this.currentPage - 1, res.result.type || '')
  1609. // 保存页面单词释义数据
  1610. this.$set(this.pageWords, this.currentPage - 1, res.result.words || [])
  1611. // 保存页面付费状态
  1612. this.$set(this.pagePay, this.currentPage - 1, res.result.pay || 'N')
  1613. }
  1614. }
  1615. },
  1616. // 获取课程列表
  1617. async getCoursePageList (bookId) {
  1618. const res = await this.$api.book.course({
  1619. id: bookId
  1620. })
  1621. if (res.code === 200) {
  1622. this.courseList = res.result.records
  1623. // 打上序列号
  1624. this.courseList = this.courseList.map((item, index) => ({
  1625. ...item,
  1626. index,
  1627. }))
  1628. }
  1629. },
  1630. // 清理音频缓存
  1631. clearAudioCache() {
  1632. this.audioCache = {};
  1633. console.log('音频缓存已清理');
  1634. },
  1635. // 清理單詞語音緩存
  1636. clearWordAudioCache() {
  1637. this.wordAudioCache = {};
  1638. console.log('單詞語音緩存已清理');
  1639. },
  1640. // 限制缓存大小,保留最近访问的页面
  1641. limitCacheSize(maxSize = 10) {
  1642. const cacheKeys = Object.keys(this.audioCache);
  1643. if (cacheKeys.length > maxSize) {
  1644. // 删除最旧的缓存项
  1645. const keysToDelete = cacheKeys.slice(0, cacheKeys.length - maxSize);
  1646. keysToDelete.forEach(key => {
  1647. delete this.audioCache[key];
  1648. });
  1649. console.log('缓存大小已限制,删除了', keysToDelete.length, '个缓存项');
  1650. }
  1651. },
  1652. // 预加载后续页面内容
  1653. async preloadNextPages() {
  1654. try {
  1655. console.log('开始预加载后续页面内容');
  1656. // 优化策略:只预加载接下来的2-3页内容,避免过多请求
  1657. const preloadCount = Math.min(3, this.courseIdList.length - 1); // 预加载3页或剩余页数
  1658. // 串行预加载,避免并发请求过多
  1659. for (let i = 1; i <= preloadCount; i++) {
  1660. if (i < this.courseIdList.length && this.bookPages[i].length === 0) {
  1661. try {
  1662. console.log(`预加载第${i + 1}页内容`);
  1663. await this.preloadSinglePage(this.courseIdList[i], i);
  1664. // 每页之间间隔800ms,给服务器更多缓冲时间
  1665. if (i < preloadCount) {
  1666. await new Promise(resolve => setTimeout(resolve, 800));
  1667. }
  1668. } catch (error) {
  1669. console.error(`预加载第${i + 1}页失败:`, error);
  1670. // 继续预加载下一页
  1671. }
  1672. }
  1673. }
  1674. console.log('页面内容预加载完成');
  1675. // 延迟1.5秒后再通知AudioControls组件开始预加载音频,避免接口冲突
  1676. setTimeout(() => {
  1677. if (this.$refs.customTabbar && this.$refs.customTabbar.$refs.audioControls) {
  1678. this.$refs.customTabbar.$refs.audioControls.startPreloadAudio();
  1679. }
  1680. }, 1500);
  1681. } catch (error) {
  1682. console.error('预加载页面内容失败:', error);
  1683. }
  1684. },
  1685. // 预加载单个页面
  1686. async preloadSinglePage(courseId, pageIndex) {
  1687. try {
  1688. const res = await this.$api.book.coursesPageDetail({
  1689. id: courseId
  1690. });
  1691. if (res.code === 200) {
  1692. const rawPageData = JSON.parse(res.result.content);
  1693. const filteredPageData = rawPageData.filter(item => {
  1694. return item && typeof item === 'object' && (item.type || item.content);
  1695. });
  1696. // 使用$set确保响应式更新
  1697. this.$set(this.bookPages, pageIndex, filteredPageData);
  1698. this.$set(this.pageTitles, pageIndex, res.result.title || '');
  1699. this.$set(this.pageTypes, pageIndex, res.result.type || '');
  1700. this.$set(this.pageWords, pageIndex, res.result.words || []);
  1701. this.$set(this.pagePay, pageIndex, res.result.pay || 'N');
  1702. console.log(`${pageIndex + 1}页内容预加载完成`);
  1703. }
  1704. } catch (error) {
  1705. console.error(`预加载第${pageIndex + 1}页失败:`, error);
  1706. throw error;
  1707. }
  1708. },
  1709. // 自動加載第一頁音頻並播放
  1710. async autoLoadAndPlayFirstPage() {
  1711. try {
  1712. console.log('開始自動加載第一頁音頻');
  1713. // 確保當前是第一頁且需要加載音頻
  1714. if (this.currentPage === 1 && this.shouldLoadAudio) {
  1715. console.log('當前是第一頁且需要音頻,開始加載音頻');
  1716. // 加載音頻
  1717. await this.getCurrentPageAudio();
  1718. // 檢查是否成功加載音頻
  1719. if (this.currentPageAudios && this.currentPageAudios.length > 0) {
  1720. console.log('音頻加載成功,getCurrentPageAudio已經自動播放第一個音頻');
  1721. // getCurrentPageAudio方法已經處理了第一個音頻的播放,這裡不需要再次調用playAudio
  1722. } else {
  1723. console.log('第一頁沒有音頻數據');
  1724. }
  1725. } else {
  1726. console.log('當前頁面不是第一頁文字頁面,跳過自動播放');
  1727. }
  1728. } catch (error) {
  1729. console.error('自動加載和播放音頻失敗:', error);
  1730. }
  1731. },
  1732. },
  1733. async onLoad(args) {
  1734. // 监听音色切换事件,传递给AudioControls组件处理
  1735. uni.$on('selectVoice', async (voiceId) => {
  1736. if (this.voiceId === voiceId) {
  1737. console.log('音色未變化,跳過處理');
  1738. return;
  1739. }
  1740. // 检查是否正在加载音频,如果是则阻止音色切换
  1741. if (this.isAudioLoading || (this.$refs.customTabbar && this.$refs.customTabbar.$refs.audioControls && this.$refs.customTabbar.$refs.audioControls.isAudioLoading)) {
  1742. console.log('音频正在加载中,阻止音色切换');
  1743. uni.showToast({
  1744. title: '音频加载中,请稍后再试',
  1745. icon: 'none',
  1746. duration: 2000
  1747. });
  1748. return;
  1749. }
  1750. // 更新本地音色ID
  1751. this.voiceId = voiceId;
  1752. // 清理單詞語音資源
  1753. this.clearWordAudioCache();
  1754. if (this.currentWordAudio) {
  1755. this.currentWordAudio.destroy();
  1756. this.currentWordAudio = null;
  1757. }
  1758. // 通知AudioControls组件处理音色切换
  1759. if (this.$refs.customTabbar && this.$refs.customTabbar.$refs.audioControls) {
  1760. try {
  1761. console.log('开始处理音色切换,清理所有音频缓存并重新获取所有页面音频');
  1762. // 传入选项:preloadAllPages: true 表示要预加载所有页面的音频
  1763. await this.$refs.customTabbar.$refs.audioControls.handleVoiceChange(voiceId, {
  1764. preloadAllPages: true
  1765. });
  1766. console.log('音色切换处理完成');
  1767. } catch (error) {
  1768. console.error('音色切换处理失败:', error);
  1769. }
  1770. }
  1771. })
  1772. this.courseId = args.courseId
  1773. this.memberId = args.memberId
  1774. // 先获取点进来的课程的页面列表
  1775. await Promise.all([this.getVoiceList(),this.getMemberInfo(),this.getCourseList(this.courseId), this.getCoursePageList(args.bookId)])
  1776. // 页面加载完成后,通知AudioControls组件自动加载第一页音频
  1777. this.$nextTick(() => {
  1778. if (this.$refs.customTabbar && this.$refs.customTabbar.$refs.audioControls) {
  1779. this.$refs.customTabbar.$refs.audioControls.autoLoadAndPlayFirstPage();
  1780. }
  1781. });
  1782. },
  1783. // 页面卸载时清理资源
  1784. onUnload() {
  1785. console.log('📱 页面卸载:开始清理所有音频资源');
  1786. uni.$off('selectVoice')
  1787. // 1. 清理单词语音资源
  1788. if (this.currentWordAudio) {
  1789. console.log('🧹 清理单词音频实例');
  1790. try {
  1791. this.currentWordAudio.destroy();
  1792. } catch (error) {
  1793. console.error('销毁单词音频实例失败:', error);
  1794. }
  1795. this.currentWordAudio = null;
  1796. }
  1797. // 2. 清理单词语音缓存
  1798. this.clearWordAudioCache();
  1799. // 3. 停止单词音频播放状态
  1800. this.isWordAudioPlaying = false;
  1801. // 4. 通知AudioControls组件清理资源
  1802. if (this.$refs.customTabbar && this.$refs.customTabbar.$refs.audioControls) {
  1803. console.log('🧹 通知AudioControls清理音频资源');
  1804. this.$refs.customTabbar.$refs.audioControls.destroyAudio();
  1805. }
  1806. // 5. 清理全局音频实例(防止遗漏)
  1807. try {
  1808. // 获取所有可能的音频上下文并销毁
  1809. if (typeof wx !== 'undefined' && wx.getBackgroundAudioManager) {
  1810. const bgAudio = wx.getBackgroundAudioManager();
  1811. if (bgAudio) {
  1812. bgAudio.stop();
  1813. }
  1814. }
  1815. } catch (error) {
  1816. console.error('清理背景音频失败:', error);
  1817. }
  1818. console.log('✅ 页面卸载:音频资源清理完成');
  1819. },
  1820. // 页面隐藏时暂停音频
  1821. onHide() {
  1822. console.log('📱 页面隐藏:暂停所有音频播放');
  1823. // 1. 暂停单词音频
  1824. if (this.currentWordAudio && this.isWordAudioPlaying) {
  1825. console.log('⏸️ 暂停单词音频播放');
  1826. try {
  1827. this.currentWordAudio.pause();
  1828. this.isWordAudioPlaying = false;
  1829. } catch (error) {
  1830. console.error('暂停单词音频失败:', error);
  1831. }
  1832. }
  1833. // 2. 通知AudioControls组件暂停音频
  1834. if (this.$refs.customTabbar && this.$refs.customTabbar.$refs.audioControls) {
  1835. console.log('⏸️ 通知AudioControls暂停音频');
  1836. this.$refs.customTabbar.$refs.audioControls.pauseOnHide();
  1837. }
  1838. console.log('✅ 页面隐藏:音频暂停完成');
  1839. }
  1840. }
  1841. </script>
  1842. <style lang="scss" scoped>
  1843. .book-container {
  1844. width: 100%;
  1845. min-height: 100vh;
  1846. background-color: #F8F8F8;
  1847. position: relative;
  1848. overflow: hidden;
  1849. }
  1850. .custom-navbar {
  1851. position: fixed;
  1852. top: 0;
  1853. left: 0;
  1854. right: 0;
  1855. background-color: #F8F8F8;
  1856. z-index: 1000;
  1857. transition: transform 0.3s ease;
  1858. &.navbar-hidden {
  1859. transform: translateY(-100%);
  1860. }
  1861. }
  1862. .navbar-content {
  1863. display: flex;
  1864. align-items: center;
  1865. justify-content: space-between;
  1866. padding: 20rpx 32rpx;
  1867. // padding-top: calc(20rpx + var(--status-bar-height, 0));
  1868. height: 60rpx;
  1869. }
  1870. .navbar-left,
  1871. .navbar-right {
  1872. width: 80rpx;
  1873. display: flex;
  1874. align-items: center;
  1875. }
  1876. .navbar-right {
  1877. justify-content: flex-end;
  1878. flex: 1;
  1879. }
  1880. .navbar-title {
  1881. transform: translateX(-50rpx);
  1882. flex: 1;
  1883. text-align: center;
  1884. font-family: PingFang SC;
  1885. font-weight: 500;
  1886. font-size: 32rpx;
  1887. color: #262626;
  1888. line-height: 48rpx;
  1889. }
  1890. .content-swiper {
  1891. flex: 1;
  1892. // min-height: calc(100vh - 100rpx);
  1893. // margin-top: 100rpx;
  1894. height: 100vh;
  1895. }
  1896. .swiper-item {
  1897. min-height: 100vh;
  1898. // background-color: red;
  1899. }
  1900. .content-area {
  1901. flex: 1;
  1902. padding: 30rpx 40rpx 100rpx;
  1903. /* #ifndef H5 */
  1904. padding: 100rpx 40rpx;
  1905. /* #endif */
  1906. // padding-top: ;
  1907. // background: linear-gradient(180deg, #DEFFFF 0%, #FBFEFF 22.65%, #F0FBFF 100%);
  1908. min-height: 100%;
  1909. box-sizing: border-box;
  1910. overflow-y: auto;
  1911. .title{
  1912. font-family: PingFang SC;
  1913. font-weight: 500;
  1914. font-size: 34rpx;
  1915. text-align: center;
  1916. color: #181818;
  1917. line-height: 48rpx;
  1918. margin-bottom: 32rpx;
  1919. }
  1920. .content-image{
  1921. width: 100%;
  1922. height: auto;
  1923. margin: 30rpx auto;
  1924. }
  1925. .video-content{
  1926. width: 100%;
  1927. height: auto;
  1928. margin: 30rpx auto;
  1929. position: relative;
  1930. .video-player{
  1931. // height: 100%;
  1932. width: 100%;
  1933. // margin: 0 auto;
  1934. // height: auto;
  1935. }
  1936. .video-loading {
  1937. position: absolute;
  1938. top: 50%;
  1939. left: 50%;
  1940. transform: translate(-50%, -50%);
  1941. color: #666;
  1942. font-size: 28rpx;
  1943. }
  1944. }
  1945. }
  1946. .card-content {
  1947. background: linear-gradient(180deg, #DEFFFF 0%, #FBFEFF 22.65%, #F0FBFF 100%);
  1948. display: flex;
  1949. flex-direction: column;
  1950. gap: 32rpx;
  1951. min-height: 1172rpx;
  1952. margin-top: 20rpx;
  1953. border-radius: 32rpx;
  1954. // height: 100%;
  1955. padding: 40rpx;
  1956. padding-bottom: 100rpx;
  1957. // margin: 0
  1958. border: 1px solid #FFFFFF;
  1959. box-sizing: border-box;
  1960. .card-line {
  1961. display: flex;
  1962. align-items: center;
  1963. // margin-bottom: 20rpx;
  1964. }
  1965. .card-line-image {
  1966. width: 48rpx;
  1967. height: 48rpx;
  1968. margin-right: 16rpx;
  1969. }
  1970. .card-line-text {
  1971. font-family: PingFang SC;
  1972. font-weight: 600;
  1973. font-size: 30rpx;
  1974. line-height: 48rpx;
  1975. color: #3B3D3D;
  1976. }
  1977. .card-image {
  1978. width: 590rpx;
  1979. height: 268rpx;
  1980. border-radius: 24rpx;
  1981. margin: 30rpx auto;
  1982. // margin-bottom: 20rpx;
  1983. }
  1984. .english-text {
  1985. display: block;
  1986. font-family: PingFang SC;
  1987. font-weight: 600;
  1988. font-size: 32rpx;
  1989. line-height: 48rpx;
  1990. color: #3B3D3D;
  1991. // margin-bottom: 16rpx;
  1992. }
  1993. .english-text-container {
  1994. display: flex;
  1995. flex-wrap: wrap;
  1996. align-items: baseline;
  1997. }
  1998. .english-token {
  1999. font-family: PingFang SC;
  2000. font-weight: 600;
  2001. font-size: 32rpx;
  2002. line-height: 48rpx;
  2003. color: #3B3D3D;
  2004. margin-right: 10rpx;
  2005. }
  2006. .clickable-word {
  2007. background: $primary-color;
  2008. text-decoration: underline;
  2009. cursor: pointer;
  2010. transition: all 0.2s ease;
  2011. }
  2012. .clickable-word:hover {
  2013. background-color: rgba(0, 122, 255, 0.1);
  2014. border-radius: 4rpx;
  2015. }
  2016. .chinese-segment {
  2017. font-family: PingFang SC;
  2018. font-weight: 400;
  2019. font-size: 28rpx;
  2020. line-height: 48rpx;
  2021. color: #3B3D3D;
  2022. }
  2023. .clickable-keyword {
  2024. background: $primary-color;
  2025. text-decoration: underline;
  2026. cursor: pointer;
  2027. transition: all 0.2s ease;
  2028. border-radius: 4rpx;
  2029. padding: 2rpx 4rpx;
  2030. }
  2031. .clickable-keyword:hover {
  2032. background-color: rgba(0, 122, 255, 0.1);
  2033. }
  2034. .chinese-text {
  2035. display: block;
  2036. font-family: PingFang SC;
  2037. font-weight: 400;
  2038. font-size: 28rpx;
  2039. line-height: 48rpx;
  2040. color: #4F4F4F;
  2041. }
  2042. }
  2043. /* 会员限制页面样式 */
  2044. .member-content {
  2045. display: flex;
  2046. flex-direction: column;
  2047. align-items: center;
  2048. justify-content: center;
  2049. height: 90%;
  2050. background-color: #F8F8F8;
  2051. padding: 40rpx;
  2052. margin: -40rpx;
  2053. box-sizing: border-box;
  2054. }
  2055. .member-title {
  2056. font-family: PingFang SC;
  2057. font-weight: 500;
  2058. font-size: 40rpx;
  2059. line-height: 1;
  2060. color: #6f6f6f;
  2061. text-align: center;
  2062. margin-bottom: 48rpx;
  2063. }
  2064. .member-button {
  2065. width: 670rpx;
  2066. height: 72rpx;
  2067. background: #06DADC;
  2068. border-radius: 200rpx;
  2069. display: flex;
  2070. align-items: center;
  2071. justify-content: center;
  2072. }
  2073. .member-button-text {
  2074. font-family: PingFang SC;
  2075. font-weight: 400;
  2076. font-size: 30rpx;
  2077. color: #FFFFFF;
  2078. }
  2079. // .video-content {
  2080. // width: 100%;
  2081. // height: auto;
  2082. // // margin: 200rpx -40rpx 0;
  2083. // // height: 500rpx;
  2084. // background-color: #FFFFFF;
  2085. // // padding: 40rpx;
  2086. // border-radius: 24rpx;
  2087. // display: flex;
  2088. // align-items: center;
  2089. // justify-content: center;
  2090. // .video-player{
  2091. // width: 100%;
  2092. // margin: 0 auto;
  2093. // height: auto;
  2094. // }
  2095. // }
  2096. .text-content {
  2097. width: 100vw;
  2098. background-color: #F6F6F6;
  2099. height: 100%;
  2100. padding: 40rpx;
  2101. margin: -40rpx;
  2102. box-sizing: border-box;
  2103. }
  2104. .content-text {
  2105. font-family: PingFang SC;
  2106. // font-weight: 400;
  2107. font-size: 28rpx;
  2108. color: #3B3D3D;
  2109. line-height: 48rpx;
  2110. letter-spacing: 0;
  2111. text-align: justify;
  2112. word-break: break-all;
  2113. transition: all 0.3s ease;
  2114. }
  2115. .clickable-text {
  2116. cursor: pointer;
  2117. &:active {
  2118. background-color: rgba(6, 218, 220, 0.1);
  2119. border-radius: 4rpx;
  2120. }
  2121. }
  2122. .lead-text {
  2123. background: #fffbe6; /* 柔和的提示背景 */
  2124. border: 1px solid #ffe58f;
  2125. border-radius: 8px;
  2126. padding: 10rpx 20rpx;
  2127. }
  2128. .text-highlight {
  2129. background-color: rgba(255, 248, 220, 0.8); /* 温暖的米黄色,对眼睛友好 */
  2130. border-left: 4rpx solid #ffd700; /* 左侧金色边框作为朗读指示 */
  2131. padding: 4rpx 8rpx;
  2132. border-radius: 6rpx;
  2133. transition: all 0.3s ease;
  2134. box-shadow: 0 2rpx 6rpx rgba(255, 215, 0, 0.15); /* 柔和的阴影 */
  2135. }
  2136. </style>