PageMusic.tsx 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. import { IconMusic } from "@/assets/icons";
  2. import { isWeixinBrowser } from "@/controllers/wxController";
  3. import { useEditor } from "@/modules/editor";
  4. import {
  5. PauseCircleOutlined,
  6. PlayCircleOutlined,
  7. SoundOutlined,
  8. } from "@ant-design/icons-vue";
  9. import { css } from "@linaria/core";
  10. import { Button, Dropdown, Slider } from "ant-design-vue";
  11. import { Howl } from "howler";
  12. import { nanoid } from "nanoid";
  13. import {
  14. defineComponent,
  15. reactive,
  16. ref,
  17. watch,
  18. onUnmounted,
  19. onMounted,
  20. } from "vue";
  21. import { bool, number } from "vue-types";
  22. import { MusicOptions } from "./localMusic";
  23. declare const WeixinJSBridge: any;
  24. export const PageMusic = defineComponent({
  25. setup() {
  26. const { store, helper, controls, actions } = useEditor();
  27. const rootComp = helper.findRootComp();
  28. const volume =
  29. rootComp?.value.volume != undefined ? rootComp?.value.volume : 0.3;
  30. const state = reactive({
  31. playStatus: false,
  32. duration: 0,
  33. currentTime: 0,
  34. muted: true,
  35. volume: volume,
  36. });
  37. let audioKey = nanoid();
  38. let audioBgm = ref();
  39. const initAudioBgm = () => {
  40. audioBgm.value = null;
  41. const curAudio = MusicOptions.find((e) => {
  42. return e.value == rootComp?.value.music;
  43. });
  44. const src = curAudio?.src || "";
  45. audioBgm.value = new Howl({
  46. src: [src],
  47. loop: store.isEditMode ? false : true,
  48. preload: true,
  49. HTML5: true,
  50. volume: volume,
  51. });
  52. controls.mediaCtrl.setMediasInstance(audioKey, audioBgm.value);
  53. audioBgm.value.on("load", () => {
  54. state.duration = audioBgm.value.duration();
  55. if (!store.isEditMode) {
  56. if (isWeixinBrowser()) {
  57. WeixinJSBridge.invoke(
  58. "getNetworkType",
  59. {},
  60. () => {
  61. playAudio(true);
  62. },
  63. false
  64. );
  65. } else {
  66. playAudio(true);
  67. }
  68. }
  69. });
  70. audioBgm.value.on("loaderror", () => {
  71. console.log("音频加载失败");
  72. });
  73. audioBgm.value.on("playerror", () => {
  74. console.log("音频播放失败");
  75. });
  76. audioBgm.value.on("play", () => {
  77. controls.mediaCtrl.pauseOtherMedia(audioKey);
  78. if (!state.playStatus) {
  79. state.playStatus = true;
  80. }
  81. playStep();
  82. });
  83. audioBgm.value.on("pause", () => {
  84. audioRest();
  85. });
  86. audioBgm.value.on("end", () => {
  87. if (store.isEditMode) {
  88. audioRest();
  89. }
  90. });
  91. setTimeout(() => {
  92. checkAutoPlay();
  93. }, 500);
  94. };
  95. const checkAutoPlay = () => {
  96. const duration = audioBgm.value.duration();
  97. if (duration) {
  98. state.duration = duration;
  99. }
  100. if (!audioBgm.value || store.isEditMode) {
  101. return;
  102. }
  103. let playing = audioBgm.value.playing();
  104. if (!playing) {
  105. playAudio(true);
  106. }
  107. };
  108. const playStep = () => {
  109. if (!audioBgm.value) {
  110. return;
  111. }
  112. let playing = audioBgm.value.playing();
  113. if (!playing) {
  114. return;
  115. }
  116. let seek = audioBgm.value.seek();
  117. state.currentTime = seek;
  118. requestAnimationFrame(playStep);
  119. };
  120. const playAudio = async (status: boolean) => {
  121. if (!audioBgm.value) {
  122. return;
  123. }
  124. if (status) {
  125. audioBgm.value.play();
  126. } else {
  127. audioRest();
  128. }
  129. let playing = audioBgm.value.playing();
  130. if (status && playing) {
  131. state.playStatus = true;
  132. return;
  133. }
  134. state.playStatus = false;
  135. };
  136. watch(
  137. () => rootComp?.value.music,
  138. () => {
  139. audioRest();
  140. initAudioBgm();
  141. }
  142. );
  143. onMounted(() => {
  144. initAudioBgm();
  145. });
  146. onUnmounted(() => {
  147. audioRest();
  148. audioBgm.value = null;
  149. controls.mediaCtrl.removeMedia(audioKey);
  150. });
  151. const seekChange = (v: number) => {
  152. state.currentTime = v;
  153. audioBgm.value && audioBgm.value.seek(v);
  154. };
  155. const volumeChange = (v: number) => {
  156. state.volume = v;
  157. if (rootComp) {
  158. actions.updateCompData(rootComp, "value.volume", v);
  159. }
  160. audioBgm.value && audioBgm.value.volume(v);
  161. };
  162. const audioRest = () => {
  163. if (!audioBgm.value) {
  164. return;
  165. }
  166. let playing = audioBgm.value.playing();
  167. if (playing) {
  168. audioBgm.value.pause();
  169. }
  170. state.playStatus = false;
  171. state.currentTime = 0;
  172. audioBgm.value.seek(0);
  173. };
  174. return () => {
  175. const music = rootComp?.value.music;
  176. return (
  177. <div class={store.isEditMode ? MusicEditStyle : MusicStyle}>
  178. {store.isEditMode ? (
  179. <AudioPlayer
  180. key={music}
  181. playStatus={state.playStatus}
  182. volume={state.volume}
  183. onStatus={playAudio}
  184. onSeekChange={seekChange}
  185. onVolumeChange={volumeChange}
  186. duration={state.duration}
  187. currentTime={state.currentTime}
  188. />
  189. ) : (
  190. <div
  191. class={["music_button", state.playStatus ? "rotating" : ""]}
  192. onClick={() => {
  193. playAudio(!state.playStatus);
  194. }}
  195. >
  196. <IconMusic />
  197. </div>
  198. )}
  199. </div>
  200. );
  201. };
  202. },
  203. });
  204. const AudioPlayer = defineComponent({
  205. props: {
  206. volume: number(),
  207. playStatus: bool(),
  208. currentTime: number(),
  209. duration: number(),
  210. },
  211. emits: ["status", "seekChange", "volumeChange"],
  212. setup(props, { emit }) {
  213. const audioControl = (playStatus: boolean) => {
  214. emit("status", playStatus);
  215. };
  216. const seekChange = (v: any) => {
  217. emit("seekChange", v);
  218. };
  219. const volumeChange = (v: any) => {
  220. emit("volumeChange", v);
  221. };
  222. const formatTime = (secs?: number) => {
  223. if (!secs) {
  224. return "00:00";
  225. }
  226. secs = Math.round(secs);
  227. const minutes = Math.floor(secs / 60) || 0;
  228. const seconds = secs - minutes * 60 || 0;
  229. return (
  230. String(minutes).padStart(2, "0") +
  231. ":" +
  232. String(seconds).padStart(2, "0")
  233. );
  234. };
  235. const volumeSlider = () => {
  236. return (
  237. <div class={[VolumeSliderStyle]}>
  238. <div class={"h-100px pb-8px"}>
  239. <Slider
  240. tooltipVisible={false}
  241. min={0}
  242. max={1}
  243. step={0.1}
  244. vertical={true}
  245. value={props.volume}
  246. onChange={volumeChange}
  247. ></Slider>
  248. </div>
  249. </div>
  250. );
  251. };
  252. return () => {
  253. return (
  254. <div class={AudioPlayerStyle}>
  255. {!props.playStatus ? (
  256. <Button
  257. type="link"
  258. icon={<PlayCircleOutlined style={{ fontSize: "24px" }} />}
  259. onClick={() => audioControl(true)}
  260. ></Button>
  261. ) : (
  262. <Button
  263. type="link"
  264. icon={<PauseCircleOutlined style={{ fontSize: "24px" }} />}
  265. onClick={() => audioControl(false)}
  266. ></Button>
  267. )}
  268. <div class={"flex-1 px-10px"}>
  269. <Slider
  270. disabled={!props.playStatus}
  271. tooltipVisible={false}
  272. min={0}
  273. max={Math.floor(props.duration || 0)}
  274. value={props.currentTime}
  275. onChange={seekChange}
  276. ></Slider>
  277. </div>
  278. <div>
  279. {formatTime(props.currentTime)}/{formatTime(props.duration)}
  280. </div>
  281. <div>
  282. <Dropdown
  283. disabled={!props.playStatus}
  284. overlay={volumeSlider()}
  285. trigger="click"
  286. placement="top"
  287. >
  288. <Button
  289. type="link"
  290. icon={<SoundOutlined style={{ fontSize: "18px" }} />}
  291. ></Button>
  292. </Dropdown>
  293. </div>
  294. </div>
  295. );
  296. };
  297. },
  298. });
  299. const VolumeSliderStyle = css`
  300. margin-bottom: -4px;
  301. padding: 8px 4px;
  302. background-color: #303030;
  303. border-radius: 4px;
  304. box-shadow: 0 0 2px 2px rgba(0, 0, 0, 0.2);
  305. /* slider style */
  306. .ant-slider-disabled {
  307. opacity: 0.7;
  308. }
  309. .ant-slider-step {
  310. background-color: rgba(255, 255, 255, 0.27);
  311. }
  312. .ant-slider-track {
  313. border-radius: 4px;
  314. background-color: rgba(255, 255, 255, 1);
  315. }
  316. .ant-slider:not(.ant-slider-disabled):hover {
  317. .ant-slider-handle {
  318. background-color: @inf-primary-color;
  319. &:not(.ant-tooltip-open) {
  320. border-color: #fff;
  321. }
  322. }
  323. }
  324. .ant-slider {
  325. &.ant-slider-disabled {
  326. .ant-slider-handle {
  327. background-color: #bbb;
  328. opacity: 0.8;
  329. }
  330. }
  331. }
  332. .ant-slider-handle {
  333. width: 14px;
  334. height: 8px;
  335. border-radius: 2px;
  336. border-color: #fff;
  337. background-color: #fff;
  338. }
  339. .ant-slider-handle-click-focused {
  340. border-color: #fff;
  341. background-color: @inf-primary-color;
  342. }
  343. `;
  344. const MusicEditStyle = css`
  345. flex: 1;
  346. `;
  347. const AudioPlayerStyle = css`
  348. width: 100%;
  349. display: flex;
  350. align-items: center;
  351. /* slider style */
  352. .ant-slider-disabled {
  353. opacity: 0.7;
  354. }
  355. .ant-slider-step {
  356. background-color: rgba(255, 255, 255, 0.27);
  357. }
  358. .ant-slider-track {
  359. border-radius: 4px;
  360. background-color: rgba(255, 255, 255, 1);
  361. }
  362. .ant-slider:not(.ant-slider-disabled):hover {
  363. .ant-slider-handle {
  364. background-color: @inf-primary-color;
  365. &:not(.ant-tooltip-open) {
  366. border-color: #fff;
  367. }
  368. }
  369. }
  370. .ant-slider {
  371. &.ant-slider-disabled {
  372. .ant-slider-handle {
  373. background-color: #bbb;
  374. opacity: 0.8;
  375. }
  376. }
  377. }
  378. .ant-slider-handle {
  379. width: 8px;
  380. border-radius: 2px;
  381. border-color: #fff;
  382. background-color: #fff;
  383. }
  384. .ant-slider-handle-click-focused {
  385. border-color: #fff;
  386. background-color: @inf-primary-color;
  387. }
  388. `;
  389. const MusicStyle = css`
  390. position: fixed;
  391. top: 10px;
  392. right: 10px;
  393. z-index: 999;
  394. .music_button {
  395. width: 48px;
  396. height: 48px;
  397. display: inline-flex;
  398. justify-content: center;
  399. align-items: center;
  400. background-color: rgba(0, 0, 0, 0.5);
  401. font-size: 28px;
  402. border-radius: 24px;
  403. cursor: pointer;
  404. &.rotating {
  405. animation: myRotate 5s linear infinite;
  406. }
  407. }
  408. @keyframes myRotate {
  409. 0% {
  410. transform: rotate(0);
  411. }
  412. 100% {
  413. transform: rotate(360deg);
  414. }
  415. }
  416. `;