player.tsx 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. import { defineComponent, Transition, Teleport, onMounted, onUnmounted } from 'vue'
  2. import runtime, * as RuntimeUtils from '/src/pages/detail/runtime'
  3. import detailState from '/src/pages/detail/state'
  4. import { Button } from 'vant'
  5. import { evaluatingRef, startButtonShow } from './'
  6. import ButtonIcon from './icon'
  7. import { useMenu, useWiredHeadsetCheck } from '../uses'
  8. import styles from './index.module.less'
  9. export default defineComponent({
  10. name: 'ButtonsPlayer',
  11. setup() {
  12. const [wiredStatus] = useWiredHeadsetCheck()
  13. const reset = () => {
  14. // 重置播放倍率
  15. RuntimeUtils.resetBaseRate();
  16. if (detailState.activeTick > -1) {
  17. return
  18. }
  19. RuntimeUtils.setCurrentTime(0)
  20. RuntimeUtils.ended(new Event('ended'))
  21. }
  22. // 播放进入的圆周长
  23. const circleLength = Math.floor(2 * Math.PI * 16)
  24. const changePlay = (res: any) => {
  25. if (res?.data?.api === 'setPlayState') {
  26. console.log('父页面的切换事件关闭播放')
  27. if (runtime.playState == 'play') {
  28. RuntimeUtils.setPlayState()
  29. }
  30. RuntimeUtils.stopTick()
  31. RuntimeUtils.setCurrentTime(0)
  32. setTimeout(() => {
  33. if (runtime.playState == 'play') {
  34. RuntimeUtils.setPlayState()
  35. }
  36. }, 300)
  37. }
  38. }
  39. onMounted(() => {
  40. window.addEventListener('message', changePlay)
  41. })
  42. onUnmounted(() => {
  43. window.removeEventListener('message', changePlay)
  44. })
  45. return () => {
  46. const playProgress = (runtime.currentTimeNum / runtime.durationNum) * circleLength
  47. return (
  48. <Teleport to="body">
  49. <div class={styles.player} id="globalPlayer">
  50. {!runtime.evaluatingStatus && (
  51. <>
  52. {
  53. runtime.currentTimeNum > 0 &&
  54. !detailState.sectionStatus && (
  55. <Button class={[styles.button, styles.fullbtn]} onClick={reset}>
  56. <ButtonIcon key="reset" name="reset" />
  57. </Button>
  58. )}
  59. <Button
  60. class={[styles.button, styles.fullbtn]}
  61. style={{
  62. marginLeft: '14px',
  63. }}
  64. disabled={detailState.activeDetail?.isAppPlay && detailState.midiPlayIniting}
  65. onClick={() => {
  66. RuntimeUtils.setPlayState()
  67. }}
  68. >
  69. <div class={styles.schedule}>
  70. <div class={styles.schedule}>
  71. {runtime.playState === 'play' ? (
  72. <ButtonIcon
  73. key="pause"
  74. name="pause"
  75. onClick={() => {
  76. console.log('暂停播放')
  77. RuntimeUtils.sendParentMessage('pause')
  78. }}
  79. />
  80. ) : (
  81. <ButtonIcon
  82. key="play"
  83. name="play"
  84. onClick={() => {
  85. console.log('开始播放')
  86. RuntimeUtils.initSetPlayRate();
  87. RuntimeUtils.sendParentMessage('play')
  88. }}
  89. />
  90. )}
  91. {runtime.currentTimeNum > 0 && !detailState.sectionStatus && (
  92. <svg
  93. class={styles.ring}
  94. width="40"
  95. height="40"
  96. viewBox="0 0 40 40"
  97. xmlns="http://www.w3.org/200/svg"
  98. >
  99. <circle
  100. cx="20"
  101. cy="20"
  102. r="16"
  103. fill="none"
  104. stroke="#fff"
  105. stroke-width="2"
  106. stroke-linecap="round"
  107. />
  108. <circle
  109. class={styles.fillring}
  110. cx="20"
  111. cy="20"
  112. r="16"
  113. fill="none"
  114. stroke="#FFC459"
  115. stroke-width="2"
  116. stroke-linecap="round"
  117. stroke-dasharray={playProgress + ',10000'}
  118. />
  119. </svg>
  120. )}
  121. </div>
  122. </div>
  123. </Button>
  124. </>
  125. )}
  126. </div>
  127. </Teleport>
  128. )
  129. }
  130. },
  131. })