useAudio.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. watch(grouthType, () => {
  70. if (defaultGrouthType.value != grouthType.value) {
  71. const list = audioList[grouthType.value];
  72. // 当鹅成长状态变更时重置播放序列
  73. audioIndex.value = 0;
  74. defaultGrouthType.value = grouthType.value;
  75. changeSource(audioList[grouthType.value][0]);
  76. }
  77. })
  78. return {
  79. innerAudioContext,
  80. }
  81. }
  82. export function useHuDong(props) {
  83. const imgUrl = ref('');
  84. function getDefaultImg() {
  85. const active = gooseGrowthTypeList.find(item => item.id == props.growthType);
  86. return active ? active.imgUrl: ''
  87. }
  88. // 初始化
  89. function init() {
  90. imgUrl.value = getDefaultImg();
  91. }
  92. function doTouch(myAudio) {
  93. myAudio.play();
  94. }
  95. uni.$on('play-audio', () => {
  96. imgUrl.value = imageList[props.growthType][1]
  97. })
  98. uni.$on('play-audio-ended', () => {
  99. imgUrl.value = imageList[props.growthType][0]
  100. })
  101. return {
  102. init,
  103. doTouch,
  104. imgUrl
  105. }
  106. }