wangxy 4 mēneši atpakaļ
vecāks
revīzija
acc39a9fb5
2 mainītis faili ar 52 papildinājumiem un 32 dzēšanām
  1. 5 1
      pages/game/components/goose.vue
  2. 47 31
      pages/game/components/useAudio.js

+ 5 - 1
pages/game/components/goose.vue

@@ -40,6 +40,7 @@
 	} = useHuDong(props);
 	const {
 		innerAudioContext,
+		handlePlay
 	} = useAudio(growthType);
 
 	init();
@@ -49,7 +50,10 @@
 	})
 
 	function handleTouch() {
-		doTouch(innerAudioContext);
+		if (!innerAudioContext.value.paused) {
+			return;
+		}
+		doTouch(handlePlay);
 	}
 
 </script>

+ 47 - 31
pages/game/components/useAudio.js

@@ -39,46 +39,59 @@ const imageList = {
 export function useAudio (grouthType) {
 	const defaultGrouthType = ref(''); // 成长状态历史
 	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) {
 		innerAudioContext.src= url;
 		innerAudioContext.currentTime = 0;
 	}
 	
 	onHide(() => {
-		innerAudioContext.stop();
+		innerAudioContext.value.stop();
 	})
 
 	// 展开食物停止播放
 	uni.$on('food-select-open', () => {
-		innerAudioContext.stop();
+		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) {
@@ -92,6 +105,7 @@ export function useAudio (grouthType) {
 	
 	return {
 		innerAudioContext,
+		handlePlay
 	}
 }
 
@@ -109,8 +123,8 @@ export function useHuDong(props) {
 		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', () => {
 		imgUrl.value = imageList[props.growthType][0]
 	})
-	
+	uni.$on('play-error',() => {
+		imgUrl.value = imageList[props.growthType][0]
+	})
 	return {
 		init,
 		doTouch,