import { defineComponent, nextTick, onMounted, reactive, toRefs } from 'vue'; // import 'plyr/dist/plyr.css'; // import Plyr from 'plyr'; import { ref } from 'vue'; import TCPlayer from 'tcplayer.js'; import 'tcplayer.js/dist/tcplayer.min.css'; import styles from './index.module.less'; import iconplay from '@views/attend-class/image/icon-pause.png'; import iconpause from '@views/attend-class/image/icon-play.png'; import iconReplay from '@views/attend-class/image/icon-replay.png'; import iconPreviewDownload from '@views/attend-class/image/icon-preivew-download.png'; import iconSpeed from '@views/attend-class/image/icon-speed.png'; import { NSlider, useMessage } from 'naive-ui'; import { saveAs } from 'file-saver'; export default defineComponent({ name: 'video-play', props: { src: { type: String, default: '' }, title: { type: String, default: '' }, poster: { type: String, default: '' }, isEmtry: { type: Boolean, default: false }, isDownload: { type: Boolean, default: false } }, emits: ['loadedmetadata', 'togglePlay', 'ended', 'reset'], setup(props, { emit, expose }) { const message = useMessage(); const { src, poster, isEmtry } = toRefs(props); const videoFroms = reactive({ paused: true, currentTimeNum: 0, currentTime: '00:00', durationNum: 0, duration: '00:00', showBar: true, speedControl: false, speedStyle: { left: '1px' }, defaultSpeed: 1 // 默认速度 }); 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; videoFroms.speedControl = false; }; const onReplay = () => { videoFroms.speedControl = false; if (!videoItem.value) return; videoItem.value.currentTime(0); }; // 切换音频播放 const onToggleVideo = (e?: MouseEvent) => { videoFroms.speedControl = false; e?.stopPropagation(); if (videoFroms.paused) { videoItem.value.play(); videoFroms.paused = false; } else { videoItem.value.pause(); videoFroms.paused = true; } emit('togglePlay', videoFroms.paused); }; // 下载资源 const onDownload = () => { if (!props.src) { message.error('下载失败'); return; } const fileUrl = props.src; // 发起Fetch请求 fetch(fileUrl) .then(response => response.blob()) .then(blob => { saveAs(blob, props.title || new Date().getTime() + ''); }) .catch(() => { message.error('下载失败'); }); }; const __init = () => { if (videoItem.value) { videoItem.value.poster(poster.value); // 封面 videoItem.value.src(isEmtry.value ? '' : src.value); // 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', () => { videoFroms.paused = true; emit('ended'); }); } }; onMounted(() => { videoItem.value = TCPlayer(videoID, { appID: '', controls: false }); // player-container-id 为播放器容器 ID,必须与 html 中一致 __init(); }); expose({ // changePlayBtn, toggleHideControl }); return () => (
{ e.stopPropagation(); emit('reset'); }}>
{ videoFroms.speedControl = !videoFroms.speedControl; }}>
{ videoFroms.speedControl = false; videoItem.value.currentTime(val); videoFroms.currentTimeNum = val; videoFroms.currentTime = timeFormat(Math.round(val || 0)); }} />
{videoFroms.currentTime}
/
{videoFroms.duration}
{props.isDownload && ( )}
{ e.stopPropagation(); }}> { if (videoFroms.defaultSpeed >= 1.5) { return; } if (videoItem.value) { videoFroms.defaultSpeed = (videoFroms.defaultSpeed * 10 + 1) / 10; videoItem.value.playbackRate(videoFroms.defaultSpeed); } }}> { videoFroms.defaultSpeed = val; if (videoItem.value) { videoItem.value.playbackRate(videoFroms.defaultSpeed); } }}> {{ thumb: () => (
{videoFroms.defaultSpeed} x
) }}
{ if (videoFroms.defaultSpeed <= 0.5) { return; } if (videoItem.value) { videoFroms.defaultSpeed = (videoFroms.defaultSpeed * 10 - 1) / 10; videoItem.value.playbackRate(videoFroms.defaultSpeed); } }}>
); } });