player.tsx 4.6 KB

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