Преглед на файлове

音频切换时bug 卡 或者 阻断

wangxy преди 1 месец
родител
ревизия
9995d357ef

+ 11 - 3
pages/newEnglish/components/mainCard.vue

@@ -74,6 +74,7 @@
 	const emits = defineEmits(['play-audio', 'goXiangjie', 'swiper-change'])
 	const currentIndex = ref(1)
   const mySwiperRef = ref(null);
+	const isXunHuanbofangYinbiao = ref(false)
 
 
 	const luyinSuccess = (imagePath, text, audioPath) => {
@@ -99,8 +100,8 @@
 	}
 
 	function onChange(code) {
-    console.log('code',code)
-    currentIndex.value = (code + 1)
+		if (props.isPlaying || isXunHuanbofangYinbiao.value) return;
+		currentIndex.value = (code + 1)
 	}
 
 	function handlePlayAudio({
@@ -116,14 +117,21 @@
 	function goXiangjie() {
 		emits('goXiangjie')
 	}
+	
+	function updateYinbiaoSataus(da) {
+		isXunHuanbofangYinbiao.value = da
+	}
+	
 	onMounted(() => {
 		statusAudio.autoplay = false
 		statusAudio.onEnded(() => {
 			statusPopup.value.close()
 		})
+		uni.$on('xunhuanYinbiaoBofang',updateYinbiaoSataus)
 	})
 	onUnmounted(() => {
-		statusAudio.destroy()
+		statusAudio.destroy();
+		uni.$on('xunhuanYinbiaoBofang',updateYinbiaoSataus)
 	})
 </script>
 

+ 41 - 2
pages/newEnglish/components/useAudio.js

@@ -25,6 +25,35 @@ audioContext.onPlay(() => {
 	// console.log('播放事件:', code)
 	uni.$emit('danci-audio-play', code);
 });
+audioContext.onError((err) => {
+	// 播放
+	uni.$emit('danci-audio-ended', code)
+	audioContext?.stop();
+	// uni.$emit('danci-audio-play', code);
+});
+function createAudioNew() {
+	audioContext = uni.createInnerAudioContext(); // 单例模式[3](@ref)
+	audioContext.onEnded(() => {
+		// console.log('触发播放结束')
+		// 播放结束
+		uni.$emit('danci-audio-ended', code)
+		audioContext?.stop();
+	
+	})
+	audioContext.onPlay(() => {
+		// 播放
+		// console.log('播放事件:', code)
+		uni.$emit('danci-audio-play', code);
+	});
+	audioContext.onError((err) => {
+		// 播放
+		uni.$emit('danci-audio-ended', code)
+		audioContext?.stop();
+		// uni.$emit('danci-audio-play', code);
+	});
+}
+
+
 
 export class audioPlayer {
 	// 播放音频
@@ -32,8 +61,18 @@ export class audioPlayer {
 		// console.log('播放文件地址', path)
 		code = code1;
 		if (audioContext.src === path && !audioContext.paused) return;
-		audioContext.src = path;
-		audioContext.play();
+		audioContext.stop();
+		audioContext.destroy();
+		audioContext = null;
+		
+		nextTick(() => {
+			createAudioNew();
+			
+			audioContext.src = path;
+			audioContext.play();
+		})
+		
+
 	}
 
 	// 暂停播放

+ 34 - 2
pages/newEnglish/components/useAudioRightWrong.js

@@ -1,3 +1,5 @@
+import { nextTick } from "vue";
+
 let audioContext = null;
 let code = null; // 身份标识
 audioContext = uni.createInnerAudioContext(); // 单例模式[3](@ref)
@@ -9,6 +11,10 @@ audioContext.onPlay(() => {
 	// 播放
 	uni.$emit('result-audio-play', code);
 });
+audioContext.onError((err) => {
+	// 播放
+	uni.$emit('result-audio-ended', code)
+});
 
 const audioList = {
 	right: '/static/mp3/newYingyu/right-tip.mp3',
@@ -20,6 +26,24 @@ export const resultImageList = {
 	wrong: '/static/images/study/cjdc/error-tip-img-gif'
 }
 
+function createAudioNew() {
+	audioContext = uni.createInnerAudioContext(); // 单例模式[3](@ref)
+	audioContext.onEnded(() => {
+		// 播放结束
+		uni.$emit('result-audio-ended', code)
+	})
+	audioContext.onPlay(() => {
+		// 播放
+		uni.$emit('result-audio-play', code);
+	});
+	audioContext.onError((err) => {
+		// 播放
+		console.log('errr',err)
+		uni.$emit('result-audio-ended', code);
+	});
+}
+
+
 
 export class resultAudioPlayer {
 	// 播放音频
@@ -29,8 +53,16 @@ export class resultAudioPlayer {
 			codeT
 		};
 		if (audioContext.src === audioList[codeT] && !audioContext.paused) return;
-		audioContext.src = audioList[codeT];
-		audioContext.play();
+		audioContext.stop();
+		audioContext.destroy();
+		audioContext = null;
+		
+		nextTick(() => {
+			createAudioNew();
+			audioContext.src = audioList[codeT];
+			audioContext.play();
+		})
+	
 	}
 
 	// 暂停播放

+ 6 - 0
pages/newEnglish/components/useYinbiao.js

@@ -17,6 +17,7 @@ export function useYinBiaoAutoPlay () {
 
     async function handlePlay() {
         isAutoPlaying.value = true;
+		uni.$emit('xunhuanYinbiaoBofang', isAutoPlaying.value)
         code = new Date().getTime();
         const activeYin = {
             url: list.value[current.value].yinpin,
@@ -31,6 +32,7 @@ export function useYinBiaoAutoPlay () {
                 icon: 'none'
             });
             isAutoPlaying.value = false;
+			uni.$emit('xunhuanYinbiaoBofang', isAutoPlaying.value)
         }
     }
 
@@ -39,6 +41,7 @@ export function useYinBiaoAutoPlay () {
 
             if (code !== mCode) {
                 isAutoPlaying.value = false;
+				uni.$emit('xunhuanYinbiaoBofang', isAutoPlaying.value)
                 return;
             }
 
@@ -49,6 +52,7 @@ export function useYinBiaoAutoPlay () {
             } else {
                 // 播放结束
                 isAutoPlaying.value = false;
+				uni.$emit('xunhuanYinbiaoBofang', isAutoPlaying.value)
             }
         })
     }
@@ -60,6 +64,8 @@ export function useYinBiaoAutoPlay () {
 
 
     function playYinbiaoAuto(clist) {
+		isAutoPlaying.value = true;
+		uni.$emit('xunhuanYinbiaoBofang', isAutoPlaying.value)
         current.value = 0
         list.value = clist;
         // 执行首次播放