index.tsx 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682
  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. import { storeData } from "/src/store";
  16. import { handleStartTick } from "../tick";
  17. import Crunker from "/src/utils/crunker"
  18. import tickMp3 from "/src/assets/tick.wav"
  19. import tockMp3 from "/src/assets/tock.wav"
  20. import { metronomeData } from "/src/helpers/metronome";
  21. import { showToast } from "vant"
  22. export const audioData = reactive({
  23. songEle: null as HTMLAudioElement | null, // 原生
  24. backgroundEle: null as HTMLAudioElement | null, //伴唱
  25. mingSongEle: null as HTMLAudioElement | null, //唱名
  26. songCollection: { // 音乐源合集 带节拍器和不带节拍器的
  27. songEle: null as HTMLAudioElement | null,
  28. backgroundEle: null as HTMLAudioElement | null,
  29. fanSongEle: null as HTMLAudioElement | null,
  30. banSongEle: null as HTMLAudioElement | null,
  31. mingSongEle: null as HTMLAudioElement | null,
  32. beatSongEle: null as HTMLAudioElement | null,
  33. beatBackgroundEle: null as HTMLAudioElement | null,
  34. beatFanSongEle: null as HTMLAudioElement | null,
  35. beatBanSongEle: null as HTMLAudioElement | null,
  36. beatMingSongEle: null as HTMLAudioElement | null
  37. },
  38. midiRender: false,
  39. progress: 0, // midi播放进度(单位:秒)
  40. duration: 0, // 音频总时长(单位:秒)
  41. mingSongType: 1 as 0 | 1,
  42. mingSongTypeCollection: {
  43. mingSongEle: null as HTMLAudioElement | null,
  44. mingSongGirlEle: null as HTMLAudioElement | null,
  45. beatMingSongEle: null as HTMLAudioElement | null,
  46. beatMingSongGirlEle: null as HTMLAudioElement | null
  47. },
  48. combineIndex: -1, // 当前 播放的总谱音频索引
  49. combineMusics: {} as Record<string, any>, // 音频 url
  50. combineMusicEles:[] as {key:number, value:HTMLAudioElement, beatValue:HTMLAudioElement|null}[] // 存储的音频el 当大于4个时候删除
  51. });
  52. const midiRef = ref();
  53. /** 播放或暂停 */
  54. export const audioListStart = (type: "play" | "paused") => {
  55. // 开始播放之前, 先设置倍数
  56. if (type === "play" && state.originSpeed !== 0) {
  57. const actualRate = state.originAudioPlayRate * state.basePlayRate;
  58. // console.log('音频播放倍率',actualRate)
  59. setAudioPlaybackRate(actualRate);
  60. }
  61. // console.log('api','midi状态1',type,state.isAppPlay)
  62. // 如果是midi播放
  63. if (state.isAppPlay) {
  64. // handleTogglePlayMidi(type);
  65. cloudToggleState(type);
  66. return;
  67. }
  68. if (type === "play") {
  69. if(state.playSource === "mingSong"){
  70. audioData.mingSongEle?.play();
  71. }else{
  72. audioData.songEle?.play();
  73. audioData.backgroundEle?.play();
  74. }
  75. } else if (type === "paused") {
  76. audioData.songEle?.pause();
  77. audioData.backgroundEle?.pause();
  78. audioData.mingSongEle?.pause();
  79. }
  80. };
  81. /** 设置倍数播放 */
  82. export const setAudioPlaybackRate = (rate: number) => {
  83. // 如果是midi播放
  84. if (state.isAppPlay) {
  85. if (state.modeType === "evaluating") return
  86. hanldeSetMidiPlaybackRate(rate);
  87. return;
  88. }
  89. audioData.songEle && (audioData.songEle.playbackRate = rate);
  90. audioData.backgroundEle && (audioData.backgroundEle.playbackRate = rate);
  91. audioData.mingSongEle && (audioData.mingSongEle.playbackRate = rate);
  92. };
  93. /** 获取当前播放的时间 */
  94. export const getAudioCurrentTime = () => {
  95. // 如果是midi播放,或者是评测
  96. if (state.isAppPlay || state.modeType === 'evaluating') {
  97. // const c = getMidiCurrentTime();
  98. return audioData.progress;
  99. }
  100. if (state.modeType === 'evaluating') {
  101. return audioData.progress;
  102. } else {
  103. // console.log('返回的时间',state.playSource, audioData.songEle?.currentTime,audioData.progress)
  104. if (state.playSource === "music") return audioData.songEle?.currentTime || audioData.progress;
  105. if (state.playSource === "background") return audioData.backgroundEle?.currentTime || audioData.progress;
  106. if (state.playSource === "mingSong") return audioData.mingSongEle?.currentTime || audioData.progress;
  107. return audioData.songEle?.currentTime || audioData.progress;
  108. }
  109. };
  110. /** 获取曲谱的总时间 */
  111. export const getAudioDuration = () => {
  112. // 如果是midi播放
  113. if (state.isAppPlay) {
  114. // const d = getMidiDuration();
  115. const songEndTime = state.times[state.times.length - 1 || 0]?.endtime || 0
  116. return audioData.duration || songEndTime;
  117. }
  118. // 唱名文件时长比较短,和普通文件不一样 所以这里单独处理
  119. if(state.playSource === "mingSong") return audioData.mingSongEle?.duration || audioData.duration;
  120. return audioData.songEle?.duration || audioData.backgroundEle?.duration || audioData.mingSongEle?.duration || audioData.duration;
  121. };
  122. /** 设置播放的开始时间 */
  123. export const setAudioCurrentTime = (time: number, index = 0) => {
  124. // console.log('开始时间12345',time)
  125. // 如果是midi播放
  126. if (state.isAppPlay) {
  127. setMidiCurrentTime(index);
  128. return;
  129. }
  130. if(state.playSource === "mingSong") {
  131. audioData.mingSongEle && (audioData.mingSongEle.currentTime = time);
  132. }
  133. audioData.songEle && (audioData.songEle.currentTime = time);
  134. audioData.backgroundEle && (audioData.backgroundEle.currentTime = time);
  135. audioData.progress = time;
  136. };
  137. /** 设置当前没有播放的音频静音 */
  138. export const toggleMutePlayAudio = (source: IPlayState, muted: boolean) => {
  139. if (source === "music") {
  140. if (audioData.songEle) {
  141. audioData.songEle.muted = muted;
  142. }
  143. } else if (source === "background") {
  144. if (audioData.backgroundEle) {
  145. audioData.backgroundEle.muted = muted;
  146. }
  147. } else if(source === "mingSong"){
  148. if (audioData.mingSongEle) {
  149. audioData.mingSongEle.muted = muted;
  150. }
  151. }
  152. };
  153. /** 切换节拍器音源 */
  154. export const changeSongSourceByBeat = (isDisBeat:boolean) => {
  155. const currentTime = getAudioCurrentTime()
  156. // isDisBeat 为true 切换到不带节拍的,为false 切换到带节拍的
  157. if (isDisBeat) {
  158. if(state.playType === "play"){
  159. audioData.songEle = audioData.songCollection.songEle
  160. audioData.backgroundEle = audioData.songCollection.backgroundEle
  161. } else {
  162. audioData.songEle = audioData.songCollection.fanSongEle
  163. audioData.backgroundEle = audioData.songCollection.banSongEle
  164. audioData.mingSongEle = audioData.songCollection.mingSongEle
  165. }
  166. } else {
  167. // 没有节拍器资源的时候 用 不带节拍器的资源,防止播放不了
  168. if(state.playType === "play"){
  169. audioData.songEle = audioData.songCollection.beatSongEle || audioData.songCollection.songEle
  170. audioData.backgroundEle = audioData.songCollection.beatBackgroundEle || audioData.songCollection.backgroundEle
  171. } else {
  172. audioData.songEle = audioData.songCollection.beatFanSongEle || audioData.songCollection.fanSongEle
  173. audioData.backgroundEle = audioData.songCollection.beatBanSongEle || audioData.songCollection.banSongEle
  174. audioData.mingSongEle = audioData.songCollection.beatMingSongEle || audioData.songCollection.mingSongEle
  175. }
  176. }
  177. // 设置进度
  178. setAudioCurrentTime(currentTime)
  179. // 设置静音与取消静音
  180. if (state.playSource === "music") {
  181. audioData.songEle && (audioData.songEle.muted = false);
  182. audioData.backgroundEle && (audioData.backgroundEle.muted = true);
  183. audioData.mingSongEle && (audioData.mingSongEle.muted = true);
  184. } else if (state.playSource === "background") {
  185. audioData.songEle && (audioData.songEle.muted = true);
  186. audioData.backgroundEle && (audioData.backgroundEle.muted = false);
  187. audioData.mingSongEle && (audioData.mingSongEle.muted = true);
  188. }else {
  189. audioData.songEle && (audioData.songEle.muted = true);
  190. audioData.backgroundEle && (audioData.backgroundEle.muted = true);
  191. audioData.mingSongEle && (audioData.mingSongEle.muted = false);
  192. }
  193. }
  194. /** 切换男声女声唱名 */
  195. export const changeMingSongType = () =>{
  196. // 当有男声女声都有值时候才能切换
  197. const { mingSongEle, mingSongGirlEle, beatMingSongEle, beatMingSongGirlEle } = audioData.mingSongTypeCollection
  198. if(mingSongEle&&mingSongGirlEle){
  199. const mingSongType = audioData.mingSongType
  200. audioData.songCollection.mingSongEle = mingSongType === 1 ? mingSongEle : mingSongGirlEle
  201. audioData.songCollection.beatMingSongEle = mingSongType === 1 ? beatMingSongEle : beatMingSongGirlEle
  202. }
  203. }
  204. const createAudio = (src?: string): Promise<HTMLAudioElement | null> => {
  205. if(!src){
  206. return Promise.resolve(null)
  207. }
  208. return new Promise((resolve) => {
  209. // const a = new Audio(src + '?v=' + Date.now());
  210. const a = new Audio(src);
  211. a.onloadedmetadata = () => {
  212. resolve(a);
  213. };
  214. a.onerror = () => {
  215. resolve(null);
  216. };
  217. // 当未加载 资源之前 切换到其他浏览器标签,浏览器可能会禁止资源加载所以无法触发onloadedmetadata事件,导致一直在加载中,这里做个兼容
  218. if (document.visibilityState === 'visible') {
  219. a.load();
  220. } else {
  221. const onVisibilityChange = () => {
  222. if (document.visibilityState === 'visible') {
  223. document.removeEventListener('visibilitychange', onVisibilityChange);
  224. a.load();
  225. }
  226. };
  227. document.addEventListener('visibilitychange', onVisibilityChange);
  228. }
  229. });
  230. };
  231. // 合成节拍器资源
  232. let CrunkerInstance: Crunker
  233. async function mergeBeatAudio(music?:string){
  234. let beatMusic
  235. if(!state.isMixBeat) {
  236. return beatMusic
  237. }
  238. if(!music){
  239. return beatMusic
  240. }
  241. console.time("音频合成时间")
  242. try{
  243. /* 音频合成 */
  244. if(!CrunkerInstance){
  245. CrunkerInstance = new Crunker()
  246. }
  247. console.time("音频加载时间")
  248. const [musicBuff, tickBuff, tockBuff] = await CrunkerInstance.fetchAudio(music?`${music}`:undefined, tickMp3, tockMp3)
  249. console.timeEnd("音频加载时间")
  250. // 计算音频空白时间
  251. const silenceDuration = musicBuff&&!state.isEvxml ? CrunkerInstance.calculateSilenceDuration(musicBuff) : 0
  252. console.log(`音频空白时间:${silenceDuration}`)
  253. const beats:AudioBuffer[] = []
  254. const beatsTime:number[] = []
  255. metronomeData.metroMeasure.map(measures=>{
  256. measures.map((item:any)=>{
  257. beats.push(item.isTick?tickBuff!:tockBuff!)
  258. beatsTime.push(item.time + silenceDuration) // xml 计算的时候 加上空白的时间
  259. })
  260. })
  261. console.time("音频合并时间")
  262. const musicBuffMeg = musicBuff && CrunkerInstance.mergeAudioBuffers([musicBuff,...beats],[0,...beatsTime])
  263. console.timeEnd("音频合并时间")
  264. console.time("音频audioDom生成时间")
  265. beatMusic = musicBuffMeg && CrunkerInstance.exportAudioElement(musicBuffMeg)
  266. console.timeEnd("音频audioDom生成时间")
  267. }catch(err){
  268. console.log(err)
  269. }
  270. console.timeEnd("音频合成时间")
  271. return beatMusic
  272. }
  273. // 处理加载节拍器音频
  274. export const handleLoadBeatMusic = async () => {
  275. if(metronomeData.disable) {
  276. return
  277. }
  278. const playType = state.playType
  279. const playSource = state.playSource
  280. const mingSongType = audioData.mingSongType
  281. // 当前模式下 如果已经有合成音频了,就不走合成逻辑了
  282. let isBeatMusic = false
  283. let currentMusic //当前的音频
  284. const beatPlayObj = {
  285. "play_music":"beatSongEle",
  286. "play_background":"beatBackgroundEle",
  287. "sing_music":"beatFanSongEle",
  288. "sing_background":"beatBanSongEle"
  289. }
  290. const playObj = {
  291. "play_music":"music",
  292. "play_background":"accompany",
  293. "sing_music":"fanSong",
  294. "sing_background":"banSong",
  295. }
  296. if(playSource === "mingSong") {
  297. // 当男声女声都有的时候 才区分
  298. if(state.mingSong && state.mingSongGirl){
  299. isBeatMusic = mingSongType === 1 ? !!audioData.mingSongTypeCollection.beatMingSongEle : !!audioData.mingSongTypeCollection.beatMingSongGirlEle
  300. currentMusic = mingSongType === 1 ? state.mingSong : state.mingSongGirl
  301. }else{
  302. isBeatMusic = !!audioData.mingSongTypeCollection.beatMingSongEle
  303. currentMusic = state.mingSong
  304. }
  305. }else{
  306. // @ts-ignore
  307. isBeatMusic = !!audioData.songCollection[beatPlayObj[`${playType}_${playSource}`]]
  308. // @ts-ignore
  309. currentMusic = state[playObj[`${playType}_${playSource}`]]
  310. }
  311. if(isBeatMusic || !currentMusic){
  312. return
  313. }
  314. state.loadingText = "资源加载中,请稍后…"
  315. state.isLoading = true
  316. const musicAudio = await mergeBeatAudio(currentMusic) as any
  317. const playEleObj = {
  318. "play_music":"beatSongEle",
  319. "play_background":"beatBackgroundEle",
  320. "sing_music":"beatFanSongEle",
  321. "sing_background":"beatBanSongEle"
  322. }
  323. // 给音频赋值
  324. if(playSource === "mingSong"){
  325. // 当男声女声都有的时候 才区分
  326. if(state.mingSong && state.mingSongGirl){
  327. if(mingSongType === 1) {
  328. audioData.mingSongTypeCollection.beatMingSongEle = musicAudio
  329. }else {
  330. audioData.mingSongTypeCollection.beatMingSongGirlEle = musicAudio
  331. }
  332. }else{
  333. audioData.songCollection.beatMingSongEle = musicAudio
  334. audioData.mingSongTypeCollection.beatMingSongEle = musicAudio
  335. }
  336. if(musicAudio){
  337. musicAudio.addEventListener("play", onPlay);
  338. musicAudio.addEventListener("ended", onEnded);
  339. }
  340. changeMingSongType()
  341. }else{
  342. if(playType === "play" && !audioData.songCollection.beatSongEle && !audioData.songCollection.beatBackgroundEle){
  343. if(musicAudio){
  344. musicAudio.addEventListener("play", onPlay);
  345. musicAudio.addEventListener("ended", onEnded);
  346. }
  347. }
  348. if(playType === "sing" && !audioData.songCollection.beatFanSongEle && !audioData.songCollection.beatBanSongEle){
  349. if(musicAudio){
  350. musicAudio.addEventListener("play", onPlay);
  351. musicAudio.addEventListener("ended", onEnded);
  352. }
  353. }
  354. // @ts-ignore
  355. audioData.songCollection[playEleObj[`${playType}_${playSource}`]] = musicAudio
  356. }
  357. state.isLoading = false
  358. }
  359. // 切换对应的声轨,并且配置当前的audio
  360. export async function changeCombineAudio (combineIndex: number){
  361. // 重复点击的时候取消选中 原音
  362. if(combineIndex === audioData.combineIndex){
  363. audioData.combineIndex = -1
  364. state.playSource = "background"
  365. state.music = ""
  366. // 当没有背景音文件的时候
  367. if(!state.accompany) {
  368. state.noMusicSource = true
  369. }
  370. return
  371. }
  372. state.loadingText = "资源加载中,请稍后…";
  373. state.isLoading = true;
  374. const musicUrl = audioData.combineMusics[combineIndex]
  375. // 有就拿缓存,没有就加载
  376. const cacheMusicIndex = audioData.combineMusicEles.findIndex(ele => {
  377. return ele.key === combineIndex
  378. })
  379. const cacheMusic = audioData.combineMusicEles[cacheMusicIndex]
  380. if(cacheMusic?.value){
  381. audioData.songCollection.songEle = cacheMusic.value
  382. audioData.songCollection.beatSongEle = cacheMusic.beatValue
  383. // 使用缓存之后 当前数据位置向后偏移,删除缓存的时候以使用顺序位置
  384. const itemMusic = audioData.combineMusicEles.splice(cacheMusicIndex, 1)
  385. audioData.combineMusicEles.push(...itemMusic)
  386. }else{
  387. const music = await createAudio(musicUrl)
  388. const beatMusic = await mergeBeatAudio(musicUrl)
  389. // 当没有背景音的时候 需要绑定事件
  390. if(!state.accompany){
  391. if(music){
  392. music.addEventListener("play", onPlay);
  393. music.addEventListener("ended", onEnded);
  394. }
  395. if(beatMusic){
  396. beatMusic.addEventListener("play", onPlay);
  397. beatMusic.addEventListener("ended", onEnded);
  398. }
  399. }
  400. audioData.combineMusicEles.push({
  401. key: combineIndex,
  402. value: music!,
  403. beatValue: beatMusic!
  404. })
  405. // 当大于4个数据的时候 删除掉最前面的数据
  406. if(audioData.combineMusicEles.length > 4){
  407. audioData.combineMusicEles.splice(0,1)
  408. }
  409. audioData.songCollection.songEle = music
  410. audioData.songCollection.beatSongEle = beatMusic!
  411. }
  412. audioData.combineIndex = combineIndex
  413. state.music = musicUrl
  414. state.playSource = "music"
  415. // 当没有背景音文件的时候
  416. if(!state.accompany) {
  417. state.noMusicSource = false
  418. }
  419. showToast({
  420. message: "已开启原声",
  421. position: "top",
  422. className: "selectionToast",
  423. });
  424. state.isLoading = false;
  425. }
  426. export default defineComponent({
  427. name: "audio-list",
  428. setup() {
  429. /** iframe 加载完成后, 加载midiURL */
  430. const handleLoad = () => {
  431. midiRef.value.contentWindow.handleRendered = () => {
  432. audioData.midiRender = true
  433. };
  434. hanldeInitMidiData(midiRef.value);
  435. };
  436. watch(
  437. () => state.playSource,
  438. () => {
  439. if (state.modeType === "evaluating" && !state.setting.enableAccompaniment) {
  440. console.log("评测模式设置了关闭伴奏,不切换原音伴奏");
  441. return;
  442. }
  443. if (state.playSource === "music") {
  444. audioData.songEle && (audioData.songEle.muted = false);
  445. audioData.backgroundEle && (audioData.backgroundEle.muted = true);
  446. audioData.mingSongEle && (audioData.mingSongEle.muted = true);
  447. } else if (state.playSource === "background") {
  448. audioData.songEle && (audioData.songEle.muted = true);
  449. audioData.backgroundEle && (audioData.backgroundEle.muted = false);
  450. audioData.mingSongEle && (audioData.mingSongEle.muted = true);
  451. }else {
  452. audioData.songEle && (audioData.songEle.muted = true);
  453. audioData.backgroundEle && (audioData.backgroundEle.muted = true);
  454. audioData.mingSongEle && (audioData.mingSongEle.muted = false);
  455. }
  456. }
  457. );
  458. /**
  459. * #11046
  460. * 声音与圆点消失的节点不一致,可能原因是部分安卓手机没有立即播放,所以需要等待有音频进度返回时再播放节拍器
  461. * mp3节拍器的圆点动画
  462. */
  463. const tickAnimate = (time: number) => {
  464. if (storeData.isApp && state.modeType === 'evaluating' && evaluatingData.needPlayTick && time > 0) {
  465. evaluatingData.needPlayTick = false;
  466. handleStartTick()
  467. }
  468. }
  469. // 监听评测曲谱音频播放进度,返回
  470. const progress = (res: any) => {
  471. const currentTime = res?.currentTime || res?.content?.currentTime;
  472. const total = res?.totalDuration || res?.content?.totalDuration;
  473. const time = currentTime / 1000;
  474. audioData.progress = time;
  475. tickAnimate(time);
  476. // audioData.songEle && (audioData.songEle.currentTime = time);
  477. // audioData.backgroundEle && (audioData.backgroundEle.currentTime = time);
  478. // audioData.mingSongEle && (audioData.mingSongEle.currentTime = time);
  479. audioData.duration = total / 1000;
  480. if (
  481. res?.content?.totalDuration > 1000 &&
  482. currentTime >= total
  483. ) {
  484. console.log('播放结束1111',evaluatingData.isAudioPlayEnd,currentTime,total)
  485. if (evaluatingData.isAudioPlayEnd) return
  486. evaluatingData.isAudioPlayEnd = true
  487. onEnded();
  488. }
  489. };
  490. // midi播放进度回调
  491. const midiProgress = (res: any) => {
  492. // console.log('api',res)
  493. if (audioData.duration == 0) {
  494. const songEndTime = state.times[state.times.length - 1 || 0]?.endtime || 0
  495. audioData.duration = songEndTime
  496. }
  497. const currentTime = res?.currentTime || res?.content?.currentTime;
  498. const total = res?.totalDuration || res?.content?.totalDuration;
  499. const time = currentTime / 1000;
  500. audioData.progress = time;
  501. // if (
  502. // audioData.duration > 1000 &&
  503. // currentTime >= audioData.duration * 1000
  504. // ) {
  505. // console.log('midi结束')
  506. // onEnded();
  507. // }
  508. // 选段模式,播放结束
  509. if (state.sectionStatus && state.section.length == 2 && currentTime >= state.section) {
  510. //
  511. }
  512. }
  513. // midi播放结束回调
  514. const midiPlayEnd = (res: any) => {
  515. if (res) {
  516. console.log('midi结束')
  517. audioData.progress = 0
  518. onEnded();
  519. }
  520. }
  521. // 加载音源
  522. function loadAudio(){
  523. return Promise.all([createAudio(state.music), createAudio(state.accompany), createAudio(state.fanSong), createAudio(state.banSong), createAudio(state.mingSong), createAudio(state.mingSongGirl)])
  524. }
  525. // 加载节拍器音源
  526. function loadBeatAudio(){
  527. return Promise.all([createAudio(state.beatSong.music), createAudio(state.beatSong.accompany), createAudio(state.beatSong.fanSong), createAudio(state.beatSong.banSong), createAudio(state.beatSong.mingSong), createAudio(state.beatSong.mingSongGirl)])
  528. }
  529. onMounted(async () => {
  530. // 预览的时候不走音频加载逻辑
  531. if(state.isPreView){
  532. state.isLoading = false;
  533. return
  534. }
  535. if (state.playMode !== "MIDI") {
  536. console.time("音频加载耗时")
  537. // 处理音源
  538. const [music, accompany, fanSong, banSong, mingSong, mingSongGirl] = await loadAudio()
  539. audioData.backgroundEle = accompany;
  540. audioData.songEle = music;
  541. Object.assign(audioData.songCollection, {
  542. songEle:music,
  543. backgroundEle:accompany,
  544. fanSongEle:fanSong,
  545. banSongEle:banSong,
  546. mingSongEle:mingSong
  547. })
  548. Object.assign(audioData.mingSongTypeCollection, {
  549. mingSongEle: mingSong,
  550. mingSongGirlEle:mingSongGirl
  551. })
  552. if (music) {
  553. music.addEventListener("play", onPlay);
  554. music.addEventListener("ended", onEnded);
  555. accompany && (accompany.muted = true);
  556. } else if (accompany) {
  557. accompany.addEventListener("play", onPlay);
  558. accompany.addEventListener("ended", onEnded);
  559. }
  560. if(fanSong){
  561. fanSong.addEventListener("play", onPlay);
  562. fanSong.addEventListener("ended", onEnded);
  563. banSong && (banSong.muted = true);
  564. mingSong && (mingSong.muted = true);
  565. }else if(banSong){
  566. banSong.addEventListener("play", onPlay);
  567. banSong.addEventListener("ended", onEnded);
  568. mingSong && (mingSong.muted = true);
  569. }
  570. if(mingSong){
  571. mingSong.addEventListener("play", onPlay);
  572. mingSong.addEventListener("ended", onEnded);
  573. }
  574. if(mingSongGirl){
  575. mingSongGirl.addEventListener("play", onPlay);
  576. mingSongGirl.addEventListener("ended", onEnded);
  577. }
  578. // 处理带节拍器的音源
  579. //const [beatMusic, beatAccompany, beatFanSong, beatBanSong, beatMingSong, beatMingSongGirl] = await loadBeatAudio()
  580. /*// 客户端合成节拍器
  581. const [beatMusic, beatAccompany, beatFanSong, beatBanSong, beatMingSong, beatMingSongGirl] = await mergeBeatAudio(state.music, state.accompany)
  582. Object.assign(audioData.songCollection, {
  583. beatSongEle:beatMusic,
  584. beatBackgroundEle:beatAccompany,
  585. beatFanSongEle:beatFanSong,
  586. beatBanSongEle:beatBanSong,
  587. beatMingSongEle:beatMingSong
  588. })
  589. Object.assign(audioData.mingSongTypeCollection, {
  590. beatMingSongEle: beatMingSong,
  591. beatMingSongGirlEle: beatMingSongGirl
  592. })
  593. if (beatMusic) {
  594. beatMusic.addEventListener("play", onPlay);
  595. beatMusic.addEventListener("ended", onEnded);
  596. beatAccompany && (beatAccompany.muted = true);
  597. } else if (beatAccompany) {
  598. beatAccompany.addEventListener("play", onPlay);
  599. beatAccompany.addEventListener("ended", onEnded);
  600. }
  601. if(beatFanSong){
  602. beatFanSong.addEventListener("play", onPlay);
  603. beatFanSong.addEventListener("ended", onEnded);
  604. beatBanSong && (beatBanSong.muted = true);
  605. beatMingSong && (beatMingSong.muted = true);
  606. }else if(beatBanSong){
  607. beatBanSong.addEventListener("play", onPlay);
  608. beatBanSong.addEventListener("ended", onEnded);
  609. beatMingSong && (beatMingSong.muted = true);
  610. }
  611. if(beatMingSong){
  612. beatMingSong.addEventListener("play", onPlay);
  613. beatMingSong.addEventListener("ended", onEnded);
  614. }
  615. if(beatMingSongGirl){
  616. beatMingSongGirl.addEventListener("play", onPlay);
  617. beatMingSongGirl.addEventListener("ended", onEnded);
  618. } */
  619. // 给男声女声赋值
  620. const userGender = storeData.user.gender
  621. // 当不为null 和undefined的时候 取userGender的值
  622. if(userGender!==undefined && userGender!==null){
  623. audioData.mingSongType = userGender
  624. }
  625. // 重新根据性别赋值
  626. changeMingSongType()
  627. state.audioDone = true;
  628. // state.isLoading = false
  629. console.timeEnd("音频加载耗时")
  630. console.timeLog('加载过程','音频加载完成')
  631. console.log("音频数据:",audioData)
  632. api_playProgress(progress);
  633. } else {
  634. state.audioDone = true;
  635. state.isLoading = false
  636. const songEndTime = state.times[state.times.length - 1 || 0]?.endtime || 0
  637. audioData.duration = songEndTime
  638. // 监听midi播放进度
  639. api_cloudTimeUpdae(midiProgress);
  640. // 监听midi播放结束
  641. api_cloudplayed(midiPlayEnd);
  642. }
  643. console.timeEnd('加载过程')
  644. });
  645. onUnmounted(() => {
  646. api_remove_cloudplayed(midiPlayEnd);
  647. api_remove_cloudTimeUpdae(midiProgress);
  648. });
  649. // console.log(state.playMode, state.midiUrl);
  650. return () => (
  651. <>
  652. <div class={styles.audioList}>
  653. {state.playMode === "MIDI" && state.speed != 0 && (
  654. <iframe
  655. style={{ display: "none" }}
  656. ref={midiRef}
  657. src={`/midi/index.html`}
  658. onLoad={handleLoad}
  659. />
  660. )}
  661. </div>
  662. </>
  663. );
  664. },
  665. });