123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156 |
- import {ref, watch,computed} from "vue";
- import {gooseGrowthTypeList} from "./constantConfig.js";
- import {onHide,onUnload} from "@dcloudio/uni-app"
- const audioList = {
- 0: [
- '/static/mp3/dan/01.MP3',
- '/static/mp3/dan/02.MP3',
- '/static/mp3/dan/03.MP3',
- ],
- 1: [
- '/static/mp3/xiaoe/01.MP3',
- '/static/mp3/xiaoe/02.MP3',
- '/static/mp3/xiaoe/03.MP3',
- '/static/mp3/xiaoe/04.MP3',
- ],
- 2: [
- '/static/mp3/zhonge/01.MP3',
- '/static/mp3/zhonge/02.MP3',
- '/static/mp3/zhonge/03.MP3',
- '/static/mp3/zhonge/04.MP3',
- ],
- 3: [
- '/static/mp3/dae/01.MP3',
- '/static/mp3/dae/02.MP3',
- '/static/mp3/dae/03.MP3',
- '/static/mp3/dae/04.MP3',
- ],
- }
- const imageList = {
- 0: ['/static/images/game/game-img1.gif', '/static/images/game/game-eat-img1.gif'],
- 1: ['/static/images/game/game-img2.gif', '/static/images/game/game-eat-img2.gif'],
- 2: ['/static/images/game/game-img3.gif', '/static/images/game/game-eat-img3.gif'],
- 3: ['/static/images/game/game-img4.gif', '/static/images/game/game-eat-img4.gif'],
- }
- export function useAudio (grouthType) {
- const defaultGrouthType = ref(''); // 成长状态历史
- const audioIndex = ref(0); // 播放序列
- const innerAudioContext = ref(null);
- function createAudio() {
- const innerAudioContext = uni.createInnerAudioContext();
- innerAudioContext.autoplay = false;
- innerAudioContext.src = audioList[grouthType.value][audioIndex.value];
- innerAudioContext.onPlay(() => {
- // 播放
- uni.$emit('play-audio');
- });
- innerAudioContext.onError((res) => {
- audioIndex.value = 0;
- uni.$emit('play-error')
- console.log(res.errMsg);
- console.log(res.errCode);
- });
- innerAudioContext.onEnded(() => {
- // 播放结束
- uni.$emit('play-audio-ended')
- // 计算播放音频序列
- const list = audioList[grouthType.value];
- if (audioIndex.value >= list.length-1) {
- audioIndex.value = 0;
- } else {
- audioIndex.value = audioIndex.value+1;
- }
- // 更新音频源
- changeSource(list[audioIndex.value]);
- })
- return innerAudioContext;
- }
- function changeSource(url) {
- innerAudioContext.src= url;
- innerAudioContext.currentTime = 0;
- }
-
- onHide(() => {
- innerAudioContext.value.stop();
- })
-
- onUnload(() => {
- innerAudioContext.value.stop();
- })
-
- // 展开食物停止播放
- uni.$on('food-select-open', () => {
- innerAudioContext.value.stop();
- audioIndex.value = 0;
- })
-
- innerAudioContext.value = createAudio();
-
- function handlePlay() {
- innerAudioContext.value.destroy();
- innerAudioContext.value = null;
- innerAudioContext.value = createAudio();
- innerAudioContext.value.play()
- }
-
- watch(grouthType, () => {
- if (defaultGrouthType.value != grouthType.value) {
- const list = audioList[grouthType.value];
- // 当鹅成长状态变更时重置播放序列
- audioIndex.value = 0;
- defaultGrouthType.value = grouthType.value;
- changeSource(audioList[grouthType.value][0]);
- }
- })
-
- return {
- innerAudioContext,
- handlePlay
- }
- }
- export function useHuDong(props) {
- const imgUrl = ref('');
-
- function getDefaultImg() {
- const active = gooseGrowthTypeList.find(item => item.id == props.growthType);
- return active ? active.imgUrl: ''
- }
- // 初始化
- function init() {
- imgUrl.value = getDefaultImg();
- }
- function doTouch(handlePlay) {
- handlePlay();
- }
-
- // 播放开始切换动画
- uni.$on('play-audio', () => {
- imgUrl.value = imageList[props.growthType][1]
- })
- // 播放结束恢复默认
- uni.$on('play-audio-ended', () => {
- imgUrl.value = imageList[props.growthType][0]
- })
- // 展开食物恢复默认
- uni.$on('food-select-open', () => {
- imgUrl.value = imageList[props.growthType][0]
- })
- uni.$on('play-error',() => {
- imgUrl.value = imageList[props.growthType][0]
- })
- return {
- init,
- doTouch,
- imgUrl
- }
-
- }
|