useAudio.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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 = uni.createInnerAudioContext();
  39. innerAudioContext.autoplay = false;
  40. innerAudioContext.src = audioList[grouthType.value][0];
  41. innerAudioContext.onPlay(() => {
  42. // 播放
  43. uni.$emit('play-audio');
  44. });
  45. innerAudioContext.onError((res) => {
  46. console.log(res.errMsg);
  47. console.log(res.errCode);
  48. });
  49. innerAudioContext.onEnded(() => {
  50. // 播放结束
  51. uni.$emit('play-audio-ended')
  52. // 计算播放音频序列
  53. const list = audioList[grouthType.value];
  54. if (audioIndex.value >= list.length-1) {
  55. audioIndex.value = 0;
  56. } else {
  57. audioIndex.value = audioIndex.value+1;
  58. }
  59. // 更新音频源
  60. changeSource(list[audioIndex.value]);
  61. })
  62. function changeSource(url) {
  63. innerAudioContext.src= url;
  64. innerAudioContext.currentTime = 0;
  65. }
  66. onHide(() => {
  67. innerAudioContext.stop();
  68. })
  69. // 展开食物停止播放
  70. uni.$on('food-select-open', () => {
  71. innerAudioContext.stop();
  72. audioIndex.value = 0;
  73. })
  74. watch(grouthType, () => {
  75. if (defaultGrouthType.value != grouthType.value) {
  76. const list = audioList[grouthType.value];
  77. // 当鹅成长状态变更时重置播放序列
  78. audioIndex.value = 0;
  79. defaultGrouthType.value = grouthType.value;
  80. changeSource(audioList[grouthType.value][0]);
  81. }
  82. })
  83. return {
  84. innerAudioContext,
  85. }
  86. }
  87. export function useHuDong(props) {
  88. const imgUrl = ref('');
  89. function getDefaultImg() {
  90. const active = gooseGrowthTypeList.find(item => item.id == props.growthType);
  91. return active ? active.imgUrl: ''
  92. }
  93. // 初始化
  94. function init() {
  95. imgUrl.value = getDefaultImg();
  96. }
  97. function doTouch(myAudio) {
  98. myAudio.play();
  99. }
  100. // 播放开始切换动画
  101. uni.$on('play-audio', () => {
  102. imgUrl.value = imageList[props.growthType][1]
  103. })
  104. // 播放结束恢复默认
  105. uni.$on('play-audio-ended', () => {
  106. imgUrl.value = imageList[props.growthType][0]
  107. })
  108. // 展开食物恢复默认
  109. uni.$on('food-select-open', () => {
  110. imgUrl.value = imageList[props.growthType][0]
  111. })
  112. return {
  113. init,
  114. doTouch,
  115. imgUrl
  116. }
  117. }