useAudio.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. import {ref, watch,computed} from "vue";
  2. import {gooseGrowthTypeList} from "./constantConfig.js";
  3. import {onHide} from "@dcloudio/uni-app"
  4. const audioList = {
  5. 0: [
  6. '/static/mp3/dan/01.MP3',
  7. '/static/mp3/dan/02.MP3',
  8. '/static/mp3/dan/03.MP3',
  9. ],
  10. 1: [
  11. '/static/mp3/xiaoe/01.MP3',
  12. '/static/mp3/xiaoe/02.MP3',
  13. '/static/mp3/xiaoe/03.MP3',
  14. '/static/mp3/xiaoe/04.MP3',
  15. ],
  16. 2: [
  17. '/static/mp3/zhonge/01.MP3',
  18. '/static/mp3/zhonge/02.MP3',
  19. '/static/mp3/zhonge/03.MP3',
  20. '/static/mp3/zhonge/04.MP3',
  21. ],
  22. 3: [
  23. '/static/mp3/dae/01.MP3',
  24. '/static/mp3/dae/02.MP3',
  25. '/static/mp3/dae/03.MP3',
  26. '/static/mp3/dae/04.MP3',
  27. ],
  28. }
  29. const imageList = {
  30. 0: ['/static/images/game/game-img1.gif', '/static/images/game/game-eat-img1.gif'],
  31. 1: ['/static/images/game/game-img2.gif', '/static/images/game/game-eat-img2.gif'],
  32. 2: ['/static/images/game/game-img3.gif', '/static/images/game/game-eat-img3.gif'],
  33. 3: ['/static/images/game/game-img4.gif', '/static/images/game/game-eat-img4.gif'],
  34. }
  35. export function useAudio (grouthType) {
  36. const defaultGrouthType = ref(''); // 成长状态历史
  37. const audioIndex = ref(0); // 播放序列
  38. const innerAudioContext = ref(null);
  39. function createAudio() {
  40. const innerAudioContext = uni.createInnerAudioContext();
  41. innerAudioContext.autoplay = false;
  42. innerAudioContext.src = audioList[grouthType.value][audioIndex.value];
  43. innerAudioContext.onPlay(() => {
  44. // 播放
  45. uni.$emit('play-audio');
  46. });
  47. innerAudioContext.onError((res) => {
  48. audioIndex.value = 0;
  49. uni.$emit('play-error')
  50. console.log(res.errMsg);
  51. console.log(res.errCode);
  52. });
  53. innerAudioContext.onEnded(() => {
  54. // 播放结束
  55. uni.$emit('play-audio-ended')
  56. // 计算播放音频序列
  57. const list = audioList[grouthType.value];
  58. if (audioIndex.value >= list.length-1) {
  59. audioIndex.value = 0;
  60. } else {
  61. audioIndex.value = audioIndex.value+1;
  62. }
  63. // 更新音频源
  64. changeSource(list[audioIndex.value]);
  65. })
  66. return innerAudioContext;
  67. }
  68. function changeSource(url) {
  69. innerAudioContext.src= url;
  70. innerAudioContext.currentTime = 0;
  71. }
  72. onHide(() => {
  73. innerAudioContext.value.stop();
  74. })
  75. // 展开食物停止播放
  76. uni.$on('food-select-open', () => {
  77. innerAudioContext.value.stop();
  78. audioIndex.value = 0;
  79. })
  80. innerAudioContext.value = createAudio();
  81. function handlePlay() {
  82. innerAudioContext.value.destroy();
  83. innerAudioContext.value = null;
  84. innerAudioContext.value = createAudio();
  85. innerAudioContext.value.play()
  86. }
  87. watch(grouthType, () => {
  88. if (defaultGrouthType.value != grouthType.value) {
  89. const list = audioList[grouthType.value];
  90. // 当鹅成长状态变更时重置播放序列
  91. audioIndex.value = 0;
  92. defaultGrouthType.value = grouthType.value;
  93. changeSource(audioList[grouthType.value][0]);
  94. }
  95. })
  96. return {
  97. innerAudioContext,
  98. handlePlay
  99. }
  100. }
  101. export function useHuDong(props) {
  102. const imgUrl = ref('');
  103. function getDefaultImg() {
  104. const active = gooseGrowthTypeList.find(item => item.id == props.growthType);
  105. return active ? active.imgUrl: ''
  106. }
  107. // 初始化
  108. function init() {
  109. imgUrl.value = getDefaultImg();
  110. }
  111. function doTouch(handlePlay) {
  112. handlePlay();
  113. }
  114. // 播放开始切换动画
  115. uni.$on('play-audio', () => {
  116. imgUrl.value = imageList[props.growthType][1]
  117. })
  118. // 播放结束恢复默认
  119. uni.$on('play-audio-ended', () => {
  120. imgUrl.value = imageList[props.growthType][0]
  121. })
  122. // 展开食物恢复默认
  123. uni.$on('food-select-open', () => {
  124. imgUrl.value = imageList[props.growthType][0]
  125. })
  126. uni.$on('play-error',() => {
  127. imgUrl.value = imageList[props.growthType][0]
  128. })
  129. return {
  130. init,
  131. doTouch,
  132. imgUrl
  133. }
  134. }