player.tsx 4.8 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 iconEvaluatingStart from './icons/icon-evaluatingStart.png'
  9. import styles from './index.module.less'
  10. export default defineComponent({
  11. name: 'ButtonsPlayer',
  12. setup() {
  13. const [wiredStatus] = useWiredHeadsetCheck()
  14. const reset = () => {
  15. sendMessage()
  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('父页面的切换事件', res.data, runtime.playState)
  27. if (runtime.playState == 'play') {
  28. RuntimeUtils.setPlayState()
  29. }
  30. RuntimeUtils.stopTick()
  31. }
  32. }
  33. const sendMessage = () => {
  34. window.parent.postMessage(
  35. {
  36. api: 'headerTogge',
  37. playState: runtime.playState
  38. },
  39. '*'
  40. )
  41. }
  42. onMounted(() => {
  43. window.addEventListener('message', changePlay)
  44. })
  45. onUnmounted(() => {
  46. window.removeEventListener('message', changePlay)
  47. })
  48. return () => {
  49. const playProgress = (runtime.currentTimeNum / runtime.durationNum) * circleLength
  50. return (
  51. <Teleport to="body">
  52. <div class={styles.player} id="globalPlayer">
  53. <Transition name="start" duration={300}>
  54. {wiredStatus.value && !evaluatingRef.value?.connentLoading && startButtonShow.value && (
  55. <Button
  56. style={{ backgroundImage: `url(${iconEvaluatingStart})` }}
  57. class={[styles.button, styles.start]}
  58. onClick={() => {
  59. startButtonShow.value = false
  60. evaluatingRef.value?.togglePlay?.()
  61. }}
  62. >
  63. </Button>
  64. )}
  65. </Transition>
  66. {!runtime.evaluatingStatus && (
  67. <>
  68. {(runtime.playState === 'pause' || runtime.playState === 'suspend') &&
  69. runtime.currentTimeNum > 0 &&
  70. !detailState.sectionStatus && (
  71. <Button class={[styles.button, styles.fullbtn]} onClick={reset}>
  72. <ButtonIcon key="reset" name="reset" />
  73. </Button>
  74. )}
  75. <Button
  76. class={[styles.button, styles.fullbtn]}
  77. style={{
  78. marginLeft: '14px',
  79. }}
  80. disabled={detailState.activeDetail?.isAppPlay && detailState.midiPlayIniting}
  81. onClick={() => {
  82. RuntimeUtils.setPlayState()
  83. sendMessage()
  84. }}
  85. >
  86. <div class={styles.schedule}>
  87. <div class={styles.schedule}>
  88. {runtime.playState === 'play' ? (
  89. <ButtonIcon key="pause" name="pause" />
  90. ) : (
  91. <ButtonIcon key="play" name="play" />
  92. )}
  93. {runtime.currentTimeNum > 0 && !detailState.sectionStatus && (
  94. <svg
  95. class={styles.ring}
  96. width="40"
  97. height="40"
  98. viewBox="0 0 40 40"
  99. xmlns="http://www.w3.org/200/svg"
  100. >
  101. <circle
  102. cx="20"
  103. cy="20"
  104. r="16"
  105. fill="none"
  106. stroke="#fff"
  107. stroke-width="2"
  108. stroke-linecap="round"
  109. />
  110. <circle
  111. class={styles.fillring}
  112. cx="20"
  113. cy="20"
  114. r="16"
  115. fill="none"
  116. stroke="#FFC459"
  117. stroke-width="2"
  118. stroke-linecap="round"
  119. stroke-dasharray={playProgress + ',10000'}
  120. />
  121. </svg>
  122. )}
  123. </div>
  124. </div>
  125. </Button>
  126. </>
  127. )}
  128. </div>
  129. </Teleport>
  130. )
  131. }
  132. },
  133. })