|
@@ -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>
|
|
|
);
|
|
|
},
|