liushengqiang il y a 2 ans
Parent
commit
8973a9730a

+ 9 - 0
src/helpers/communication.ts

@@ -259,3 +259,12 @@ export const api_keepScreenLongLight = () => {
 		},
 	});
 };
+
+/** 监听APP播放 */
+export const api_playProgress = (callback: any) => {
+	listenerMessage("playProgress", callback);
+};
+/** 卸载播放 */
+export const api_remove_playProgress = (callback: any) => {
+	removeListenerMessage("playProgress", callback);
+};

+ 3 - 2
src/page-instrument/evaluat-model/delay-check/index.tsx

@@ -28,6 +28,7 @@ import icon_3_1 from "./image/icon_3_1.png";
 import icon_3_2 from "./image/icon_3_2.png";
 import { evaluatingData } from "/src/view/evaluating";
 import { IPostMessage } from "/src/utils/native-message";
+import state from "/src/state";
 
 export default defineComponent({
 	name: "delay-check",
@@ -53,7 +54,7 @@ export default defineComponent({
 		};
 		/** 持续检测耳机状态 */
 		const keepCheckEarphone = async () => {
-			if (data.checkStatus === "end") return;
+			if (data.step >= 7) return;
 			evaluatingData.earphone = await getEarphoneState();
 			if (evaluatingData.earphone) {
 				if (data.step <= 5) {
@@ -111,8 +112,8 @@ export default defineComponent({
 		};
 		const hanldeEndTune = () => {
 			data.step = 7;
-			data.checkStatus = "end";
 			anim.value?.play();
+			state.setting.soundEffect = false;
 		};
 
 		const toggleTune = async (state: "start" | "stop" | "finishTune") => {

+ 1 - 0
src/page-instrument/evaluat-model/index.tsx

@@ -240,6 +240,7 @@ export default defineComponent({
 		};
 
 		onMounted(() => {
+			evaluatingData.isDisabledPlayMusic = true
 			handlePerformDetection();
 		});
 		return () => (

+ 0 - 1
src/state.ts

@@ -284,7 +284,6 @@ export const skipNotePlay = (itemIndex: number, isStart = false) => {
 	if (isStart) {
 		itemTime = 0;
 	}
-	// console.log("🚀 ~ itemTime:", itemTime);
 	if (item) {
 		setAudioCurrentTime(itemTime, itemIndex);
 		gotoNext(item);

+ 1 - 1
src/utils/native-message.ts

@@ -47,7 +47,7 @@ if (instance) {
 					data.content = JSON.parse(data.content);
 				}
 			} catch (error) {}
-			console.log("h5_接受_api:", data?.api, data.content);
+			// console.log("h5_接受_api:", data?.api, data.content);
 			if (!uuid) {
 				const keys = Object.keys(calls).filter((key) => key.indexOf(data.api) === 0);
 				for (const key of keys) {

+ 60 - 25
src/view/audio-list/index.tsx

@@ -1,19 +1,29 @@
 import { computed, defineComponent, onMounted, reactive, ref, watch } from "vue";
 import styles from "./index.module.less";
-import { getMidiCurrentTime, getMidiDuration, handleTogglePlayMidi, hanldeInitMidiData, hanldeSetMidiPlaybackRate, setMidiCurrentTime } from "./midiPlayer";
+import {
+	getMidiCurrentTime,
+	getMidiDuration,
+	handleTogglePlayMidi,
+	hanldeInitMidiData,
+	hanldeSetMidiPlaybackRate,
+	setMidiCurrentTime,
+} from "./midiPlayer";
 import state, { IPlayState, onEnded, onPlay } from "/src/state";
+import { api_playProgress } from "/src/helpers/communication";
 
 const audioData = reactive({
 	songEle: null as unknown as HTMLAudioElement,
 	backgroundEle: null as unknown as HTMLAudioElement,
 	midiRender: false,
+	progress: 0,
+	duration: 0
 });
 const midiRef = ref();
 /** 播放或暂停 */
 export const audioListStart = (type: "play" | "paused") => {
 	// 开始播放之前, 先设置倍数
-	if( type === "play" && state.originSpeed !== 0){
-		setAudioPlaybackRate(state.speed / state.originSpeed)
+	if (type === "play" && state.originSpeed !== 0) {
+		setAudioPlaybackRate(state.speed / state.originSpeed);
 	}
 	// 如果是midi播放
 	if (audioData.midiRender) {
@@ -46,9 +56,9 @@ export const getAudioCurrentTime = () => {
 		const c = getMidiCurrentTime();
 		return c;
 	}
-	if (state.playSource === "music") return audioData.songEle?.currentTime;
-	if (state.playSource === "background") return audioData.backgroundEle?.currentTime;
-	return audioData.songEle?.currentTime;
+	if (state.playSource === "music") return audioData.songEle?.currentTime || audioData.progress;
+	if (state.playSource === "background") return audioData.backgroundEle?.currentTime || audioData.progress;
+	return audioData.songEle?.currentTime || audioData.progress;
 };
 /** 获取曲谱的总时间 */
 export const getAudioDuration = () => {
@@ -57,7 +67,7 @@ export const getAudioDuration = () => {
 		const d = getMidiDuration();
 		return d;
 	}
-	return audioData.songEle?.duration || audioData.backgroundEle?.duration || 0;
+	return audioData.songEle?.duration || audioData.backgroundEle?.duration || audioData.duration;
 };
 
 /** 设置播放的开始时间 */
@@ -69,6 +79,7 @@ export const setAudioCurrentTime = (time: number, index = 0) => {
 	}
 	audioData.songEle && (audioData.songEle.currentTime = time);
 	audioData.backgroundEle && (audioData.backgroundEle.currentTime = time);
+	audioData.progress = time;
 };
 
 /** 设置当前没有播放的音频静音 */
@@ -95,7 +106,6 @@ export const detectTheNumberOfSoundSources = () => {
 export default defineComponent({
 	name: "audio-list",
 	setup() {
-
 		/** iframe 加载完成后, 加载midiURL */
 		const handleLoad = () => {
 			midiRef.value.contentWindow.handleRendered = () => {
@@ -133,33 +143,58 @@ export default defineComponent({
 				};
 			});
 		};
+		// 监听评测曲谱音频播放进度,返回
+		const progress = (res: any) => {
+			const currentTime = res?.currentTime || res?.content?.currentTime;
+			const total = res?.totalDuration || res?.content?.totalDuration;
+			const time = currentTime / 1000;
+			audioData.progress = time;
+			audioData.duration = total / 1000;
+			if (
+				res?.content?.totalDuration > 1000 &&
+				currentTime >= total
+			) {
+				onEnded();
+			}
+		};
 
 		onMounted(() => {
 			if (state.playMode !== "MIDI") {
-				Promise.all([createAudio(state.music), createAudio(state.accompany)]).then(([music, accompany]) => {
-					// console.log(music, accompany);
-					if (music) {
-						audioData.songEle = music;
+				Promise.all([createAudio(state.music), createAudio(state.accompany)]).then(
+					([music, accompany]) => {
+						// console.log(music, accompany);
+						if (music) {
+							audioData.songEle = music;
+						}
+						if (accompany) {
+							audioData.backgroundEle = accompany;
+						}
+						if (audioData.songEle) {
+							audioData.songEle.addEventListener("play", onPlay);
+							audioData.songEle.addEventListener("ended", onEnded);
+							accompany && (accompany.muted = true);
+						} else if (audioData.backgroundEle) {
+							audioData.backgroundEle.addEventListener("play", onPlay);
+							audioData.backgroundEle.addEventListener("ended", onEnded);
+						}
 					}
-					if (accompany) {
-						audioData.backgroundEle = accompany;
-					}
-					if (audioData.songEle) {
-						audioData.songEle.addEventListener("play", onPlay);
-						audioData.songEle.addEventListener("ended", onEnded);
-						accompany && (accompany.muted = true)
-					} else if (audioData.backgroundEle) {
-						audioData.backgroundEle.addEventListener("play", onPlay);
-						audioData.backgroundEle.addEventListener("ended", onEnded);
-					}
-				});
+				);
+
+				api_playProgress(progress);
 			}
 		});
 
 		// console.log(state.playMode, state.midiUrl);
 		return () => (
 			<div class={styles.audioList}>
-				{state.playMode === "MIDI" && state.speed != 0 && <iframe style={{ display: "none" }} ref={midiRef} src={`/midi/index.html`} onLoad={handleLoad} />}
+				{state.playMode === "MIDI" && state.speed != 0 && (
+					<iframe
+						style={{ display: "none" }}
+						ref={midiRef}
+						src={`/midi/index.html`}
+						onLoad={handleLoad}
+					/>
+				)}
 			</div>
 		);
 	},

+ 38 - 14
src/view/evaluating/index.tsx

@@ -26,6 +26,7 @@ import state, {
 	IPlayState,
 	clearSelection,
 	handleStopPlay,
+	onPlay,
 	resetPlaybackToStart,
 	togglePlay,
 } from "/src/state";
@@ -33,6 +34,7 @@ import { IPostMessage } from "/src/utils/native-message";
 import { usePageVisibility } from "@vant/use";
 import { browser } from "/src/utils";
 import { getAudioCurrentTime, toggleMutePlayAudio } from "../audio-list";
+import { handleStartTick } from "../tick";
 
 const browserInfo = browser();
 export const evaluatingData = reactive({
@@ -59,6 +61,8 @@ export const evaluatingData = reactive({
 	resulstMode: false,
 	/** 是否是完整评测 */
 	isComplete: false,
+	/**  */
+	isDisabledPlayMusic: false,
 });
 
 /** 点击开始评测按钮 */
@@ -145,26 +149,26 @@ export const handlePerformDetection = async () => {
 	if (evaluatingData.checkStep === 0) {
 		evaluatingData.checkStep = 5;
 		// 没有设备延迟数据 或 开启了效音 显示检测组件,并持续检测耳机状态
-		if (state.setting.soundEffect){
-			evaluatingData.soundEffectMode = true
-			return
+		if (state.setting.soundEffect) {
+			evaluatingData.soundEffectMode = true;
+			return;
 		}
-		const delayTime = await api_getDeviceDelay()
-		console.log("🚀 ~ delayTime:", delayTime)
-		if (!delayTime){
-			evaluatingData.soundEffectMode = true
-			return
+		const delayTime = await api_getDeviceDelay();
+		console.log("🚀 ~ delayTime:", delayTime);
+		if (!delayTime) {
+			evaluatingData.soundEffectMode = true;
+			return;
 		}
 		handlePerformDetection();
-		return
+		return;
 	}
 	// 检测耳机
 	if ((evaluatingData.checkStep = 5)) {
 		evaluatingData.checkStep = 10;
 		const erji = await checkUseEarphone();
-		if (!erji){
+		if (!erji) {
 			evaluatingData.earphoneMode = true;
-			return
+			return;
 		}
 		handlePerformDetection();
 		return;
@@ -243,8 +247,23 @@ export const handleStartBegin = async () => {
 		return;
 	}
 	evaluatingData.startBegin = true;
+	if (evaluatingData.isDisabledPlayMusic) {
+		state.playState = state.playState === "paused" ? "play" : "paused";
+		// 设置为开始播放时, 如果需要节拍,先播放节拍器
+		if (state.playState === "play" && state.needTick) {
+			const tickend = await handleStartTick();
+			// console.log("🚀 ~ tickend:", tickend)
+			// 节拍器返回false, 取消播放
+			if (!tickend) {
+				state.playState = "paused";
+				evaluatingData.startBegin = false;
+				return;
+			}
+		}
+		onPlay();
+	}
 	//开始录音
-	api_startRecording();
+	await api_startRecording();
 };
 
 /** 播放音乐 */
@@ -258,6 +277,7 @@ const playMusic = async () => {
 	}
 	// 检测播放进度, 计算延迟
 	check_currentTime();
+
 	// 如果开启了摄像头, 开启录制视频
 	if (state.setting.camera && state.setting.saveToAlbum) {
 		console.log("开始录制视频");
@@ -269,6 +289,7 @@ let _audio: HTMLAudioElement;
 
 /** 录音开始,记录开始时间点 */
 const recordStartTimePoint = async (res?: IPostMessage) => {
+	console.error("开始录音");
 	// 没有开始评测,不处理
 	if (!evaluatingData.startBegin) return;
 	let inteveral = res?.content?.inteveral || 0;
@@ -284,12 +305,14 @@ const recordStartTimePoint = async (res?: IPostMessage) => {
 		"记录时间点:",
 		Date.now()
 	);
+	// 是否禁播
+	if (evaluatingData.isDisabledPlayMusic) {
+		return;
+	}
 	// 开始播放
 	playMusic();
-
 };
 
-
 /**
  * 结束评测
  * @param isComplete 是否完整评测
@@ -425,6 +448,7 @@ export default defineComponent({
 		});
 
 		onMounted(() => {
+			resetPlaybackToStart();
 			hanlde_record();
 			evaluatingData.resultData = {};