2222.vue 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <template>
  2. <view class="container">
  3. <!-- 主音频 -->
  4. <view v-if="audioInfo?.main" class="audio-section">
  5. <text class="section-title">单词发音</text>
  6. <view class="audio-item" @click="playMainAudio">
  7. <text>{{ audioInfo.main.text }}</text>
  8. <text>{{ wordData.yinbiao }}</text>
  9. </view>
  10. </view>
  11. <!-- 音节音频 -->
  12. <view v-if="audioInfo?.syllables?.length" class="audio-section">
  13. <text class="section-title">音节分解</text>
  14. <view
  15. v-for="syllable in audioInfo.syllables"
  16. :key="syllable.id"
  17. class="audio-item"
  18. @click="playSyllable(syllable)">
  19. <text>{{ syllable.text }}</text>
  20. <text>{{ syllable.index + 1 }}/{{ audioInfo.syllables.length }}</text>
  21. </view>
  22. </view>
  23. <!-- 频度音频 -->
  24. <view v-if="audioInfo?.frequencies?.length" class="audio-section">
  25. <text class="section-title">频度发音</text>
  26. <view
  27. v-for="frequency in audioInfo.frequencies"
  28. :key="frequency.id"
  29. class="audio-item"
  30. @click="playFrequency(frequency)">
  31. <text>{{ frequency.text }}</text>
  32. </view>
  33. </view>
  34. </view>
  35. </template>
  36. <script setup>
  37. import { ref, onMounted } from 'vue'
  38. import audioManager from '@/utils/audioManager'
  39. const props = defineProps({
  40. wordData: {
  41. type: Object,
  42. required: true
  43. }
  44. })
  45. const audioInfo = ref(null)
  46. // 初始化并缓存音频
  47. const initAudio = async () => {
  48. // 1. 从缓存获取已有信息
  49. const cachedInfo = audioManager.getAudioInfoFromStorage(props.wordData.id)
  50. if (cachedInfo) {
  51. audioInfo.value = cachedInfo
  52. }
  53. // 2. 预加载所有音频(无论是否已有缓存)
  54. await audioManager.cacheWordAudios(props.wordData)
  55. // 3. 更新音频信息
  56. audioInfo.value = audioManager.getAudioInfoFromStorage(props.wordData.id)
  57. }
  58. // 播放主音频
  59. const playMainAudio = () => {
  60. if (audioInfo.value && audioInfo.value.main) {
  61. audioManager.playAudio(audioInfo.value.main)
  62. }
  63. }
  64. // 播放音节音频
  65. const playSyllable = (syllable) => {
  66. audioManager.playAudio(syllable)
  67. }
  68. // 播放频度音频
  69. const playFrequency = (frequency) => {
  70. audioManager.playAudio(frequency)
  71. }
  72. onMounted(() => {
  73. initAudio()
  74. })
  75. </script>
  76. <style>
  77. .container {
  78. padding: 20rpx;
  79. }
  80. .audio-section {
  81. margin-bottom: 30rpx;
  82. }
  83. .section-title {
  84. display: block;
  85. font-weight: bold;
  86. margin-bottom: 15rpx;
  87. color: #333;
  88. font-size: 32rpx;
  89. }
  90. .audio-item {
  91. padding: 20rpx;
  92. margin: 10rpx 0;
  93. background: #f5f5f5;
  94. border-radius: 8rpx;
  95. display: flex;
  96. justify-content: space-between;
  97. align-items: center;
  98. }
  99. </style>