index.tsx 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. import { computed, defineComponent, onMounted, onUnmounted, reactive, ref, watch } from "vue";
  2. import styles from "./index.module.less";
  3. import {
  4. getMidiCurrentTime,
  5. getMidiDuration,
  6. handleTogglePlayMidi,
  7. hanldeInitMidiData,
  8. hanldeSetMidiPlaybackRate,
  9. setMidiCurrentTime,
  10. } from "./midiPlayer";
  11. import state, { IPlayState, onEnded, onPlay } from "/src/state";
  12. import { api_playProgress, api_cloudTimeUpdae, api_cloudplayed, api_remove_cloudplayed, api_remove_cloudTimeUpdae } from "/src/helpers/communication";
  13. import { evaluatingData } from "/src/view/evaluating";
  14. import { cloudToggleState } from "/src/helpers/midiPlay"
  15. export const audioData = reactive({
  16. songEle: null as unknown as HTMLAudioElement,
  17. backgroundEle: null as unknown as HTMLAudioElement,
  18. midiRender: false,
  19. progress: 0, // midi播放进度(单位:秒)
  20. duration: 0 // 音频总时长(单位:秒)
  21. });
  22. const midiRef = ref();
  23. /** 播放或暂停 */
  24. export const audioListStart = (type: "play" | "paused") => {
  25. // 开始播放之前, 先设置倍数
  26. if (type === "play" && state.originSpeed !== 0) {
  27. setAudioPlaybackRate(state.speed / state.originSpeed);
  28. }
  29. // console.log('api','midi状态1',type,state.isAppPlay)
  30. // 如果是midi播放
  31. if (state.isAppPlay) {
  32. // handleTogglePlayMidi(type);
  33. cloudToggleState(type);
  34. return;
  35. }
  36. if (type === "play") {
  37. audioData.songEle?.play();
  38. audioData.backgroundEle?.play();
  39. } else if (type === "paused") {
  40. audioData.songEle?.pause();
  41. audioData.backgroundEle?.pause();
  42. }
  43. };
  44. /** 设置倍数播放 */
  45. export const setAudioPlaybackRate = (rate: number) => {
  46. // 如果是midi播放
  47. if (state.isAppPlay) {
  48. if (state.modeType === "evaluating") return
  49. hanldeSetMidiPlaybackRate(rate);
  50. return;
  51. }
  52. audioData.songEle && (audioData.songEle.playbackRate = rate);
  53. audioData.backgroundEle && (audioData.backgroundEle.playbackRate = rate);
  54. };
  55. /** 获取当前播放的时间 */
  56. export const getAudioCurrentTime = () => {
  57. // 如果是midi播放
  58. if (state.isAppPlay) {
  59. // const c = getMidiCurrentTime();
  60. return audioData.progress;
  61. }
  62. // console.log('返回的时间',state.playSource, audioData.songEle?.currentTime,audioData.progress)
  63. if (state.playSource === "music") return audioData.songEle?.currentTime || audioData.progress;
  64. if (state.playSource === "background") return audioData.backgroundEle?.currentTime || audioData.progress;
  65. return audioData.songEle?.currentTime || audioData.progress;
  66. };
  67. /** 获取曲谱的总时间 */
  68. export const getAudioDuration = () => {
  69. // 如果是midi播放
  70. if (state.isAppPlay) {
  71. // const d = getMidiDuration();
  72. const songEndTime = state.times[state.times.length - 1 || 0]?.endtime || 0
  73. return audioData.duration || songEndTime;
  74. }
  75. return audioData.songEle?.duration || audioData.backgroundEle?.duration || audioData.duration;
  76. };
  77. /** 设置播放的开始时间 */
  78. export const setAudioCurrentTime = (time: number, index = 0) => {
  79. // console.log('开始时间12345',time)
  80. // 如果是midi播放
  81. if (state.isAppPlay) {
  82. setMidiCurrentTime(index);
  83. return;
  84. }
  85. audioData.songEle && (audioData.songEle.currentTime = time);
  86. audioData.backgroundEle && (audioData.backgroundEle.currentTime = time);
  87. audioData.progress = time;
  88. };
  89. /** 设置当前没有播放的音频静音 */
  90. export const toggleMutePlayAudio = (source: IPlayState, muted: boolean) => {
  91. if (source === "music") {
  92. if (audioData.songEle) {
  93. audioData.songEle.muted = muted;
  94. }
  95. } else if (source === "background") {
  96. if (audioData.backgroundEle) {
  97. audioData.backgroundEle.muted = muted;
  98. }
  99. }
  100. };
  101. /** 检测音源数量 */
  102. export const detectTheNumberOfSoundSources = () => {
  103. let total = 0;
  104. if (audioData.songEle) total += 1;
  105. if (audioData.backgroundEle) total += 1;
  106. return total;
  107. };
  108. export default defineComponent({
  109. name: "audio-list",
  110. setup() {
  111. /** iframe 加载完成后, 加载midiURL */
  112. const handleLoad = () => {
  113. midiRef.value.contentWindow.handleRendered = () => {
  114. audioData.midiRender = true
  115. };
  116. hanldeInitMidiData(midiRef.value);
  117. };
  118. watch(
  119. () => state.playSource,
  120. () => {
  121. if (state.modeType === "evaluating" && !state.setting.enableAccompaniment) {
  122. console.log("评测模式设置了关闭伴奏,不切换原音伴奏");
  123. return;
  124. }
  125. if (state.playSource === "music") {
  126. audioData.songEle && (audioData.songEle.muted = false);
  127. audioData.backgroundEle && (audioData.backgroundEle.muted = true);
  128. } else {
  129. audioData.songEle && (audioData.songEle.muted = true);
  130. audioData.backgroundEle && (audioData.backgroundEle.muted = false);
  131. }
  132. }
  133. );
  134. const createAudio = (src: string): Promise<HTMLAudioElement | null> => {
  135. return new Promise((resolve) => {
  136. const a = new Audio(src + '?v=' + Date.now());
  137. a.load();
  138. a.onloadedmetadata = () => {
  139. resolve(a);
  140. };
  141. a.onerror = () => {
  142. resolve(null);
  143. };
  144. });
  145. };
  146. // 监听评测曲谱音频播放进度,返回
  147. const progress = (res: any) => {
  148. const currentTime = res?.currentTime || res?.content?.currentTime;
  149. const total = res?.totalDuration || res?.content?.totalDuration;
  150. const time = currentTime / 1000;
  151. audioData.progress = time;
  152. audioData.songEle && (audioData.songEle.currentTime = time);
  153. audioData.backgroundEle && (audioData.backgroundEle.currentTime = time);
  154. audioData.duration = total / 1000;
  155. if (
  156. res?.content?.totalDuration > 1000 &&
  157. currentTime >= total
  158. ) {
  159. if (evaluatingData.isAudioPlayEnd) return
  160. evaluatingData.isAudioPlayEnd = true
  161. onEnded();
  162. }
  163. };
  164. // midi播放进度回调
  165. const midiProgress = (res: any) => {
  166. // console.log('api',res)
  167. if (audioData.duration == 0) {
  168. const songEndTime = state.times[state.times.length - 1 || 0]?.endtime || 0
  169. audioData.duration = songEndTime
  170. }
  171. const currentTime = res?.currentTime || res?.content?.currentTime;
  172. const total = res?.totalDuration || res?.content?.totalDuration;
  173. const time = currentTime / 1000;
  174. audioData.progress = time;
  175. // if (
  176. // audioData.duration > 1000 &&
  177. // currentTime >= audioData.duration * 1000
  178. // ) {
  179. // console.log('midi结束')
  180. // onEnded();
  181. // }
  182. // 选段模式,播放结束
  183. if (state.sectionStatus && state.section.length == 2 && currentTime >= state.section) {
  184. //
  185. }
  186. }
  187. // midi播放结束回调
  188. const midiPlayEnd = (res: any) => {
  189. if (res) {
  190. console.log('midi结束')
  191. audioData.progress = 0
  192. onEnded();
  193. }
  194. }
  195. onMounted(() => {
  196. if (state.playMode !== "MIDI") {
  197. Promise.all([createAudio(state.music), createAudio(state.accompany)]).then(
  198. ([music, accompany]) => {
  199. state.audioDone = true;
  200. // console.log(music, accompany);
  201. if (music) {
  202. audioData.songEle = music;
  203. }
  204. if (accompany) {
  205. audioData.backgroundEle = accompany;
  206. }
  207. if (audioData.songEle) {
  208. audioData.songEle.addEventListener("play", onPlay);
  209. audioData.songEle.addEventListener("ended", onEnded);
  210. accompany && (accompany.muted = true);
  211. } else if (audioData.backgroundEle) {
  212. audioData.backgroundEle.addEventListener("play", onPlay);
  213. audioData.backgroundEle.addEventListener("ended", onEnded);
  214. }
  215. }
  216. );
  217. api_playProgress(progress);
  218. } else {
  219. const songEndTime = state.times[state.times.length - 1 || 0]?.endtime || 0
  220. audioData.duration = songEndTime
  221. // 监听midi播放进度
  222. api_cloudTimeUpdae(midiProgress);
  223. // 监听midi播放结束
  224. api_cloudplayed(midiPlayEnd);
  225. }
  226. });
  227. onUnmounted(() => {
  228. api_remove_cloudplayed(midiPlayEnd);
  229. api_remove_cloudTimeUpdae(midiProgress);
  230. });
  231. // console.log(state.playMode, state.midiUrl);
  232. return () => (
  233. <div class={styles.audioList}>
  234. {state.playMode === "MIDI" && state.speed != 0 && (
  235. <iframe
  236. style={{ display: "none" }}
  237. ref={midiRef}
  238. src={`/midi/index.html`}
  239. onLoad={handleLoad}
  240. />
  241. )}
  242. </div>
  243. );
  244. },
  245. });