mtaRadio.vue 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <template>
  2. <view></view>
  3. </template>
  4. <script setup>
  5. import {
  6. useAudio
  7. } from './useAudio';
  8. import { onUnmounted } from "vue"
  9. let historyData = null;
  10. const {
  11. handlePlay,
  12. handleStop,
  13. innerAudioContext
  14. } = useAudio();
  15. let num = 0;
  16. let alreadyplay = false; // 是否是首次播放
  17. function doPlay(data) {
  18. // console.log('dddd')
  19. if (historyData) {
  20. if (historyData.stId == data.stId && historyData.index == data.index) {
  21. // 播放中切换其他音频时发出事件 同题切换音频
  22. historyData = null
  23. // console.log('切换同频 暂停或播放')
  24. uni.$emit('reset-playing-status', historyData);
  25. return;
  26. }
  27. if (historyData.stId == data.stId && historyData.index != data.index) {
  28. // 播放中切换其他音频时发出事件 同题切换音频
  29. uni.$emit('reset-playing-status', historyData);
  30. // console.log('切换同题 不同音频')
  31. handlePlay(data)
  32. historyData = data;
  33. return;
  34. }
  35. if (historyData.stId != data.stId) {
  36. // 播放中切换其他音频时发出事件 切换其他试题
  37. uni.$emit('reset-playing-status', historyData);
  38. historyData = null
  39. // console.log('切换不同题')
  40. return;
  41. }
  42. }
  43. if (num == 1) {
  44. // console.log('执行播放 已拦截')
  45. num = 0;
  46. alreadyplay = true
  47. return;
  48. }
  49. // console.log('执行播放')
  50. handlePlay(data)
  51. historyData = data;
  52. num++;
  53. if (!alreadyplay) {
  54. num = 0;
  55. alreadyplay = true;
  56. }
  57. }
  58. function doPause() {
  59. handleStop()
  60. }
  61. function doStop(){
  62. handleStop()
  63. }
  64. uni.$emit('swiper-change', () => {
  65. num = 0;
  66. })
  67. // 播放
  68. uni.$on('do-yy-audio-play', doPlay)
  69. // 暂停
  70. uni.$on('do-yy-audio-stop', doPause)
  71. // 销毁
  72. uni.$on('destory-stop', doStop)
  73. onUnmounted(() => {
  74. // 播放
  75. uni.$off('do-yy-audio-play', doPlay)
  76. // 暂停
  77. uni.$off('do-yy-audio-stop', doPause)
  78. // 销毁
  79. uni.$off('destory-stop', doStop)
  80. })
  81. </script>
  82. <style>
  83. </style>