danxuanCeshi.vue 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <template>
  2. <view v-if="question" class="ezy-yingyu-danxuan-box">
  3. <!-- 标题区域 -->
  4. <view class="yingyu-danxuan-title"></view>
  5. <view v-html="data.name">
  6. </view>
  7. <!-- 选项区域 -->
  8. <view v-for="(item,index) in data.contents" class="yingyu-danxuan-option-box" :key="index">
  9. <text class="option-change">{{item.number}}</text>
  10. <text class="option-question">{{item.neirong}}</text>
  11. <i class="yingyu-canplay-img" @click="audioCreat(item,index)"></i>
  12. </view>
  13. </view>
  14. </template>
  15. <script setup>
  16. import {
  17. ref,
  18. reactive,
  19. watch
  20. } from 'vue';
  21. import {
  22. useQuestionTools
  23. } from "../useQuestionTools"
  24. import textReplaceIconVue from './textReplaceIcon.vue'
  25. const {
  26. getLetterByIndex
  27. } = useQuestionTools();
  28. const props = defineProps({
  29. question: {
  30. type: Object,
  31. },
  32. showError: {
  33. type: Boolean,
  34. default: false
  35. },
  36. placeholders: { // 占位符
  37. type: Array,
  38. required: true
  39. },
  40. code: {
  41. type: String,
  42. }
  43. })
  44. const data = reactive({
  45. name: '', //题干数据
  46. contents: [], // 选项数据
  47. })
  48. watch(() => props.question, (val) => formatData(val), {
  49. immediate: true
  50. })
  51. function formatData(val) {
  52. if (val) {
  53. data.name = val.name;
  54. data.contents = val.optList.map((item, index) => {
  55. return {
  56. neirong: item.neirong,
  57. audio: item.audio,
  58. number: getLetterByIndex(index)
  59. }
  60. })
  61. }
  62. }
  63. let audioContext = null;
  64. let isPlaying = false;
  65. let currentIndex = -1;
  66. function initAudioContext() {
  67. if (!audioContext) {
  68. audioContext = uni.createInnerAudioContext();
  69. // 监听音频播放状态
  70. audioContext.onPlay(() => {
  71. isPlaying = true;
  72. console.log('音频开始播放');
  73. });
  74. audioContext.onPause(() => {
  75. isPlaying = false;
  76. console.log('音频暂停播放');
  77. });
  78. audioContext.onStop(() => {
  79. isPlaying = false;
  80. console.log('音频停止播放');
  81. });
  82. audioContext.onEnded(() => {
  83. isPlaying = false;
  84. console.log('音频播放结束');
  85. });
  86. }
  87. }
  88. function playAudio(src) {
  89. if (!audioContext) {
  90. initAudioContext();
  91. }
  92. audioContext.src = src;
  93. audioContext.play();
  94. }
  95. function audioCreat(item, index) {
  96. // 如果 index 变化了,或者当前没有音频在播放,就播放新音频
  97. if (index !== currentIndex || !isPlaying) {
  98. // 如果之前有音频在播放,先暂停
  99. if (isPlaying) {
  100. audioContext.pause();
  101. }
  102. // 更新当前索引和播放状态
  103. currentIndex = index;
  104. playAudio(item.audio);
  105. } else {
  106. // 如果 index 没变化且音频正在播放,就暂停音频
  107. if (isPlaying) {
  108. audioContext.pause();
  109. } else {
  110. // 如果 index 没变化但音频没播放,就播放音频
  111. playAudio(item.audio);
  112. }
  113. }
  114. }
  115. </script>