Browse Source

fix: midi播放结束状态

TIANYONG 1 year ago
parent
commit
7b156073da
2 changed files with 34 additions and 9 deletions
  1. 5 0
      src/helpers/communication.ts
  2. 29 9
      src/view/audio-list/index.tsx

+ 5 - 0
src/helpers/communication.ts

@@ -462,6 +462,11 @@ export const api_cloudplayed = (callback: any) => {
 	listenerMessage("cloudplayed", callback);
 };
 
+/** 卸载midi播放结束回调 */
+export const api_remove_cloudplayed = (callback: any) => {
+	removeListenerMessage("cloudplayed", callback);
+};
+
 /** midi评测传offsetTime和micDelay */
 export const api_midiMicDelay = (content: any) => {
 	postMessage({

+ 29 - 9
src/view/audio-list/index.tsx

@@ -1,4 +1,4 @@
-import { computed, defineComponent, onMounted, reactive, ref, watch } from "vue";
+import { computed, defineComponent, onMounted, onUnmounted, reactive, ref, watch } from "vue";
 import styles from "./index.module.less";
 import {
 	getMidiCurrentTime,
@@ -9,7 +9,7 @@ import {
 	setMidiCurrentTime,
 } from "./midiPlayer";
 import state, { IPlayState, onEnded, onPlay } from "/src/state";
-import { api_playProgress, api_cloudTimeUpdae } from "/src/helpers/communication";
+import { api_playProgress, api_cloudTimeUpdae, api_cloudplayed, api_remove_cloudplayed } from "/src/helpers/communication";
 import { evaluatingData } from "/src/view/evaluating";
 import { cloudToggleState } from "/src/helpers/midiPlay"
 
@@ -173,15 +173,27 @@ export default defineComponent({
 		// midi播放进度回调
 		const midiProgress = (res: any) => {
 			// console.log('api',res)
+			if (audioData.duration == 0) {
+				const songEndTime = state.times[state.times.length - 1 || 0]?.endtime || 0
+				audioData.duration = songEndTime
+			}
 			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
-			) {
+			// if (
+			// 	audioData.duration > 1000 &&
+			// 	currentTime >= audioData.duration * 1000
+			// ) {
+			// 	console.log('midi结束')
+			// 	onEnded();
+			// }
+		}
+		// midi播放结束回调
+		const midiPlayEnd = (res: any) => {
+			if (res) {
+				console.log('midi结束')
+				audioData.progress = 0
 				onEnded();
 			}
 		}
@@ -209,9 +221,17 @@ export default defineComponent({
 				);
 
 				api_playProgress(progress);
+			} else {
+				const songEndTime = state.times[state.times.length - 1 || 0]?.endtime || 0
+				audioData.duration = songEndTime
+				// 监听midi播放进度
+				api_cloudTimeUpdae(midiProgress);
+				// 监听midi播放结束
+				api_cloudplayed(midiPlayEnd);
 			}
-			// 监听midi播放进度
-			api_cloudTimeUpdae(midiProgress);
+		});
+		onUnmounted(() => {
+			api_remove_cloudplayed(midiPlayEnd);
 		});
 
 		// console.log(state.playMode, state.midiUrl);