浏览代码

update 选 拼

wangxy 3 月之前
父节点
当前提交
8e53328f3d
共有 1 个文件被更改,包括 55 次插入0 次删除
  1. 55 0
      pages/newEnglish/components/useAudio.js

+ 55 - 0
pages/newEnglish/components/useAudio.js

@@ -0,0 +1,55 @@
+	import {
+		reactive,
+		ref
+	} from 'vue';
+	import { onHide, onUnload} from "@dcloudio/uni-app"
+
+export function useAudio (url) {
+	const innerAudioContext = ref(null);
+	function createAudio() {
+		const innerAudioContext = uni.createInnerAudioContext();
+		innerAudioContext.autoplay = false;
+		innerAudioContext.src = url;
+		innerAudioContext.onPlay(() => {
+			// 播放
+			uni.$emit('play-audio');
+		});
+		innerAudioContext.onError((res) => {
+		  uni.$emit('play-error')
+		});
+		innerAudioContext.onEnded(() => {
+			// 播放结束
+			uni.$emit('play-audio-ended')
+		})
+		return innerAudioContext;
+	}
+	
+	onHide(() => {
+		innerAudioContext.value.stop();
+	})
+	
+	onUnload(() => {
+		innerAudioContext.value.stop();
+	})
+	
+
+	// 展开食物停止播放
+	uni.$on('swiper-change', () => {
+		innerAudioContext.value.stop();
+	})
+	
+	innerAudioContext.value = createAudio();
+	
+	function handlePlay() {
+		innerAudioContext.value.destroy();
+		innerAudioContext.value = null;
+		innerAudioContext.value = createAudio();
+		innerAudioContext.value.play()
+	}
+	
+	return {
+		innerAudioContext,
+		handlePlay
+	}
+}
+