video-play.tsx 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  1. import {
  2. defineComponent,
  3. nextTick,
  4. onMounted,
  5. reactive,
  6. toRefs,
  7. watch
  8. } from 'vue';
  9. import { ref } from 'vue';
  10. import styles from './video.module.less';
  11. import { postMessage } from '@/helpers/native-message';
  12. // import iconLoop from '../image/icon-loop.svg';
  13. // import iconLoopActive from '../image/icon-loop-active.svg';
  14. // import iconplay from '../image/icon-play.svg';
  15. // import iconpause from '../image/icon-pause.svg';
  16. import {
  17. // iconVideoBg,
  18. iconLoop,
  19. iconLoopActive,
  20. iconPlay,
  21. iconPause,
  22. iconSpeed
  23. } from '../image/icons.json';
  24. import TCPlayer from 'tcplayer.js';
  25. import 'tcplayer.js/dist/tcplayer.min.css';
  26. import { Slider } from 'vant';
  27. import { state } from '@/state';
  28. // 秒转分
  29. export const getSecondRPM = (second: number, type?: string) => {
  30. if (isNaN(second)) return '00:00';
  31. const mm = Math.floor(second / 60)
  32. .toString()
  33. .padStart(2, '0');
  34. const dd = Math.floor(second % 60)
  35. .toString()
  36. .padStart(2, '0');
  37. if (type === 'cn') {
  38. return mm + '分' + dd + '秒';
  39. } else {
  40. return mm + ':' + dd;
  41. }
  42. };
  43. export default defineComponent({
  44. name: 'video-play',
  45. props: {
  46. item: {
  47. type: Object,
  48. default: () => {
  49. return {};
  50. }
  51. },
  52. isEmtry: {
  53. type: Boolean,
  54. default: false
  55. },
  56. isActive: {
  57. type: Boolean,
  58. default: false
  59. },
  60. activeModel: {
  61. type: Boolean,
  62. default: true
  63. }
  64. },
  65. emits: [
  66. 'loadedmetadata',
  67. 'togglePlay',
  68. 'ended',
  69. 'reset',
  70. 'error',
  71. 'close',
  72. 'play',
  73. 'pause',
  74. 'seeked',
  75. 'seeking',
  76. 'waiting',
  77. 'timeupdate'
  78. ],
  79. setup(props, { emit, expose }) {
  80. const { item } = toRefs(props);
  81. const data = reactive({
  82. timer: null as any,
  83. currentTime: 0,
  84. duration: 0.1,
  85. loop: false,
  86. playState: 'pause' as 'play' | 'pause',
  87. vudio: null as any,
  88. showBar: true,
  89. speedControl: false,
  90. speedStyle: {
  91. left: '1px'
  92. },
  93. defaultSpeed: 1 // 默认速度
  94. });
  95. const speedBtnId = 'speed' + Date.now() + Math.floor(Math.random() * 100);
  96. // const forms = reactive({
  97. // subjectIds: [],
  98. // orgainIds: []
  99. // });
  100. const videoRef = ref();
  101. const videoItem = ref();
  102. const videoID = 'video' + Date.now() + Math.floor(Math.random() * 100);
  103. const toggleHideControl = (isShow: boolean) => {
  104. data.speedControl = false;
  105. data.showBar = isShow;
  106. };
  107. // const togglePlay = (e: Event) => {
  108. // e.stopPropagation()
  109. // }
  110. let playTimer = null as any;
  111. // 切换音频播放
  112. const onToggleAudio = (state: 'play' | 'pause') => {
  113. // console.log(state, 'state')
  114. data.speedControl = false;
  115. clearTimeout(playTimer);
  116. if (state === 'play') {
  117. playTimer = setTimeout(() => {
  118. videoItem.value?.play();
  119. data.playState = 'play';
  120. }, 100);
  121. } else {
  122. videoItem.value?.pause();
  123. data.playState = 'pause';
  124. }
  125. emit('togglePlay', data.playState);
  126. };
  127. const toggleLoop = () => {
  128. data.speedControl = false;
  129. if (!videoItem.value) return;
  130. if (data.loop) {
  131. videoItem.value.loop(false);
  132. } else {
  133. videoItem.value.loop(true);
  134. }
  135. data.loop = !data.loop;
  136. };
  137. const changePlayBtn = (code: string) => {
  138. // data.speedControl = false;
  139. if (code == 'play') {
  140. data.playState = 'play';
  141. } else {
  142. data.playState = 'pause';
  143. }
  144. };
  145. /** 改变播放时间 */
  146. const handleChangeTime = (val: number) => {
  147. data.currentTime = val;
  148. clearTimeout(data.timer);
  149. data.timer = setTimeout(() => {
  150. videoItem.value.currentTime(val);
  151. data.timer = null;
  152. }, 300);
  153. };
  154. const __initVideo = () => {
  155. if (videoItem.value && props.item.id) {
  156. console.log(videoItem.value, 'videoItem.value');
  157. nextTick(() => {
  158. videoItem.value?.currentTime(0);
  159. });
  160. videoItem.value.poster(props.item.coverImg); // 封面
  161. videoItem.value.src(props.item.content); // url 播放地址
  162. videoItem.value.playbackRate(data.defaultSpeed);
  163. data.speedControl = false;
  164. // 初步加载时
  165. videoItem.value.on('loadedmetadata', () => {
  166. videoItem.value.playbackRate(data.defaultSpeed);
  167. // 获取时长
  168. data.duration = videoItem.value.duration();
  169. // 必须在当前元素
  170. if (item.value.autoPlay && videoItem.value && props.isActive) {
  171. // videoItem.value?.play()
  172. nextTick(() => {
  173. videoItem.value.currentTime(0);
  174. nextTick(handlePlayVideo);
  175. });
  176. }
  177. emit('loadedmetadata', videoItem.value);
  178. });
  179. // 视频播放时加载
  180. videoItem.value.on('timeupdate', () => {
  181. if (data.timer) return;
  182. data.currentTime = videoItem.value.currentTime();
  183. emit('timeupdate');
  184. });
  185. // 视频播放结束
  186. videoItem.value.on('ended', () => {
  187. changePlayBtn('pause');
  188. emit('ended');
  189. });
  190. //
  191. videoItem.value.on('pause', () => {
  192. data.playState = 'pause';
  193. changePlayBtn('pause');
  194. emit('togglePlay', true);
  195. emit('pause');
  196. });
  197. videoItem.value.on('seeked', () => {
  198. emit('seeked');
  199. });
  200. videoItem.value.on('seeking', () => {
  201. emit('seeking');
  202. });
  203. videoItem.value.on('waiting', () => {
  204. emit('waiting');
  205. });
  206. videoItem.value.on('play', () => {
  207. // console.log(play, 'playing')
  208. changePlayBtn('play');
  209. if (videoItem.value) {
  210. videoItem.value.muted(false);
  211. videoItem.value.volume(1);
  212. }
  213. // if (
  214. // !item.value.autoPlay &&
  215. // !item.value.isprepare &&
  216. // videoItem.value
  217. // ) {
  218. // // 加载完成后,取消静音播放
  219. // videoItem.value.pause();
  220. // }
  221. emit('togglePlay', videoItem.value?.paused);
  222. emit('play');
  223. });
  224. // 视频播放异常
  225. videoItem.value.on('error', (e: any) => {
  226. handleErrorVideo();
  227. emit('error');
  228. console.log(e, 'error');
  229. });
  230. }
  231. };
  232. let videoTimer = null as any;
  233. let videoTimerErrorCount = 0;
  234. const handlePlayVideo = () => {
  235. if (videoTimerErrorCount > 5) {
  236. return;
  237. }
  238. clearTimeout(videoTimer);
  239. nextTick(() => {
  240. videoItem.value?.play().catch((err: any) => {
  241. // console.log('🚀 ~ err:', err)
  242. videoTimer = setTimeout(() => {
  243. if (err?.message?.includes('play()')) {
  244. emit('play');
  245. }
  246. handlePlayVideo();
  247. }, 1000);
  248. });
  249. });
  250. videoTimerErrorCount++;
  251. };
  252. let videoErrorTimer = null as any;
  253. let videoErrorCount = 0;
  254. const handleErrorVideo = () => {
  255. if (videoErrorCount > 5) {
  256. return;
  257. }
  258. clearTimeout(videoErrorTimer);
  259. nextTick(() => {
  260. videoErrorTimer = setTimeout(() => {
  261. videoItem.value.src(props.item?.content);
  262. emit('play');
  263. videoItem.value.load();
  264. // eslint-disable-next-line @typescript-eslint/no-unused-vars
  265. handleErrorVideo();
  266. }, 1000);
  267. });
  268. videoErrorCount++;
  269. };
  270. onMounted(() => {
  271. videoItem.value = TCPlayer(videoID, {
  272. appID: '',
  273. controls: false,
  274. autoplay: true
  275. }); // player-container-id 为播放器容器 ID,必须与 html 中一致
  276. __initVideo();
  277. document.getElementById(speedBtnId)?.addEventListener('click', e => {
  278. e.stopPropagation();
  279. data.speedControl = !data.speedControl;
  280. });
  281. });
  282. watch(
  283. () => props.activeModel,
  284. () => {
  285. toggleHideControl(props.activeModel);
  286. }
  287. );
  288. watch(
  289. () => props.item,
  290. () => {
  291. videoItem.value?.currentTime(0);
  292. setTimeout(() => {
  293. videoItem.value?.pause();
  294. __initVideo();
  295. }, 60);
  296. }
  297. );
  298. // 去云练习完整版
  299. const gotoAccomany = (e: any) => {
  300. // 去云练习完整版
  301. e.stopPropagation();
  302. // const Authorization = sessionStorage.getItem('Authorization') || '';
  303. // const origin = /(localhost|192)/.test(location.host)
  304. // ? 'https://test.gym.lexiaoya.cn'
  305. // : location.origin;
  306. // const src = `${origin}/${
  307. // state.platformType === 'TEACHER' ? 'accompany-teacher' : 'accompany'
  308. // }/#/detail/${
  309. // item.value.materialMusicId
  310. // }?Authorization=${Authorization}&isHideTitle=true`;
  311. const origin = /(localhost|192)/.test(location.host)
  312. ? 'https://test.gym.lexiaoya.cn/'
  313. : location.origin;
  314. const src = `${origin}/gym-music-score/?id=${item.value.materialMusicId}&isHideMusicList=true&systemType=${ state.platformType === 'TEACHER' ? 'teacher' : 'student'}`
  315. postMessage({
  316. api: 'openAccompanyWebView',
  317. content: {
  318. url: src,
  319. orientation: 0,
  320. c_orientation: 0,
  321. isHideTitle: true,
  322. statusBarTextColor: false,
  323. isOpenLight: true
  324. }
  325. });
  326. };
  327. const getVideoRef = () => {
  328. return videoRef.value;
  329. };
  330. const getPlyrRef = () => {
  331. return videoItem.value;
  332. };
  333. expose({
  334. changePlayBtn,
  335. toggleHideControl,
  336. getVideoRef,
  337. getPlyrRef
  338. });
  339. watch(
  340. () => props.isActive,
  341. val => {
  342. if (!val) {
  343. videoItem.value?.pause();
  344. }
  345. }
  346. );
  347. return () => (
  348. <div
  349. class={styles.videoWrap}
  350. onClick={() => {
  351. data.speedControl = false;
  352. }}>
  353. <video
  354. style={{ width: '100%', height: '100%' }}
  355. src={item.value.content}
  356. ref={videoRef}
  357. id={videoID}
  358. preload="auto"
  359. playsinline
  360. webkit-playsinline></video>
  361. <div class={styles.videoSection}></div>
  362. <div
  363. class={[styles.controls, data.showBar ? '' : styles.hide]}
  364. onClick={(e: Event) => {
  365. e.stopPropagation();
  366. }}
  367. // onTouchmove={(e: TouchEvent) => {
  368. // emit('close')
  369. // }}
  370. >
  371. <div class={styles.time}>
  372. <div>{getSecondRPM(data.currentTime)}</div>/
  373. <div>{getSecondRPM(data.duration)}</div>
  374. </div>
  375. <div class={styles.slider}>
  376. <Slider
  377. step={0.01}
  378. class={styles.timeProgress}
  379. v-model={data.currentTime}
  380. max={data.duration}
  381. onUpdate:modelValue={val => {
  382. handleChangeTime(val);
  383. }}
  384. />
  385. </div>
  386. <div class={styles.actionSection}>
  387. <div class={styles.actions} onClick={() => emit('close')}>
  388. <div
  389. class={styles.actionBtn}
  390. onClick={(e: any) => {
  391. e.stopPropagation();
  392. onToggleAudio(data.playState === 'pause' ? 'play' : 'pause');
  393. }}>
  394. <img src={data.playState === 'pause' ? iconPlay : iconPause} />
  395. </div>
  396. <div class={styles.actionBtn} onClick={toggleLoop}>
  397. <img src={data.loop ? iconLoopActive : iconLoop} />
  398. </div>
  399. <div class={styles.actionBtn} id={speedBtnId}>
  400. <img src={iconSpeed} />
  401. </div>
  402. </div>
  403. <div class={styles.name}>{item.value.name}</div>
  404. </div>
  405. </div>
  406. {item.value.materialMusicId && (
  407. <div
  408. class={[styles.goPractice, data.showBar ? '' : styles.hide]}
  409. onClick={gotoAccomany}></div>
  410. )}
  411. <div
  412. style={{
  413. display: data.speedControl ? 'block' : 'none'
  414. }}>
  415. <div
  416. class={styles.sliderPopup}
  417. onClick={(e: Event) => {
  418. e.stopPropagation();
  419. }}>
  420. <i
  421. class={styles.iconAdd}
  422. onClick={() => {
  423. if (data.defaultSpeed >= 1.5) {
  424. return;
  425. }
  426. if (videoItem.value) {
  427. data.defaultSpeed = (data.defaultSpeed * 10 + 1) / 10;
  428. videoItem.value.playbackRate(data.defaultSpeed);
  429. }
  430. }}></i>
  431. <Slider
  432. min={0.6}
  433. max={1.5}
  434. step={0.1}
  435. v-model={data.defaultSpeed}
  436. vertical
  437. barHeight={5}
  438. reverse
  439. onChange={() => {
  440. if (videoItem.value) {
  441. videoItem.value.playbackRate(data.defaultSpeed);
  442. }
  443. }}>
  444. {{
  445. button: () => (
  446. <div class={styles.sliderPoint}>
  447. {data.defaultSpeed}
  448. <span>x</span>
  449. </div>
  450. )
  451. }}
  452. </Slider>
  453. <i
  454. class={[styles.iconCut]}
  455. onClick={() => {
  456. if (data.defaultSpeed <= 0.6) {
  457. return;
  458. }
  459. if (videoItem.value) {
  460. data.defaultSpeed = (data.defaultSpeed * 10 - 1) / 10;
  461. videoItem.value.playbackRate(data.defaultSpeed);
  462. }
  463. }}></i>
  464. </div>
  465. </div>
  466. </div>
  467. );
  468. }
  469. });