enjoyPlayer.vue 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558
  1. <template>
  2. <div class="enjoyPlayer">
  3. <div v-if="!isBase" v-show="isShowEnjoyPlayerList" class="enjoyPlayerList">
  4. <div class="titNameCon">
  5. <div class="titName">
  6. <div class="tit">{{ `音频列表 (${elementInfo.enjoyList?.length || 0})` }}</div>
  7. </div>
  8. </div>
  9. <div class="enjoyPlayerListCon">
  10. <draggable
  11. :handle="'.drag-handle_enjoyPlayer'"
  12. v-model="elementInfo.enjoyList"
  13. itemKey="id"
  14. :animation="200"
  15. :scroll="true"
  16. :scrollSensitivity="50"
  17. @end="handleDragEnd"
  18. >
  19. <template #item="{ element }">
  20. <div class="playerItem" :class="{ active: element.id === elementInfo.sid }" @click="handlePlayMusic(element)">
  21. <div class="itemLeft">
  22. {{ element.title }}
  23. </div>
  24. <div class="itemRight">
  25. <template v-if="!isScreening">
  26. <div class="itemListBtn drag-handle_enjoyPlayer" @click.stop></div>
  27. <div class="itemCloseBtn" @click.stop="handleEnjoyDel(element.id)"></div>
  28. </template>
  29. </div>
  30. </div>
  31. </template>
  32. </draggable>
  33. </div>
  34. </div>
  35. <audio
  36. v-if="!isBase"
  37. ref="audioRef"
  38. :src="elementInfo.src"
  39. @durationchange="handleDurationchange()"
  40. @timeupdate="handleTimeupdate()"
  41. @play="handlePlayed()"
  42. @pause="handlePaused()"
  43. @ended="handleEnded()"
  44. @progress="handleProgress()"
  45. @error="handleError()"
  46. ></audio>
  47. <div class="playerCon">
  48. <img class="tipImg" src="./imgs/tip.png" alt="" />
  49. <div class="operateBtn" :class="{ paused: paused }" @click="toggle"></div>
  50. <div class="operateMidCon">
  51. <div class="titleCon">
  52. <div class="title">{{ elementInfo.title }}</div>
  53. <div class="timesCon">
  54. {{ `${ptime}/${dtime}` }}
  55. </div>
  56. </div>
  57. <div class="bar-wrap" ref="playBarWrap" @mousedown="handleMousedownPlayBar()" @touchstart="handleMousedownPlayBar()">
  58. <div class="bar">
  59. <div class="loaded" :style="{ width: loadedBarWidth }"></div>
  60. <div class="played" :style="{ width: playedBarWidth }">
  61. <div class="thumb"></div>
  62. </div>
  63. </div>
  64. </div>
  65. </div>
  66. <div class="operateRightBtn">
  67. <div class="preBtn" @click="handleChangeMusic('pre')"></div>
  68. <div class="nextBtn" @click="handleChangeMusic('next')"></div>
  69. <div class="listBtn" @click="hanleEnjoyList"></div>
  70. </div>
  71. </div>
  72. </div>
  73. </template>
  74. <script setup lang="ts">
  75. import { computed, ref, onUnmounted } from "vue"
  76. import message from "@/utils/message"
  77. import type { PPTEnjoyElement } from "@/types/slides"
  78. import { useSlidesStore, useMainStore } from "@/store"
  79. import draggable from "vuedraggable"
  80. const slidesStore = useSlidesStore()
  81. const mainStore = useMainStore()
  82. const props = withDefaults(
  83. defineProps<{
  84. scale?: number
  85. elementInfo: PPTEnjoyElement
  86. isScreening?: boolean
  87. isBase?: boolean
  88. }>(),
  89. {
  90. scale: 1,
  91. isScreening: false,
  92. isBase: false
  93. }
  94. )
  95. const isShowEnjoyPlayerList = ref(false)
  96. function hanleEnjoyList() {
  97. if (props.isBase) {
  98. return
  99. }
  100. isShowEnjoyPlayerList.value = !isShowEnjoyPlayerList.value
  101. mainStore.setIsPPTWheelPageState(!isShowEnjoyPlayerList.value)
  102. }
  103. // 卸载之后让ppt能滚动
  104. onUnmounted(() => {
  105. mainStore.setIsPPTWheelPageState(true)
  106. })
  107. function handleDragEnd() {
  108. slidesStore.updateElement({
  109. id: props.elementInfo.id,
  110. props: {
  111. enjoyList: props.elementInfo.enjoyList
  112. }
  113. })
  114. }
  115. function handleEnjoyDel(id: string) {
  116. if (id === props.elementInfo.sid) {
  117. //删除组件
  118. if (props.elementInfo.enjoyList.length === 1) {
  119. slidesStore.deleteElement(props.elementInfo.id)
  120. } else {
  121. const index = props.elementInfo.enjoyList.findIndex(item => {
  122. return item.id === id
  123. })
  124. // eslint-disable-next-line vue/no-mutating-props
  125. props.elementInfo.enjoyList.splice(index, 1)
  126. const enjoyList = props.elementInfo.enjoyList
  127. slidesStore.updateElement({
  128. id: props.elementInfo.id,
  129. props: {
  130. sid: enjoyList[0].id,
  131. title: enjoyList[0].title,
  132. src: enjoyList[0].src,
  133. enjoyList: enjoyList
  134. }
  135. })
  136. // 关闭播放按钮
  137. paused.value = true
  138. }
  139. } else {
  140. const index = props.elementInfo.enjoyList.findIndex(item => {
  141. return item.id === id
  142. })
  143. // eslint-disable-next-line vue/no-mutating-props
  144. props.elementInfo.enjoyList.splice(index, 1)
  145. slidesStore.updateElement({
  146. id: props.elementInfo.id,
  147. props: {
  148. enjoyList: props.elementInfo.enjoyList
  149. }
  150. })
  151. }
  152. }
  153. function handleChangeMusic(type: "pre" | "next") {
  154. if (props.elementInfo.enjoyList.length === 1) {
  155. seek(0)
  156. } else {
  157. let index = props.elementInfo.enjoyList.findIndex(item => {
  158. return item.id === props.elementInfo.sid
  159. })
  160. index += type === "next" ? 1 : -1
  161. if (index > props.elementInfo.enjoyList.length - 1) {
  162. index = 0
  163. } else if (index < 0) {
  164. index = props.elementInfo.enjoyList.length - 1
  165. }
  166. const enjoyData = props.elementInfo.enjoyList[index]
  167. slidesStore.updateElement({
  168. id: props.elementInfo.id,
  169. props: {
  170. sid: enjoyData.id,
  171. title: enjoyData.title,
  172. src: enjoyData.src
  173. }
  174. })
  175. }
  176. }
  177. function handlePlayMusic(item: Record<string, any>) {
  178. if (item.id === props.elementInfo.sid) {
  179. toggle()
  180. } else {
  181. slidesStore.updateElement({
  182. id: props.elementInfo.id,
  183. props: {
  184. sid: item.id,
  185. title: item.title,
  186. src: item.src
  187. }
  188. })
  189. }
  190. }
  191. const secondToTime = (second = 0) => {
  192. if (second === 0 || isNaN(second)) return "00:00"
  193. const add0 = (num: number) => (num < 10 ? "0" + num : "" + num)
  194. const hour = Math.floor(second / 3600)
  195. const min = Math.floor((second - hour * 3600) / 60)
  196. const sec = Math.floor(second - hour * 3600 - min * 60)
  197. return (hour > 0 ? [hour, min, sec] : [min, sec]).map(add0).join(":")
  198. }
  199. const getBoundingClientRectViewLeft = (element: HTMLElement) => {
  200. return element.getBoundingClientRect().left
  201. }
  202. const audioRef = ref<HTMLAudioElement>()
  203. const playBarWrap = ref<HTMLElement>()
  204. const paused = ref(true)
  205. const currentTime = ref(0)
  206. const duration = ref(0)
  207. const loaded = ref(0)
  208. const isThumbDown = ref(false)
  209. const ptime = computed(() => secondToTime(currentTime.value))
  210. const dtime = computed(() => secondToTime(duration.value))
  211. const playedBarWidth = computed(() => (currentTime.value / duration.value) * 100 + "%")
  212. const loadedBarWidth = computed(() => (loaded.value / duration.value) * 100 + "%")
  213. const seek = (time: number) => {
  214. if (!audioRef.value) return
  215. time = Math.max(time, 0)
  216. time = Math.min(time, duration.value)
  217. audioRef.value.currentTime = time
  218. currentTime.value = time
  219. }
  220. const play = () => {
  221. if (!audioRef.value) return
  222. paused.value = false
  223. audioRef.value.play()
  224. }
  225. const pause = () => {
  226. if (!audioRef.value) return
  227. paused.value = true
  228. audioRef.value.pause()
  229. }
  230. const toggle = () => {
  231. if (paused.value) play()
  232. else pause()
  233. }
  234. const handleDurationchange = () => {
  235. duration.value = audioRef.value?.duration || 0
  236. }
  237. const handleTimeupdate = () => {
  238. currentTime.value = audioRef.value?.currentTime || 0
  239. }
  240. const handlePlayed = () => {
  241. paused.value = false
  242. }
  243. const handlePaused = () => {
  244. paused.value = true
  245. }
  246. const handleEnded = () => {
  247. return
  248. if (isThumbDown.value) {
  249. return
  250. }
  251. seek(0)
  252. play()
  253. }
  254. const handleProgress = () => {
  255. loaded.value = audioRef.value?.buffered.length ? audioRef.value.buffered.end(audioRef.value.buffered.length - 1) : 0
  256. if (!paused.value) {
  257. play()
  258. }
  259. }
  260. const handleError = () => message.error("音频加载失败")
  261. const thumbMove = (e: MouseEvent | TouchEvent) => {
  262. if (!audioRef.value || !playBarWrap.value) return
  263. const clientX = "clientX" in e ? e.clientX : e.changedTouches[0].clientX
  264. let percentage = (clientX - getBoundingClientRectViewLeft(playBarWrap.value)) / playBarWrap.value.clientWidth / props.scale
  265. percentage = Math.max(percentage, 0)
  266. percentage = Math.min(percentage, 1)
  267. const time = percentage * duration.value
  268. audioRef.value.currentTime = time
  269. currentTime.value = time
  270. }
  271. const thumbUp = (e: MouseEvent | TouchEvent) => {
  272. if (!audioRef.value || !playBarWrap.value) return
  273. const clientX = "clientX" in e ? e.clientX : e.changedTouches[0].clientX
  274. let percentage = (clientX - getBoundingClientRectViewLeft(playBarWrap.value)) / playBarWrap.value.clientWidth / props.scale
  275. percentage = Math.max(percentage, 0)
  276. percentage = Math.min(percentage, 1)
  277. const time = percentage * duration.value
  278. audioRef.value.currentTime = time
  279. currentTime.value = time
  280. const _time = setTimeout(() => {
  281. clearTimeout(_time)
  282. isThumbDown.value = false
  283. }, 500)
  284. document.removeEventListener("mousemove", thumbMove)
  285. document.removeEventListener("touchmove", thumbMove)
  286. document.removeEventListener("mouseup", thumbUp)
  287. document.removeEventListener("touchend", thumbUp)
  288. }
  289. const handleMousedownPlayBar = () => {
  290. isThumbDown.value = true
  291. document.addEventListener("mousemove", thumbMove)
  292. document.addEventListener("touchmove", thumbMove)
  293. document.addEventListener("mouseup", thumbUp)
  294. document.addEventListener("touchend", thumbUp)
  295. }
  296. </script>
  297. <style lang="scss" scoped>
  298. .enjoyPlayer {
  299. transform-origin: left top;
  300. .playerCon {
  301. background: linear-gradient(180deg, #ffffff 0%, #dfdfdf 100%);
  302. box-shadow:
  303. 0px 3px 16px 0px rgba(0, 0, 0, 0.08),
  304. inset 0px -6px 6px 0px rgba(0, 0, 0, 0.08);
  305. border-radius: 112px;
  306. outline: 2px solid rgba(0, 0, 0, 0.03);
  307. padding: 16px 21px;
  308. width: 670px;
  309. display: flex;
  310. align-items: center;
  311. position: relative;
  312. z-index: 1;
  313. .tipImg {
  314. position: absolute;
  315. width: 44px;
  316. height: 12px;
  317. right: 47px;
  318. top: 7px;
  319. }
  320. .operateBtn {
  321. flex-shrink: 0;
  322. background: url("./imgs/pause.png") no-repeat;
  323. background-size: 100% 100%;
  324. width: 70px;
  325. height: 70px;
  326. cursor: pointer;
  327. &.paused {
  328. background: url("./imgs/play.png") no-repeat;
  329. background-size: 100% 100%;
  330. }
  331. }
  332. .operateMidCon {
  333. margin: 0 21px;
  334. flex-grow: 1;
  335. .titleCon {
  336. display: flex;
  337. align-items: center;
  338. justify-content: space-between;
  339. .title {
  340. width: 200px;
  341. font-weight: 600;
  342. font-size: 22px;
  343. color: #131415;
  344. line-height: 30px;
  345. white-space: nowrap;
  346. overflow: hidden;
  347. text-overflow: ellipsis;
  348. }
  349. .timesCon {
  350. flex-shrink: 0;
  351. font-weight: 400;
  352. font-size: 18px;
  353. color: #999999;
  354. line-height: 25px;
  355. }
  356. }
  357. .bar-wrap {
  358. position: relative;
  359. cursor: pointer;
  360. width: 100%;
  361. margin-top: 12px;
  362. .bar {
  363. position: relative;
  364. height: 10px;
  365. width: 100%;
  366. background: #dcdcdc;
  367. box-shadow: inset 0px 2px 3px 0px #a9a9a9;
  368. border-radius: 5px;
  369. outline: 2px solid rgba(255, 255, 255, 0.5);
  370. .loaded {
  371. position: absolute;
  372. left: 0;
  373. top: 0;
  374. bottom: 0;
  375. background: rgba(255, 255, 255, 0.4);
  376. height: 10px;
  377. transition: all 0.5s ease;
  378. will-change: width;
  379. border-radius: 6px;
  380. }
  381. .played {
  382. position: absolute;
  383. left: 0;
  384. top: 0;
  385. bottom: 0;
  386. height: 10px;
  387. will-change: width;
  388. background: linear-gradient(270deg, #97d1fd 0%, #0d93ff 100%);
  389. border-radius: 6px;
  390. .thumb {
  391. position: absolute;
  392. top: -12px;
  393. right: -26px;
  394. cursor: pointer;
  395. width: 52px;
  396. height: 38px;
  397. background: url("./imgs/td.png") no-repeat;
  398. background-size: 100% 100%;
  399. }
  400. }
  401. }
  402. }
  403. }
  404. .operateRightBtn {
  405. flex-shrink: 0;
  406. display: flex;
  407. align-items: center;
  408. .preBtn {
  409. background: url("./imgs/pre.png") no-repeat;
  410. background-size: 100% 100%;
  411. width: 56px;
  412. height: 56px;
  413. margin-right: 14px;
  414. cursor: pointer;
  415. }
  416. .nextBtn {
  417. background: url("./imgs/next.png") no-repeat;
  418. background-size: 100% 100%;
  419. width: 56px;
  420. height: 56px;
  421. margin-right: 14px;
  422. cursor: pointer;
  423. }
  424. .listBtn {
  425. background: url("./imgs/list.png") no-repeat;
  426. background-size: 100% 100%;
  427. width: 56px;
  428. height: 56px;
  429. cursor: pointer;
  430. }
  431. }
  432. }
  433. .enjoyPlayerList {
  434. width: 100%;
  435. height: 520px;
  436. position: absolute;
  437. left: 0;
  438. bottom: 51px;
  439. box-shadow: 0px 3px 22px 0px rgba(0, 0, 0, 0.12);
  440. border-radius: 20px 20px 0px 0px;
  441. background-color: rgba(255, 255, 255, 0.9);
  442. padding: 20px 0 61px 10px;
  443. .titNameCon {
  444. padding-left: 10px;
  445. display: flex;
  446. align-items: center;
  447. height: 38px;
  448. .titName {
  449. font-weight: 600;
  450. font-size: 22px;
  451. color: #131415;
  452. line-height: 30px;
  453. position: relative;
  454. &::after {
  455. position: absolute;
  456. left: 0;
  457. bottom: 0;
  458. content: "";
  459. width: 100%;
  460. height: 12px;
  461. background: linear-gradient(90deg, #77bbff 0%, rgba(163, 231, 255, 0.22) 100%);
  462. }
  463. .tit {
  464. position: relative;
  465. z-index: 1;
  466. }
  467. }
  468. }
  469. .enjoyPlayerListCon {
  470. margin-top: 20px;
  471. overflow-y: auto;
  472. height: calc(100% - 58px);
  473. padding-right: 10px;
  474. .playerItem {
  475. padding: 18px 5px 18px 10px;
  476. display: flex;
  477. justify-content: space-between;
  478. align-items: center;
  479. &.active {
  480. background: rgba(85, 176, 255, 0.15);
  481. border-radius: 10px;
  482. .itemLeft {
  483. color: #198cfe;
  484. font-weight: 600;
  485. }
  486. }
  487. .itemLeft {
  488. font-weight: 400;
  489. font-size: 20px;
  490. color: #131415;
  491. line-height: 28px;
  492. white-space: nowrap;
  493. overflow: hidden;
  494. text-overflow: ellipsis;
  495. }
  496. .itemRight {
  497. margin-left: 10px;
  498. flex-shrink: 0;
  499. display: flex;
  500. .itemListBtn {
  501. width: 34px;
  502. height: 34px;
  503. margin-right: 10px;
  504. background: url("./imgs/td2.png") no-repeat;
  505. background-size: 24px 24px;
  506. background-position: center;
  507. cursor: grab;
  508. }
  509. .itemCloseBtn {
  510. width: 34px;
  511. height: 34px;
  512. background: url("./imgs/del.png") no-repeat;
  513. background-size: 24px 24px;
  514. background-position: center;
  515. cursor: pointer;
  516. }
  517. }
  518. }
  519. }
  520. }
  521. }
  522. </style>