communication.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. import { storeData } from "../store";
  2. import {
  3. CallBack,
  4. IPostMessage,
  5. listenerMessage,
  6. postMessage,
  7. promisefiyPostMessage,
  8. removeListenerMessage,
  9. } from "../utils/native-message";
  10. /** 获取token */
  11. export const api_getToken = (): Promise<IPostMessage | undefined> => {
  12. if (!storeData.isApp) return Promise.resolve({} as any);
  13. return promisefiyPostMessage({ api: "getToken" });
  14. };
  15. /**获取耳机的插入状态 */
  16. export const getEarphone = (): Promise<IPostMessage | undefined> => {
  17. if (!storeData.isApp) return Promise.resolve({} as any);
  18. return promisefiyPostMessage({ api: "isWiredHeadsetOn" });
  19. };
  20. /** 获取异形屏信息 */
  21. export const isSpecialShapedScreen = (): Promise<IPostMessage | undefined> => {
  22. if (!storeData.isApp) return Promise.resolve({} as any);
  23. return promisefiyPostMessage({ api: "isSpecialShapedScreen" });
  24. };
  25. /** 开始录音 */
  26. export const startSoundCheck = () => {
  27. postMessage({
  28. api: "startSoundCheck",
  29. });
  30. };
  31. /** 录音返回 */
  32. export const sendResult = (callback: CallBack) => {
  33. listenerMessage("sendResult", callback);
  34. };
  35. /** 取消监听录音返回 */
  36. export const removeResult = (callback: CallBack) => {
  37. removeListenerMessage("sendResult", callback);
  38. };
  39. /** 结束录音 */
  40. export const endSoundCheck = () => {
  41. postMessage({
  42. api: "endSoundCheck",
  43. });
  44. };
  45. /** 开始评测 */
  46. export const startEvaluating = (content: any): Promise<IPostMessage | undefined> => {
  47. if (!storeData.isApp) return Promise.resolve({} as any);
  48. return promisefiyPostMessage({ api: "startEvaluating", content: content });
  49. };
  50. /** 结束评测 */
  51. export const endEvaluating = (content: any): Promise<IPostMessage | undefined> => {
  52. if (!storeData.isApp) return Promise.resolve({} as any);
  53. return promisefiyPostMessage({ api: "endEvaluating", content: content });
  54. };
  55. /** 取消评测 */
  56. export const cancelEvaluating = () => {
  57. postMessage({
  58. api: "cancelEvaluating",
  59. });
  60. };
  61. /** 评测开始录音 */
  62. export const api_startRecording = (content: any): Promise<IPostMessage | undefined> => {
  63. if (!storeData.isApp) return Promise.resolve({} as any);
  64. return promisefiyPostMessage({ api: "startRecording", content: content });
  65. };
  66. /** 评测结束录音 */
  67. export const api_stopRecording = (): Promise<IPostMessage | undefined> => {
  68. if (!storeData.isApp) return Promise.resolve({} as any);
  69. return promisefiyPostMessage({ api: "stopRecording" });
  70. };
  71. /** 和websocket通信 */
  72. export const api_proxyServiceMessage = (content: any): Promise<IPostMessage | undefined> => {
  73. if (!storeData.isApp) return Promise.resolve({} as any);
  74. return promisefiyPostMessage({ api: "proxyServiceMessage", content });
  75. };
  76. /** 监听app真正开始录音 */
  77. export const api_recordStartTime = (callback: CallBack) => {
  78. listenerMessage("recordStartTime", callback);
  79. };
  80. /** 卸载监听app真正开始录音 */
  81. export const api_remove_recordStartTime = (callback: CallBack) => {
  82. removeListenerMessage("recordStartTime", callback);
  83. };
  84. /** 上传评测视频 */
  85. export const api_videoUpdate = (callback: CallBack) => {
  86. postMessage(
  87. {
  88. api: "videoUpdate",
  89. },
  90. callback
  91. );
  92. };
  93. /** 分享 */
  94. export const api_shareAchievements = (content: any): Promise<IPostMessage | undefined> => {
  95. if (!storeData.isApp) return Promise.resolve({} as any);
  96. return promisefiyPostMessage({ api: "shareAchievements", content });
  97. };
  98. /** openwebview */
  99. export const api_openWebView = (content: any): Promise<IPostMessage | undefined> => {
  100. if (!storeData.isApp) return Promise.resolve({} as any);
  101. return promisefiyPostMessage({ api: "openWebView", content });
  102. };
  103. /** 开启摄像头 */
  104. export const api_openCamera = (): Promise<IPostMessage | undefined> => {
  105. if (!storeData.isApp) return Promise.resolve({} as any);
  106. return promisefiyPostMessage({ api: "openCamera" });
  107. };
  108. /** 关闭摄像头 */
  109. export const api_closeCamera = (): Promise<IPostMessage | undefined> => {
  110. if (!storeData.isApp) return Promise.resolve({} as any);
  111. return promisefiyPostMessage({ api: "closeCamera" });
  112. };
  113. /** 安卓隐藏状态栏 */
  114. export const api_setStatusBarVisibility = () => {
  115. // 安卓的状态栏
  116. postMessage({
  117. api: "setStatusBarVisibility",
  118. content: {
  119. isVisibility: 0,
  120. },
  121. });
  122. };
  123. /** 跟练录音切换 */
  124. export const api_cloudToggleFollow = (state: "start" | "end") => {
  125. if (!storeData.isApp) return Promise.resolve({} as any);
  126. return promisefiyPostMessage({
  127. api: "cloudToggleFollow",
  128. content: {
  129. state,
  130. },
  131. });
  132. };
  133. /** 跟练录音监听 */
  134. export const api_cloudFollowTime = (onFollowTime: CallBack, listen = true) => {
  135. if (listen) {
  136. listenerMessage("cloudFollowTime", onFollowTime);
  137. } else {
  138. removeListenerMessage("cloudFollowTime", onFollowTime);
  139. }
  140. };
  141. export const api_goback = () => {
  142. postMessage({
  143. api: "goBack",
  144. });
  145. };
  146. /** 结束webview */
  147. export const api_back = () => {
  148. postMessage({
  149. api: "back",
  150. });
  151. };
  152. /** 切换全屏loading */
  153. export const api_cloudLoading = (show = false) => {
  154. postMessage({
  155. api: "cloudLoading",
  156. content: {
  157. show,
  158. type: "fullscreen",
  159. },
  160. });
  161. };
  162. /** 销毁云教练 */
  163. export const api_cloudDestroy = () => {
  164. postMessage({
  165. api: "cloudDestroy",
  166. });
  167. };
  168. /** 事件埋点统计: 酷乐秀用,其它没有用 */
  169. export const api_setEventTracking = () => {
  170. postMessage({
  171. api: "setEventTracking",
  172. content: {
  173. type: "klx_xiaokuAI",
  174. },
  175. });
  176. };
  177. /** 保存图片 */
  178. export const api_savePicture = (content: any): Promise<IPostMessage | undefined> => {
  179. if (!storeData.isApp) return Promise.resolve({} as any);
  180. return promisefiyPostMessage({ api: "savePicture", content });
  181. };
  182. /** 缓存评测分数 */
  183. export const api_setCache = (content: any) => {
  184. postMessage({
  185. api: "setCache",
  186. content,
  187. });
  188. };
  189. /** app切换到后台 */
  190. export const api_suspendPlay = (callback: CallBack) => {
  191. listenerMessage("suspendPlay", callback);
  192. };
  193. /** 开始录制视频 */
  194. export const api_startCapture = () => {
  195. postMessage({
  196. api: "startCapture",
  197. });
  198. };
  199. /** 结束录制视频 */
  200. export const api_endCapture = () => {
  201. postMessage({
  202. api: "endCapture",
  203. });
  204. };
  205. /** 获取设备延迟检测 */
  206. export const api_getDeviceDelay = () => {
  207. if (!storeData.isApp) return Promise.resolve({} as any);
  208. return promisefiyPostMessage({ api: "getDeviceDelay" });
  209. };
  210. /** 延迟检测触发 */
  211. export const api_toggleTune = (state: "start" | "stop" | "finishTune", count?: number) => {
  212. if (!storeData.isApp) return Promise.resolve({} as any);
  213. if (state === "start") {
  214. // 开始效音
  215. return promisefiyPostMessage({
  216. api: "startTune",
  217. content: {
  218. count: count + "",
  219. },
  220. });
  221. } else if (state === "stop") {
  222. // 结束效音,触发时机: 1.监听后台效音返回 2.点击跳过效音或关闭效音
  223. return promisefiyPostMessage({ api: "endTune" });
  224. } else if (state === "finishTune") {
  225. // 效音完成
  226. return promisefiyPostMessage({ api: "finishTune" });
  227. }
  228. };
  229. /** 初始化曲谱音频 和效音音频 */
  230. export const api_createMusicPlayer = (content: any) => {
  231. if (!storeData.isApp) return Promise.resolve({} as any);
  232. return promisefiyPostMessage({ api: "createMusicPlayer", content });
  233. };
  234. /** 进入页面设置常量 */
  235. export const api_keepScreenLongLight = () => {
  236. postMessage({
  237. api: "keepScreenLongLight",
  238. content: {
  239. isOpenLight: true,
  240. },
  241. });
  242. };
  243. /** 设置横竖屏
  244. * 0:横屏
  245. * 1:竖屏
  246. */
  247. export const api_setRequestedOrientation = (orientation: number) => {
  248. postMessage({
  249. api: "setRequestedOrientation",
  250. content: {
  251. orientation,
  252. },
  253. });
  254. };
  255. /** 监听APP播放 */
  256. export const api_playProgress = (callback: any) => {
  257. listenerMessage("playProgress", callback);
  258. };
  259. /** 卸载播放 */
  260. export const api_remove_playProgress = (callback: any) => {
  261. removeListenerMessage("playProgress", callback);
  262. };
  263. /** 合成音频传伴奏 */
  264. export const api_cloudAccompanyMessage = (audioFileUrl: string) => {
  265. postMessage({
  266. api: "cloudAccompanyMessage",
  267. content: {
  268. accompanyUrl: audioFileUrl,
  269. },
  270. });
  271. };
  272. /** 合成音频传伴奏弹窗 */
  273. export const api_openAdjustRecording = (content: any) => {
  274. postMessage({
  275. api: "openAdjustRecording",
  276. content,
  277. });
  278. };
  279. /** 监听评测弹窗是否隐藏保存演奏按钮 */
  280. export const hideComplexButton = (callback: CallBack, listen?: boolean) => {
  281. if (listen) {
  282. listenerMessage("hideComplexButton", callback);
  283. } else {
  284. removeListenerMessage("hideComplexButton", callback);
  285. }
  286. };
  287. /** 评测模式是否开启/关闭伴奏 */
  288. export const api_onoffAccompaniment = () => {
  289. postMessage({
  290. api: "onoffAccompaniment",
  291. content: {
  292. state: 0,
  293. },
  294. });
  295. };
  296. /** 延迟检测校音,校音音频播放完成回调 */
  297. export const addCheckPlayEnd = (callback: CallBack) => {
  298. listenerMessage("checkPlayEnd", callback);
  299. };
  300. /** 取消监听延迟检测校返回 */
  301. export const removeCheckPlayEnd = (callback: CallBack) => {
  302. removeListenerMessage("checkPlayEnd", callback);
  303. };
  304. /** 检查APP端websocket状态 */
  305. export const api_checkSocketStatus = () => {
  306. return promisefiyPostMessage({ api: "checkSocketStatus" });
  307. };
  308. /** 监听APP发送的异常信息 */
  309. export const addAccompanyError = (callback: CallBack) => {
  310. listenerMessage("accompanyError", callback);
  311. };
  312. /** 取消监听APP发送的异常信息 */
  313. export const removeAccompanyError = (callback: CallBack) => {
  314. removeListenerMessage("accompanyError", callback);
  315. };
  316. /** 检测socket链接状态 */
  317. export const addSocketStatus = (callback: CallBack) => {
  318. listenerMessage("socketConnectSuccessReport", callback);
  319. };
  320. /** 取消检测socket链接状态 */
  321. export const removeSocketStatus = (callback: CallBack) => {
  322. removeListenerMessage("socketConnectSuccessReport", callback);
  323. };
  324. /** 检查APP端websocket状态 */
  325. export const api_disconnectSocket = () => {
  326. return promisefiyPostMessage({ api: "disconnectSocket" });
  327. };
  328. // MIDI播放&评测相关的api
  329. /** 发送midi音频等信息 */
  330. export const api_cloudDetail = (content: any, callback: CallBack) => {
  331. postMessage(
  332. {
  333. api: "cloudDetail",
  334. content,
  335. },
  336. callback
  337. );
  338. };
  339. /** 检查midi播放器状态,status: 'init' | 'play' | 'suspend' */
  340. export const api_cloudGetMediaStatus = () => {
  341. return promisefiyPostMessage({ api: "cloudGetMediaStatus" });
  342. };
  343. /** midi开始播放 */
  344. export const api_cloudPlay = (content: any) => {
  345. promisefiyPostMessage({
  346. api: "cloudPlay",
  347. content,
  348. });
  349. };
  350. /** midi暂停播放 */
  351. export const api_cloudSuspend = (content: any) => {
  352. promisefiyPostMessage({
  353. api: "cloudSuspend",
  354. content,
  355. });
  356. };
  357. /** midi跳转到指定位置播放 */
  358. export const api_cloudSetCurrentTime = (content: any) => {
  359. promisefiyPostMessage({
  360. api: "cloudSetCurrentTime",
  361. content,
  362. });
  363. };
  364. /** midi调整播放速度 */
  365. export const api_cloudChangeSpeed = (content: any) => {
  366. promisefiyPostMessage({
  367. api: "cloudChangeSpeed",
  368. content,
  369. });
  370. };
  371. /** midi设置声轨音量 */
  372. export const api_cloudVolume = (content: any) => {
  373. promisefiyPostMessage({
  374. api: "cloudVolume",
  375. content,
  376. });
  377. };
  378. /** midi,播放系统节拍器 */
  379. export const api_cloudMetronome = (content: any, callback: CallBack) => {
  380. postMessage(
  381. {
  382. api: "cloudMetronome",
  383. content,
  384. },
  385. callback
  386. );
  387. };
  388. /** midi练习播放&评测播放回调 */
  389. export const api_cloudTimeUpdae = (callback: any) => {
  390. listenerMessage("cloudTimeUpdae", callback);
  391. };
  392. /** 卸载监听midi播放回调 */
  393. export const api_remove_cloudTimeUpdae = (callback: any) => {
  394. removeListenerMessage("cloudTimeUpdae", callback);
  395. };
  396. /** midi播放结束回调 */
  397. export const api_cloudplayed = (callback: any) => {
  398. listenerMessage("cloudplayed", callback);
  399. };