|
@@ -39,46 +39,59 @@ const imageList = {
|
|
export function useAudio (grouthType) {
|
|
export function useAudio (grouthType) {
|
|
const defaultGrouthType = ref(''); // 成长状态历史
|
|
const defaultGrouthType = ref(''); // 成长状态历史
|
|
const audioIndex = ref(0); // 播放序列
|
|
const audioIndex = ref(0); // 播放序列
|
|
- const innerAudioContext = uni.createInnerAudioContext();
|
|
|
|
- innerAudioContext.autoplay = false;
|
|
|
|
- innerAudioContext.src = audioList[grouthType.value][0];
|
|
|
|
- innerAudioContext.onPlay(() => {
|
|
|
|
- // 播放
|
|
|
|
- uni.$emit('play-audio');
|
|
|
|
- });
|
|
|
|
- innerAudioContext.onError((res) => {
|
|
|
|
- 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]);
|
|
|
|
- })
|
|
|
|
-
|
|
|
|
|
|
+ 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) {
|
|
function changeSource(url) {
|
|
innerAudioContext.src= url;
|
|
innerAudioContext.src= url;
|
|
innerAudioContext.currentTime = 0;
|
|
innerAudioContext.currentTime = 0;
|
|
}
|
|
}
|
|
|
|
|
|
onHide(() => {
|
|
onHide(() => {
|
|
- innerAudioContext.stop();
|
|
|
|
|
|
+ innerAudioContext.value.stop();
|
|
})
|
|
})
|
|
|
|
|
|
// 展开食物停止播放
|
|
// 展开食物停止播放
|
|
uni.$on('food-select-open', () => {
|
|
uni.$on('food-select-open', () => {
|
|
- innerAudioContext.stop();
|
|
|
|
|
|
+ innerAudioContext.value.stop();
|
|
audioIndex.value = 0;
|
|
audioIndex.value = 0;
|
|
})
|
|
})
|
|
-
|
|
|
|
|
|
+
|
|
|
|
+ innerAudioContext.value = createAudio();
|
|
|
|
+
|
|
|
|
+ function handlePlay() {
|
|
|
|
+ innerAudioContext.value.destroy();
|
|
|
|
+ innerAudioContext.value = null;
|
|
|
|
+ innerAudioContext.value = createAudio();
|
|
|
|
+ innerAudioContext.value.play()
|
|
|
|
+ }
|
|
|
|
|
|
watch(grouthType, () => {
|
|
watch(grouthType, () => {
|
|
if (defaultGrouthType.value != grouthType.value) {
|
|
if (defaultGrouthType.value != grouthType.value) {
|
|
@@ -92,6 +105,7 @@ export function useAudio (grouthType) {
|
|
|
|
|
|
return {
|
|
return {
|
|
innerAudioContext,
|
|
innerAudioContext,
|
|
|
|
+ handlePlay
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
@@ -109,8 +123,8 @@ export function useHuDong(props) {
|
|
imgUrl.value = getDefaultImg();
|
|
imgUrl.value = getDefaultImg();
|
|
}
|
|
}
|
|
|
|
|
|
- function doTouch(myAudio) {
|
|
|
|
- myAudio.play();
|
|
|
|
|
|
+ function doTouch(handlePlay) {
|
|
|
|
+ handlePlay();
|
|
}
|
|
}
|
|
|
|
|
|
// 播放开始切换动画
|
|
// 播放开始切换动画
|
|
@@ -125,7 +139,9 @@ export function useHuDong(props) {
|
|
uni.$on('food-select-open', () => {
|
|
uni.$on('food-select-open', () => {
|
|
imgUrl.value = imageList[props.growthType][0]
|
|
imgUrl.value = imageList[props.growthType][0]
|
|
})
|
|
})
|
|
-
|
|
|
|
|
|
+ uni.$on('play-error',() => {
|
|
|
|
+ imgUrl.value = imageList[props.growthType][0]
|
|
|
|
+ })
|
|
return {
|
|
return {
|
|
init,
|
|
init,
|
|
doTouch,
|
|
doTouch,
|