index.tsx 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868
  1. import styles from "./index.module.less";
  2. import { Snackbar } from "@varlet/ui";
  3. import { closeToast, showLoadingToast, showToast, Popup } from "vant";
  4. import { defineComponent, onMounted, onUnmounted, reactive, ref, watch } from "vue";
  5. import { getLeveByScore, getLeveByScoreMeasure, IEvaluatings } from "./evaluatResult";
  6. import {
  7. cancelEvaluating,
  8. endEvaluating,
  9. endSoundCheck,
  10. getEarphone,
  11. api_proxyServiceMessage,
  12. removeResult,
  13. sendResult,
  14. startEvaluating,
  15. startSoundCheck,
  16. api_openWebView,
  17. api_startRecording,
  18. api_startRecordingCb,
  19. api_stopRecording,
  20. api_recordStartTime,
  21. api_remove_recordStartTime,
  22. api_startCapture,
  23. api_endCapture,
  24. api_closeCamera,
  25. api_getDeviceDelay,
  26. hideComplexButton,
  27. api_checkSocketStatus,
  28. addAccompanyError,
  29. removeAccompanyError,
  30. addSocketStatus,
  31. removeSocketStatus,
  32. api_disconnectSocket,
  33. api_midiMicDelay,
  34. api_cloudSetCurrentTime,
  35. api_cloudChangeSpeed,
  36. api_startDelayCheck,
  37. api_closeDelayCheck,
  38. api_openCamera,
  39. } from "/src/helpers/communication";
  40. import state, { IPlayState, clearSelection, handleStopPlay, onPlay, resetPlaybackToStart, togglePlay, initSetPlayRate, resetBaseRate, scrollViewNote } from "/src/state";
  41. import { IPostMessage } from "/src/utils/native-message";
  42. import { usePageVisibility } from "@vant/use";
  43. import { browser } from "/src/utils";
  44. import { getAudioCurrentTime, toggleMutePlayAudio, audioListStart, audioData } from "../audio-list";
  45. import { handleStartTick, closeTick } from "../tick";
  46. import AbnormalPop from "../abnormal-pop";
  47. import { storeData } from "../../store";
  48. import icon_bg from "../abnormal-pop/icon_bg.svg";
  49. import icon_close from "../abnormal-pop/icon_close.svg";
  50. import icon_btn from "../abnormal-pop/icon_btn.png";
  51. import icon_success from "../abnormal-pop/icon_success.png";
  52. import { data } from "../../page-instrument/custom-plugins/work-index";
  53. import { startCountdown } from "/src/page-instrument/evaluat-model/countdown";
  54. const browserInfo = browser();
  55. let socketStartTime = 0;
  56. export const popImgs = {
  57. icon_bg,
  58. icon_close,
  59. icon_btn,
  60. icon_success,
  61. };
  62. export const evaluatingData = reactive({
  63. /** 评测数据 */
  64. contentData: {} as any,
  65. /** 评测模块是否加载完成 */
  66. rendered: false,
  67. earphone: false, // 是否插入耳机
  68. soundEffect: false, // 是否效音
  69. soundEffectFrequency: 0, // 效音频率
  70. checkStep: 0, // 执行步骤
  71. checkEnd: false, // 检测结束
  72. earphoneMode: false, // 耳机弹窗
  73. earPhoneType: "" as "" | "有线耳机" | "蓝牙耳机", // 耳机类型
  74. soundEffectMode: false, // 效音弹窗
  75. websocketState: false, // websocket连接状态
  76. /**是否开始播放 */
  77. startBegin: false, // 开始
  78. backtime: 0, // 延迟时间
  79. /** 已经评测的数据 */
  80. evaluatings: {} as IEvaluatings,
  81. /** 评测结果 */
  82. resultData: {} as any,
  83. /** 评测结果弹窗 */
  84. resulstMode: false,
  85. /** 是否是完整评测 */
  86. isComplete: false,
  87. /** */
  88. isDisabledPlayMusic: false,
  89. /** socket异常状态弹窗 */
  90. socketErrorPop: false,
  91. /** 异常提示 */
  92. errorContents: '',
  93. /** socket异常状态弹窗的状态值 */
  94. socketErrorStatus: 0,
  95. /** 延迟检测,socket状态异常 */
  96. delayCheckSocketError: false,
  97. /** 异常状态,不生成评测记录,不调用保存接口 */
  98. isErrorState: false,
  99. /** accompanyError,错误类型 */
  100. accompanyErrorType: '',
  101. /** app播放结束状态,重新评测需要重置为 */
  102. isAudioPlayEnd: false,
  103. preloadJson: true, // 预加载延迟检测的资源
  104. jsonLoading: true, // 延迟检测的资源加载中状态
  105. jsonLoadDone: true, // 延迟检测的动画dom加载完成状态
  106. hideResultModal: false, // 评测作业,如果不是完整评测,需要隐藏评测结果弹窗
  107. oneselfCancleEvaluating: false, // 是否是自主取消评测,自主取消评测,不生产评测记录
  108. isBeginMask: false, // 倒计时和系统节拍器时候的遮罩,防止用户点击,
  109. recordingTime: 0, // 调用startRecording的时间
  110. endEvaluatingTime: 0, // 调用endEvaluating的时间
  111. evaluatSpeed: 0, // 评测记录的速度
  112. needReplayEvaluat: false, // 手动取消评测,需要自动开始评测
  113. needPlayTick: false, // 评测时,mp3节拍器需要等待音频开始播放后再执行播放节拍器的圆点动画
  114. tipErjiShow: false, // 评测提示弹窗
  115. onceErjiPopShow: false, // 是否已经提示过耳机弹窗,重新进入评测页面,重置该状态为false,手动关掉耳机弹窗,改变该状态为true,本次评测都不在提示耳机状态弹窗
  116. needCheckErjiStatus: true, // 点击评测模式进入评测模块的需要检测耳机状态,通过返回按钮进入评测模块的,不检测耳机状态
  117. evaluatResultLoading: false, // 评测结果处理中
  118. });
  119. const sendOffsetTime = async (offsetTime: number) => {
  120. const delayData = await api_getDeviceDelay();
  121. api_midiMicDelay({
  122. header: {
  123. commond: "audioPlayStart",
  124. type: "SOUND_COMPARE",
  125. },
  126. body: {
  127. offsetTime,
  128. micDelay: delayData?.content?.value,
  129. },
  130. });
  131. };
  132. /** 点击开始评测按钮 */
  133. export const handleStartEvaluat = async () => {
  134. if (state.modeType === "evaluating") {
  135. handleCancelEvaluat();
  136. // 放下面会在异步之后执行 旋律线可能在会隐藏不了
  137. state.modeType = "practise";
  138. } else {
  139. // 放下面会在异步之后执行 旋律线可能在会隐藏不了
  140. state.modeType = "evaluating";
  141. if (state.platform !== "PC") {
  142. // 评测前先检查APP端的websocket状态
  143. const res = await api_checkSocketStatus();
  144. if (res?.content?.status === "connected") {
  145. handleStopPlay();
  146. } else {
  147. // socket未连接
  148. // evaluatingData.socketErrorPop = true
  149. }
  150. } else {
  151. handleStopPlay();
  152. }
  153. }
  154. //state.modeType = state.modeType === "evaluating" ? "practise" : "evaluating";
  155. if (state.modeType !== "evaluating") {
  156. // 切换到练习模式,卸载评测模块
  157. evaluatingData.rendered = false;
  158. }
  159. };
  160. /** 开始评测 & 延迟检测开始按钮 */
  161. export const startCheckDelay = async () => {
  162. // 评测前先检查APP端的websocket状态
  163. const res = await api_checkSocketStatus();
  164. if (res?.content?.status === "connected") {
  165. //
  166. return new Promise((resolve) => {
  167. resolve({ checked: true });
  168. });
  169. } else {
  170. /**
  171. * socket未连接,记录此时的时间,以便于和收到socket成功链接,进行对比,对比时间小于500ms时,则连接中的状态默认显示500ms持续时间
  172. *
  173. * */
  174. socketStartTime = +new Date();
  175. evaluatingData.socketErrorPop = true;
  176. evaluatingData.socketErrorStatus = 1;
  177. return new Promise((resolve) => {
  178. resolve({ checked: false });
  179. });
  180. }
  181. };
  182. const check_currentTime = () => {
  183. let preTime = 0;
  184. // 选段评测模式
  185. if (state.isSelectMeasureMode) {
  186. preTime = state.section[0].time * 1000;
  187. }
  188. const currentTime = getAudioCurrentTime() * 1000 - preTime;
  189. // console.log('播放进度music', currentTime, 'preTime:' + preTime)
  190. if (currentTime >= 500) {
  191. sendEvaluatingOffsetTime(500);
  192. return;
  193. }
  194. setTimeout(() => {
  195. check_currentTime();
  196. }, 10);
  197. };
  198. /** 开始播放发送延迟时间 */
  199. export const sendEvaluatingOffsetTime = async (currentTime: number) => {
  200. // 没有开始时间点, 不处理
  201. if (!evaluatingData.backtime) return;
  202. const nowTime = Date.now();
  203. const delayTime = nowTime - evaluatingData.backtime - currentTime;
  204. console.error("真正播放延迟", delayTime, "currentTime:", currentTime);
  205. await api_proxyServiceMessage({
  206. header: {
  207. commond: "audioPlayStart",
  208. type: "SOUND_COMPARE",
  209. },
  210. body: {
  211. offsetTime: delayTime < 0 ? 0 : delayTime,
  212. micDelay: 0,
  213. },
  214. });
  215. };
  216. /** 检测耳机 */
  217. export const checkUseEarphone = async () => {
  218. const res = await getEarphone();
  219. return res?.content?.checkIsWired || false;
  220. };
  221. /**
  222. * 开始录音
  223. */
  224. const handleStartSoundCheck = () => {
  225. startSoundCheck();
  226. };
  227. /** 结束录音 */
  228. export const handleEndSoundCheck = () => {
  229. endSoundCheck();
  230. };
  231. /** 连接websocket */
  232. export const connectWebsocket = async (content: any) => {
  233. evaluatingData.contentData = content;
  234. evaluatingData.websocketState = true;
  235. };
  236. /**
  237. * 执行检测
  238. */
  239. export const handlePerformDetection = async () => {
  240. // 检测完成不检测了
  241. if (evaluatingData.checkEnd) return;
  242. // 延迟检测
  243. if (evaluatingData.checkStep === 0) {
  244. evaluatingData.checkStep = 5;
  245. // 没有设备延迟数据 或 开启了效音 显示检测组件,并持续检测耳机状态
  246. if (state.setting.soundEffect) {
  247. evaluatingData.soundEffectMode = true;
  248. return;
  249. }
  250. const delayTime = await api_getDeviceDelay();
  251. console.log("🚀 ~ delayTime:", delayTime);
  252. if (!delayTime) {
  253. evaluatingData.soundEffectMode = true;
  254. return;
  255. }
  256. handlePerformDetection();
  257. return;
  258. }
  259. // 检测耳机
  260. if ((evaluatingData.checkStep = 5)) {
  261. evaluatingData.checkStep = 10;
  262. const erji = await checkUseEarphone();
  263. if (!erji) {
  264. evaluatingData.earphoneMode = true;
  265. return;
  266. }
  267. handlePerformDetection();
  268. return;
  269. }
  270. // 效音
  271. // if (evaluatingData.checkStep === 7) {
  272. // // 是否需要开启效音
  273. // evaluatingData.checkStep = 10;
  274. // if (state.setting.soundEffect && !state.isPercussion) {
  275. // evaluatingData.soundEffectMode = true;
  276. // handleStartSoundCheck();
  277. // return
  278. // }
  279. // handlePerformDetection();
  280. // return;
  281. // }
  282. // 效验完成
  283. if (evaluatingData.checkStep === 10) {
  284. evaluatingData.checkEnd = true;
  285. }
  286. };
  287. /** 记录小节分数 */
  288. export const addMeasureScore = (measureScore: any, show = true) => {
  289. // #8720 bug修复
  290. for (let idx in evaluatingData.evaluatings) {
  291. evaluatingData.evaluatings[idx].show = false;
  292. }
  293. evaluatingData.evaluatings[measureScore.measureRenderIndex] = {
  294. ...measureScore,
  295. leve: getLeveByScoreMeasure(measureScore.score),
  296. show,
  297. };
  298. // console.log("🚀 ~ measureScore:", evaluatingData.evaluatings)
  299. };
  300. const handleScoreResult = (res?: IPostMessage) => {
  301. console.log("返回", res, evaluatingData.oneselfCancleEvaluating);
  302. // 如果是手动取消评测,不生成评测记录
  303. // if (evaluatingData.oneselfCancleEvaluating) {
  304. // return;
  305. // }
  306. if (res?.content) {
  307. const { header, body } = res.content;
  308. // 效音返回
  309. if (header.commond === "checking") {
  310. evaluatingData.soundEffectFrequency = body.frequency;
  311. }
  312. // 小节评分返回
  313. if (header?.commond === "measureScore" && !evaluatingData.oneselfCancleEvaluating) {
  314. console.log("🚀 ~ 评测返回:", res);
  315. addMeasureScore(body);
  316. }
  317. // 评测结束返回
  318. if (header?.commond === "overall") {
  319. console.log("🚀 ~ 评测返回:", res);
  320. console.log("评测结束", body);
  321. state.isHideEvaluatReportSaveBtn = false;
  322. setTimeout(() => {
  323. // 评测作业,如果不是完整评测,不展示评测弹窗
  324. if (data.trainingType === "EVALUATION" && !evaluatingData.isComplete) {
  325. evaluatingData.hideResultModal = true;
  326. } else {
  327. evaluatingData.hideResultModal = false;
  328. }
  329. // 手动取消评测,不展示评测弹窗
  330. if (evaluatingData.oneselfCancleEvaluating) {
  331. evaluatingData.hideResultModal = true;
  332. }
  333. evaluatingData.resulstMode = evaluatingData.isErrorState ? false : true;
  334. evaluatingData.startBegin = false;
  335. evaluatingData.evaluatResultLoading = false;
  336. }, 200);
  337. evaluatingData.resultData = {
  338. ...body,
  339. ...getLeveByScore(body.score),
  340. };
  341. // console.log("🚀 ~ evaluatingData.resultData:", evaluatingData.resultData)
  342. closeToast();
  343. state.isLoading = false
  344. }
  345. }
  346. };
  347. /** 开始评测 */
  348. export const handleStartBegin = async (preTimes?: number) => {
  349. // 滚动到当前小节所在区域
  350. scrollViewNote(true);
  351. evaluatingData.needPlayTick = false;
  352. if (state.isAppPlay) {
  353. await api_cloudSetCurrentTime({
  354. currentTime: 0,
  355. songID: state.examSongId,
  356. })
  357. }
  358. evaluatingData.isComplete = false;
  359. evaluatingData.evaluatings = {};
  360. evaluatingData.resultData = {};
  361. evaluatingData.backtime = 0;
  362. evaluatingData.isAudioPlayEnd = false;
  363. const res = await startEvaluating(evaluatingData.contentData);
  364. if (res?.api !== "startEvaluating") {
  365. Snackbar.error("请在APP端进行评测");
  366. evaluatingData.startBegin = false;
  367. return;
  368. }
  369. if (res?.content?.reson) {
  370. // #10984,APP端会有弹窗提示,不需要再次提示
  371. // showToast(res.content?.des);
  372. evaluatingData.startBegin = false;
  373. return;
  374. }
  375. resetPlaybackToStart();
  376. evaluatingData.startBegin = true;
  377. if (evaluatingData.isDisabledPlayMusic) {
  378. evaluatingData.isBeginMask = true
  379. // 先播放倒计时
  380. await startCountdown()
  381. state.playState = state.playState === "paused" ? "play" : "paused";
  382. // 设置为开始播放时, 如果需要节拍,先播放节拍器
  383. if (state.playState === "play" && (state.playType==="play"&&state.needTick)||(state.playType==="sing"&&state.needSingTick)) {
  384. // 如果是系统节拍器 等系统节拍器播完了再播,如果是mp3节拍器 直接播
  385. if((state.playType==="play" && !state.isOpenMetronome)||(state.playType==="sing" && !state.isSingOpenMetronome)){
  386. const tickend = await handleStartTick();
  387. console.log("🚀 ~ tickend:", tickend)
  388. // 节拍器返回false, 取消播放
  389. if (!tickend) {
  390. state.playState = "paused";
  391. evaluatingData.startBegin = false;
  392. evaluatingData.isBeginMask = false
  393. return;
  394. }
  395. }else{
  396. // handleStartTick()
  397. // 需要等待音频返回进度后再执行节拍器圆点动画
  398. evaluatingData.needPlayTick = true;
  399. }
  400. }
  401. evaluatingData.isBeginMask = false
  402. onPlay();
  403. }
  404. if (evaluatingData.isErrorState) {
  405. state.playState = 'paused';
  406. evaluatingData.startBegin = false;
  407. return
  408. }
  409. //开始录音
  410. // await api_startRecording({
  411. // accompanimentState: state.setting.enableAccompaniment ? 1 : 0,
  412. // firstNoteTime: preTimes || 0,
  413. // });
  414. const rate = state.basePlayRate * state.originAudioPlayRate; // 播放倍率
  415. await api_startRecordingCb({
  416. // accompanimentState: state.setting.enableAccompaniment ? 1 : 0,
  417. accompanimentState: !state.accompany ? 0 : 1, // 评测没有伴奏时,静音播放
  418. firstNoteTime: preTimes || 0,
  419. speedRate: parseFloat(rate.toFixed(2)), // 播放倍率
  420. }, () => {
  421. if (state.isAppPlay) {
  422. setTimeout(() => {
  423. sendOffsetTime(0)
  424. }, 300);
  425. }
  426. })
  427. /**
  428. * 安卓端,快速点击开始评测和结束评测,间隔太小了时,会出现异常情况(音频没有停止播放)
  429. * 记录一下startRecord的开始时间
  430. */
  431. evaluatingData.recordingTime = +new Date();
  432. // 如果开启了摄像头, 开启录制视频
  433. if (state.setting.camera) {
  434. console.log("开始录制视频");
  435. await api_startCapture();
  436. }
  437. // 如果是midi音频评测,需要调用cloudPlay
  438. if (state.isAppPlay) {
  439. await api_cloudChangeSpeed({
  440. speed: state.originSpeed,
  441. originalSpeed: state.originSpeed,
  442. songID: state.examSongId,
  443. });
  444. audioData.progress = 0
  445. audioListStart(state.playState);
  446. }
  447. evaluatingData.oneselfCancleEvaluating = false;
  448. };
  449. /** 播放音乐 */
  450. const playMusic = async () => {
  451. const playState = await togglePlay("play");
  452. // 取消播放,停止播放
  453. if (!playState) {
  454. evaluatingData.startBegin = false;
  455. handleCancelEvaluat();
  456. return;
  457. }
  458. // 检测播放进度, 计算延迟
  459. check_currentTime();
  460. // 如果开启了摄像头, 开启录制视频
  461. if (state.setting.camera) {
  462. console.log("开始录制视频");
  463. api_startCapture();
  464. }
  465. };
  466. let _audio: HTMLAudioElement;
  467. /** 录音开始,记录开始时间点 */
  468. const recordStartTimePoint = async (res?: IPostMessage) => {
  469. console.error("开始录音");
  470. // 没有开始评测,不处理
  471. if (!evaluatingData.startBegin) return;
  472. let inteveral = res?.content?.inteveral || 0;
  473. if (browserInfo.ios) {
  474. inteveral *= 1000;
  475. }
  476. evaluatingData.backtime = inteveral || Date.now();
  477. console.log("🚀 ~ 开始时间点:", evaluatingData.backtime, "已经录的时间:", Date.now() - inteveral, "记录时间点:", Date.now());
  478. // 是否禁播
  479. if (evaluatingData.isDisabledPlayMusic) {
  480. return;
  481. }
  482. // 开始播放
  483. playMusic();
  484. };
  485. /**
  486. * 结束评测
  487. * @param isComplete 是否完整评测
  488. * @param endType 结束类型,selfCancel:是否是自己取消本次评测
  489. * @returns
  490. */
  491. export const handleEndEvaluat = (isComplete = false, endType?: string) => {
  492. // 没有开始评测 , 不是评测模式 , 不评分;evaluatResultLoading:评测结果处理中,避免重复结束
  493. if (!evaluatingData.startBegin || state.modeType !== "evaluating" || evaluatingData.evaluatResultLoading) return;
  494. // 结束录音
  495. // api_stopRecording();
  496. // 结束评测
  497. evaluatingData.evaluatResultLoading = true
  498. console.log("评测结束1");
  499. endEvaluating({
  500. musicScoreId: state.examSongId,
  501. });
  502. // 评测作业如果不是完整评测,给出提示
  503. if (!isComplete && data.trainingType === "EVALUATION") {
  504. showToast({
  505. message: "完整演奏结束才算作业分数!",
  506. });
  507. } else {
  508. if (!endType) {
  509. state.loadingText = "正在评分中,请稍等..."
  510. state.isLoading = true
  511. // showLoadingToast({
  512. // message: "评分中",
  513. // duration: 0,
  514. // overlay: true,
  515. // overlayClass: styles.scoreMode,
  516. // });
  517. }
  518. }
  519. setTimeout(() => {
  520. // evaluatingData.startBegin = false;
  521. if (endType === 'selfCancel') {
  522. // 重置播放倍率
  523. const item: any = (state.sectionStatus && state.section.length === 2) ? state.sectionFirst || state.section[0] : state.times[0];
  524. state.activeNoteIndex = item.i;
  525. state.activeMeasureIndex = item.MeasureNumberXML;
  526. resetBaseRate(item.i);
  527. }
  528. }, 500);
  529. evaluatingData.isComplete = isComplete;
  530. // 如果开启了摄像头, 结束录制视频
  531. if (state.setting.camera) {
  532. console.log("结束录制视频1");
  533. api_endCapture();
  534. }
  535. };
  536. // 校验评测结束最小的时间间隔
  537. export const checkMinInterval = () => {
  538. const currentTime = +new Date();
  539. // 开始评测和结束评测的间隔时间小于800毫秒,则不处理
  540. if (currentTime - evaluatingData.recordingTime < 800) {
  541. return;
  542. }
  543. }
  544. /**
  545. * 结束评测(手动结束评测)
  546. */
  547. export const handleEndBegin = () => {
  548. handleEndEvaluat();
  549. handleStopPlay();
  550. };
  551. /**
  552. * 取消评测
  553. */
  554. export const handleCancelEvaluat = (cancelType?: string) => {
  555. evaluatingData.evaluatings = {};
  556. evaluatingData.startBegin = false;
  557. // 关闭提示
  558. closeToast();
  559. // 取消记录
  560. api_proxyServiceMessage({
  561. header: {
  562. commond: "recordCancel",
  563. type: "SOUND_COMPARE",
  564. status: 200,
  565. },
  566. });
  567. /**
  568. * 异常状态是取消评测(cancelEvaluating),正常结束时结束评测(endEvaluating)
  569. */
  570. // if (cancelType === "cancel") {
  571. // // 取消评测
  572. // cancelEvaluating();
  573. // } else {
  574. // endEvaluating({
  575. // musicScoreId: state.examSongId,
  576. // });
  577. // }
  578. cancelEvaluating();
  579. // 停止播放
  580. handleStopPlay();
  581. console.log("评测结束2");
  582. endEvaluating({
  583. musicScoreId: state.examSongId,
  584. });
  585. // 如果开启了摄像头, 结束录制视频
  586. if (state.setting.camera) {
  587. console.log("结束录制视频2");
  588. api_endCapture();
  589. api_closeCamera();
  590. }
  591. };
  592. /** 查看报告 */
  593. export const handleViewReport = (key: "recordId" | "recordIdStr", type: "gym" | "colexiu" | "orchestra" | "instrument") => {
  594. const id = evaluatingData.resultData?.[key] || "";
  595. let url = "";
  596. switch (type) {
  597. case "gym":
  598. url = location.origin + location.pathname + "#/report/" + id;
  599. break;
  600. case "orchestra":
  601. url = location.origin + location.pathname + "report-share.html?id=" + id;
  602. break;
  603. case "instrument":
  604. url = location.origin + location.pathname + "#/evaluat-report?id=" + id + "&musicRenderType=" + state.musicRenderType + "&systemType=" + state.systemType;
  605. if (state.isSchool) {
  606. url += `&school=1`;
  607. }
  608. break;
  609. default:
  610. url = location.origin + location.pathname + "report-share.html?id=" + id;
  611. break;
  612. }
  613. api_openWebView({
  614. url,
  615. orientation: 0,
  616. isHideTitle: true, // 此处兼容安卓,意思为隐藏全部头部
  617. statusBarTextColor: false,
  618. isOpenLight: true,
  619. c_orientation: 0,
  620. showLoadingAnim: true
  621. });
  622. };
  623. // 隐藏存演奏按钮
  624. const handleComplexButton = (res?: IPostMessage) => {
  625. console.log("监听是否隐藏保存按钮", res);
  626. if (res?.content) {
  627. const { header, body } = res.content;
  628. state.isHideEvaluatReportSaveBtn = true;
  629. }
  630. };
  631. // 检测到APP发送的异常信息
  632. const handleAccompanyError = (res?: IPostMessage) => {
  633. console.log("异常信息返回", res);
  634. if (res?.content) {
  635. const { type, reson } = res.content;
  636. state.playState = 'paused'
  637. switch (type) {
  638. case "enterBackground":
  639. // App退到后台
  640. case "playError":
  641. // 播放异常
  642. case "socketError":
  643. // socket连接断开,评测中,则取消评测
  644. // 延迟检测中
  645. if (evaluatingData.soundEffectMode) {
  646. evaluatingData.socketErrorStatus = 0;
  647. evaluatingData.delayCheckSocketError = true;
  648. evaluatingData.socketErrorPop = type === "socketError" ? true : false;
  649. evaluatingData.accompanyErrorType = type;
  650. // api_checkSocketStatus()
  651. return;
  652. }
  653. // 评测中
  654. if (state.modeType === "evaluating" && evaluatingData.startBegin) {
  655. handleCancelEvaluat("cancel");
  656. }
  657. // 关闭节拍器
  658. closeTick();
  659. // socketerrror,才发送关闭延迟检测的消息
  660. if (type === "socketError") {
  661. api_closeDelayCheck({});
  662. }
  663. evaluatingData.socketErrorStatus = 0;
  664. evaluatingData.socketErrorPop = type === "socketError" ? true : false;
  665. evaluatingData.isErrorState = true;
  666. evaluatingData.accompanyErrorType = type;
  667. resetPlaybackToStart();
  668. break;
  669. case "recordError":
  670. // 录音异常
  671. break;
  672. default:
  673. break;
  674. }
  675. }
  676. };
  677. // 监测socket状态,是否已经成功连接
  678. const handleSocketStatus = (res?: IPostMessage) => {
  679. if (res?.content?.status === "connected") {
  680. const currentTime = +new Date();
  681. evaluatingData.delayCheckSocketError = false;
  682. const diffTime = currentTime - socketStartTime;
  683. if (diffTime < 1000) {
  684. const remainingTime = 1000 - diffTime;
  685. setTimeout(() => {
  686. evaluatingData.socketErrorStatus = 2;
  687. }, remainingTime);
  688. }
  689. }
  690. };
  691. // 评测出现异常,再试一次
  692. export const hanldeConfirmPop = async () => {
  693. api_checkSocketStatus();
  694. evaluatingData.socketErrorStatus = 1;
  695. socketStartTime = +new Date();
  696. };
  697. // 关闭异常弹窗
  698. export const hanldeClosePop = () => {
  699. evaluatingData.socketErrorPop = false;
  700. evaluatingData.socketErrorStatus = 0;
  701. };
  702. export default defineComponent({
  703. name: "evaluating",
  704. setup() {
  705. const pageVisibility = usePageVisibility();
  706. // 需要记录的数据
  707. const record_old_data = reactive({
  708. /** 指法 */
  709. finger: false,
  710. /** 原音伴奏 */
  711. play_mode: "" as IPlayState,
  712. /** 评测是否要伴奏 */
  713. enableAccompaniment: true,
  714. });
  715. /** 记录状态 */
  716. const hanlde_record = () => {
  717. // 取消指法
  718. // record_old_data.finger = state.setting.displayFingering;
  719. // state.setting.displayFingering = false;
  720. // 切换为伴奏
  721. record_old_data.play_mode = state.playSource;
  722. record_old_data.enableAccompaniment = state.setting.enableAccompaniment;
  723. // 如果关闭伴奏,评测静音
  724. if (!record_old_data.enableAccompaniment) {
  725. console.log("关闭伴奏");
  726. toggleMutePlayAudio(record_old_data.play_mode === "music" ? "music" : record_old_data.play_mode === "background" ? "background" : "mingSong", true);
  727. }
  728. };
  729. /** 还原状态 */
  730. const handle_reduction = () => {
  731. // 还原指法
  732. // state.setting.displayFingering = record_old_data.finger;
  733. state.playSource = record_old_data.play_mode;
  734. // 如果关闭伴奏, 结束评测取消静音
  735. if (!record_old_data.enableAccompaniment) {
  736. toggleMutePlayAudio(record_old_data.play_mode === "music" ? "music" : record_old_data.play_mode === "background" ? "background" : "mingSong", false);
  737. }
  738. };
  739. // 打开摄像头
  740. const openSetCamera = async () => {
  741. if (state.setting.camera) {
  742. const res = await api_openCamera();
  743. // 没有授权
  744. if (res?.content?.reson) {
  745. state.setting.camera = false;
  746. store.set("musicscoresetting", state.setting);
  747. }
  748. }
  749. }
  750. watch(pageVisibility, (value) => {
  751. if (value == "hidden" && evaluatingData.startBegin) {
  752. // handleEndBegin();
  753. }
  754. });
  755. watch(
  756. () => evaluatingData.socketErrorStatus,
  757. () => {
  758. if (evaluatingData.socketErrorStatus === 2) {
  759. setTimeout(() => {
  760. evaluatingData.socketErrorPop = false;
  761. // evaluatingData.socketErrorStatus = 0
  762. }, 1000);
  763. }
  764. }
  765. );
  766. watch(
  767. () => evaluatingData.socketErrorPop,
  768. () => {
  769. if (evaluatingData.socketErrorPop && state.setting.soundEffect) {
  770. // 监听到socket状态异常,需要关闭延迟检测
  771. api_closeDelayCheck({});
  772. }
  773. }
  774. );
  775. onMounted(() => {
  776. openSetCamera();
  777. resetPlaybackToStart();
  778. hanlde_record();
  779. evaluatingData.resultData = {};
  780. // evaluatingData.resulstMode = true;
  781. // evaluatingData.resultData = {...getLeveByScore(10), score: 10, intonation: 10, cadence: 30, integrity: 40}
  782. // console.log("🚀 ~ evaluatingData.resultData:", evaluatingData.resultData)
  783. evaluatingData.onceErjiPopShow = false;
  784. evaluatingData.evaluatings = {};
  785. evaluatingData.soundEffectFrequency = 0;
  786. evaluatingData.checkStep = 0;
  787. evaluatingData.rendered = true;
  788. sendResult(handleScoreResult);
  789. hideComplexButton(handleComplexButton, true);
  790. api_recordStartTime(recordStartTimePoint);
  791. addAccompanyError(handleAccompanyError);
  792. addSocketStatus(handleSocketStatus);
  793. // 不是选段模式评测, 就清空已选段
  794. if (!state.isSelectMeasureMode) {
  795. clearSelection();
  796. }
  797. console.log("加载评测模块成功");
  798. });
  799. onUnmounted(() => {
  800. evaluatingData.checkEnd = false;
  801. evaluatingData.rendered = false;
  802. resetPlaybackToStart();
  803. removeResult(handleScoreResult);
  804. hideComplexButton(() => {}, false);
  805. api_remove_recordStartTime(recordStartTimePoint);
  806. handle_reduction();
  807. removeAccompanyError(handleAccompanyError);
  808. if (evaluatingData.socketErrorPop && state.setting.soundEffect) {
  809. console.log('延迟检测出错')
  810. } else {
  811. removeSocketStatus(handleSocketStatus);
  812. }
  813. api_disconnectSocket();
  814. console.log("卸载评测模块成功");
  815. });
  816. return () => (
  817. <div>
  818. {/** 预加载一下断网需要用到的图片 */}
  819. <div class={styles.hiddenPop}>
  820. <img src={popImgs.icon_bg} />
  821. <img src={popImgs.icon_btn} />
  822. <img src={popImgs.icon_success} />
  823. <img src={popImgs.icon_close} />
  824. </div>
  825. <Popup teleport="body" closeOnClickOverlay={false} class={["popup-custom", "van-scale", evaluatingData.socketErrorStatus === 2 && styles.socketErrorStatus]} transition="van-scale" v-model:show={evaluatingData.socketErrorPop} overlay-style={evaluatingData.socketErrorStatus === 2?{ background: "initial" }:{}}>
  826. <AbnormalPop onConfirm={hanldeConfirmPop} onClose={hanldeClosePop} />
  827. </Popup>
  828. </div>
  829. );
  830. },
  831. });