| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185 |
- import { defineComponent, nextTick, onMounted, reactive, toRefs } from 'vue';
- import TCPlayer from 'tcplayer.js';
- import 'tcplayer.js/dist/tcplayer.min.css';
- // import 'plyr/dist/plyr.css';
- // import Plyr from 'plyr';
- import { ref } from 'vue';
- import styles from './video.module.less';
- import iconplay from '../image/icon-pause.png';
- import iconpause from '../image/icon-play.png';
- import iconReplay from '../image/icon-replay.png';
- import { NSlider } from 'naive-ui';
- export default defineComponent({
- name: 'video-play',
- props: {
- item: {
- type: Object,
- default: () => {
- return {};
- }
- },
- isEmtry: {
- type: Boolean,
- default: false
- }
- },
- emits: ['loadedmetadata', 'togglePlay', 'ended', 'reset'],
- setup(props, { emit, expose }) {
- const { item, isEmtry } = toRefs(props);
- const videoFroms = reactive({
- paused: true,
- currentTimeNum: 0,
- currentTime: '00:00',
- durationNum: 0,
- duration: '00:00',
- showBar: true
- });
- const videoRef = ref();
- const videoItem = ref();
- const videoID = 'video' + Date.now() + Math.floor(Math.random() * 100);
- // 对时间进行格式化
- const timeFormat = (num: number) => {
- if (num > 0) {
- const m = Math.floor(num / 60);
- const s = num % 60;
- return (m < 10 ? '0' + m : m) + ':' + (s < 10 ? '0' + s : s);
- } else {
- return '00:00';
- }
- };
- //
- const toggleHideControl = (isShow: false) => {
- videoFroms.showBar = isShow;
- };
- const onReplay = () => {
- if (!videoItem.value) return;
- videoItem.value.currentTime(0);
- };
- // 切换音频播放
- const onToggleVideo = (e?: MouseEvent) => {
- e?.stopPropagation();
- if (videoFroms.paused) {
- videoItem.value.play();
- videoFroms.paused = false;
- } else {
- videoItem.value.pause();
- videoFroms.paused = true;
- }
- emit('togglePlay', videoFroms.paused);
- };
- onMounted(() => {
- videoItem.value = TCPlayer(videoID, {
- appID: '',
- controls: false
- }); // player-container-id 为播放器容器 ID,必须与 html 中一致
- if (videoItem.value) {
- videoItem.value.src(isEmtry.value ? '' : item.value.content); // url 播放地址
- // 初步加载时
- videoItem.value.one('loadedmetadata', () => {
- console.log(' Loading metadata');
- // 获取时长
- videoFroms.duration = timeFormat(
- Math.round(videoItem.value.duration())
- );
- videoFroms.durationNum = videoItem.value.duration();
- emit('loadedmetadata', videoItem.value);
- });
- // 视频播放时加载
- videoItem.value.on('timeupdate', () => {
- videoFroms.currentTime = timeFormat(
- Math.round(videoItem.value?.currentTime() || 0)
- );
- videoFroms.currentTimeNum = videoItem.value.currentTime();
- });
- // 视频播放结束
- videoItem.value.on('ended', () => {
- emit('ended');
- });
- }
- });
- expose({
- // changePlayBtn,
- toggleHideControl
- });
- return () => (
- <div class={styles.videoWrap}>
- <video
- style={{ width: '100%', height: '100%' }}
- src={isEmtry.value ? '' : item.value.content}
- ref={videoRef}
- id={videoID}
- preload="auto"
- playsinline
- webkit-playsinline></video>
- <div
- class={[
- styles.controls,
- videoFroms.showBar ? '' : styles.sectionAnimate
- ]}
- onClick={(e: MouseEvent) => {
- e.stopPropagation();
- emit('reset');
- }}>
- <div class={styles.actions}>
- <div class={styles.actionWrap}>
- <button class={styles.actionBtn} onClick={onToggleVideo}>
- {videoFroms.paused ? (
- <img class={styles.playIcon} src={iconplay} />
- ) : (
- <img class={styles.playIcon} src={iconpause} />
- )}
- </button>
- </div>
- <div class={styles.time}>
- <div
- class="plyr__time plyr__time--current"
- aria-label="Current time">
- {videoFroms.currentTime}
- </div>
- <span class={styles.line}>/</span>
- <div
- class="plyr__time plyr__time--duration"
- aria-label="Duration">
- {videoFroms.duration}
- </div>
- </div>
- </div>
- <div class={styles.slider}>
- <NSlider
- value={videoFroms.currentTimeNum}
- step={0.01}
- max={videoFroms.durationNum}
- tooltip={false}
- onUpdate:value={(val: number) => {
- videoItem.value.currentTime = val;
- videoFroms.currentTimeNum = val;
- videoFroms.currentTime = timeFormat(Math.round(val || 0));
- }}
- />
- </div>
- <div class={styles.actions}>
- <div class={styles.actionWrap}>
- <button class={styles.iconReplay} onClick={onReplay}>
- <img src={iconReplay} />
- </button>
- </div>
- </div>
- </div>
- </div>
- );
- }
- });
|