audioOne.vue 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. <template>
  2. <!-- 待播放 -->
  3. <view class="audio-play-btn" v-if="!data.isPlaying" @click="handlePlay"></view>
  4. <!-- 播放中 -->
  5. <view class="audio-playing-btn" v-else></view>
  6. </template>
  7. <script setup>
  8. import {
  9. reactive,
  10. computed,
  11. onUnmounted
  12. } from 'vue';
  13. import {
  14. onLoad
  15. } from "@dcloudio/uni-app"
  16. const props = defineProps({
  17. activeWord: {
  18. type: Object,
  19. },
  20. })
  21. const emits = defineEmits(['play-audio'])
  22. const data = reactive({
  23. isPlaying: false,
  24. code: null
  25. })
  26. onLoad(() => {
  27. uni.$on('danci-audio-ended', (code) => {
  28. data.isPlaying = false;
  29. })
  30. uni.$on('danci-audio-play', (code) => {
  31. if (data.code == code) {
  32. data.isPlaying = true;
  33. } else {
  34. data.isPlaying = false;
  35. }
  36. })
  37. })
  38. onUnmounted(() => {
  39. uni.$off('danci-audio-ended')
  40. uni.$off('danci-audio-play')
  41. })
  42. function handlePlay() {
  43. console.log('播放')
  44. data.code = new Date().getTime()
  45. emits('play-audio', {
  46. url: props.activeWord.yinpin,
  47. code: data.code
  48. })
  49. }
  50. </script>
  51. <style>
  52. </style>