index.tsx 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. import { defineComponent, onMounted, reactive, toRefs, watch } from 'vue';
  2. import { ref } from 'vue';
  3. import styles from './index.module.less';
  4. import iconLoop from '../../image/icon-loop.svg';
  5. import iconLoopActive from '../../image/icon-loop-active.svg';
  6. import iconplay from '../../image/icon-play.svg';
  7. import iconpause from '../../image/icon-pause.svg';
  8. import { NSlider } from 'naive-ui';
  9. import { getSecondRPM } from '@/helpers/utils';
  10. export default defineComponent({
  11. name: 'video-play',
  12. props: {
  13. item: {
  14. type: Object,
  15. default: () => {
  16. return {};
  17. }
  18. },
  19. pageVisibility: {
  20. type: String,
  21. default: ''
  22. },
  23. show: {
  24. type: Boolean,
  25. default: false
  26. },
  27. showModel: {
  28. type: Boolean,
  29. default: false
  30. },
  31. isEmtry: {
  32. type: Boolean,
  33. default: false
  34. }
  35. },
  36. emits: ['loadedmetadata', 'togglePlay', 'ended', 'reset'],
  37. setup(props, { emit }) {
  38. const { item, isEmtry } = toRefs(props);
  39. const data = reactive({
  40. timer: null as any,
  41. currentTime: 0,
  42. duration: 0,
  43. loop: false,
  44. playState: 'pause' as 'play' | 'pause',
  45. vudio: null as any
  46. });
  47. const canvasRef: any = ref();
  48. const elRef: any = ref();
  49. const contetRef = ref();
  50. watch(
  51. () => props.show,
  52. val => {
  53. onToggleAudio('pause');
  54. }
  55. );
  56. watch(
  57. () => props.pageVisibility,
  58. val => {
  59. if (val === 'hidden' && props.show) {
  60. onToggleAudio('pause');
  61. }
  62. }
  63. );
  64. let playTimer = null as any;
  65. // 切换音频播放
  66. const onToggleAudio = (state: 'play' | 'pause') => {
  67. clearTimeout(playTimer);
  68. if (state === 'play') {
  69. playTimer = setTimeout(() => {
  70. elRef.value.play();
  71. data.playState = 'play';
  72. }, 100);
  73. } else {
  74. elRef.value.pause();
  75. data.playState = 'pause';
  76. }
  77. };
  78. /** 加载成功 */
  79. const onLoadedmetadata = () => {
  80. data.duration = elRef.value.duration;
  81. // // 加载成功后台, 如果是第一次加载, 且是show状态, 则播放
  82. // if (props.show) {
  83. // onToggleAudio('play');
  84. // }
  85. emit('loadedmetadata');
  86. };
  87. /** 改变播放时间 */
  88. const handleChangeTime = (val: number) => {
  89. data.currentTime = val;
  90. clearTimeout(data.timer);
  91. data.timer = setTimeout(() => {
  92. elRef.value.currentTime = val;
  93. data.timer = null;
  94. }, 300);
  95. };
  96. /** 播放结束 */
  97. const onEnded = () => {
  98. data.playState = 'pause';
  99. // emit('ended');
  100. };
  101. onMounted(() => {
  102. console.log('加载');
  103. });
  104. return () => (
  105. <div class={styles.videoWrap}>
  106. <div class={styles.content}>
  107. <div ref={contetRef} class={styles.contentWrap}>
  108. <video
  109. poster={props.item.coverImg}
  110. src={isEmtry.value ? '' : item.value.content}
  111. ref={elRef}
  112. loop={data.loop}
  113. onLoadedmetadata={onLoadedmetadata}
  114. onTimeupdate={() => {
  115. if (data.timer) return;
  116. data.currentTime = elRef.value.currentTime;
  117. }}
  118. onEnded={onEnded}
  119. playsinline="false"></video>
  120. </div>
  121. </div>
  122. <div class={[styles.controls, props.showModel ? '' : styles.hide]}>
  123. <div class={styles.time}>
  124. <div>{getSecondRPM(data.currentTime)}</div>
  125. <div>{getSecondRPM(data.duration)}</div>
  126. </div>
  127. <div class={styles.slider}>
  128. <NSlider
  129. tooltip={false}
  130. step={0.01}
  131. class={styles.timeProgress}
  132. value={data.currentTime}
  133. max={data.duration}
  134. onUpdate:value={val => handleChangeTime(val)}
  135. />
  136. </div>
  137. <div class={styles.actions}>
  138. <div
  139. class={styles.actionBtn}
  140. onClick={() =>
  141. onToggleAudio(data.playState === 'pause' ? 'play' : 'pause')
  142. }>
  143. <img src={data.playState === 'pause' ? iconplay : iconpause} />
  144. </div>
  145. <div
  146. class={styles.actionBtn}
  147. onClick={() => (data.loop = !data.loop)}>
  148. <img src={data.loop ? iconLoopActive : iconLoop} />
  149. </div>
  150. </div>
  151. </div>
  152. </div>
  153. );
  154. }
  155. });