| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281 |
- <script>
- export default {
- name: "FullProgress",
- props: {
- playerInstance: {
- type: Object,
- default: () => {
- return {}
- }
- }
- },
- data() {
- return {
- isDrag: false,
- // 总时长
- totalTime: 0,
- // 当前播放时间
- currentTime: 0,
- // 拖拽进度
- progress: 0,
- // 拖拽开始位置
- startX: 0,
- // 拖拽开始时的进度
- startProgress: 0,
- // 定时器
- timer: null,
- // 是否在播放
- isPlay: false,
- }
- },
- mounted() {
- this.$nextTick(() => {
- this.registerEvent();
- })
- },
- methods: {
- // 注册监听事件
- registerEvent() {
- // 初始化完成
- this.playerInstance.on("player_init_over", (totalTime) => {
- this.totalTime = totalTime || 0;
- })
- // 播放
- this.playerInstance.on('player_play', (currentTime) => {
- this.currentTime = currentTime;
- if(currentTime === 0) this.progress = 0;
- this.startTimer();
- this.isPlay = true;
- });
- // 暂停
- this.playerInstance.on('player_pause', () => {
- this.pauseTimer();
- this.isPlay = false;
- });
- // 拖拽结束
- this.playerInstance.on("player_drag_seek", (currentTime) => {
- this.currentTime = currentTime;
- // 计算当时的进度
- this.progress = (currentTime / this.totalTime) * 100;
- })
- },
- handlePlayOrPause() {
- console.log('切换开关')
- if (this.isPlay) {
- this.playerInstance?.pause("c_player_pause");
- } else {
- this.playerInstance?.play("c_player_play");
- }
- },
- startDrag(event) {
- // 阻止默认行为,防止页面滚动等
- event.preventDefault();
- if (event.type === 'touchstart') {
- this.startX = event.touches[0].clientX;
- } else {
- this.startX = event.clientX;
- }
- this.isDrag = true;
- this.onDrag && this.onDrag(true);
- this.pauseTimer(); // 暂停定时器
- this.startProgress = this.progress;
- },
- handleTouch(event) {
- if (!this.isDrag) return;
- let currentX;
- if (event.type === 'touchmove') {
- currentX = event.touches[0].clientX;
- } else {
- currentX = event.clientX;
- }
- const diffX = currentX - this.startX;
- const containerWidth = this.$refs.fullProgress.clientWidth;
- this.progress = Math.min(100, Math.max(0, this.startProgress + (diffX / containerWidth) * 100));
- this.updateCurrentTime();
- },
- // 更新当前时间
- updateCurrentTime() {
- this.currentTime = (this.totalTime * this.progress) / 100;
- },
- // 格式化时间
- formatTime(seconds) {
- const minutes = Math.floor(seconds / 60);
- const secs = Math.floor(seconds % 60);
- return [
- minutes.toString().padStart(2, '0'),
- secs.toString().padStart(2, '0')
- ].join(':');
- },
- // 拖拽结束
- handleTouchEnd() {
- this.onDrag && this.onDrag(false);
- this.isDrag = false;
- this.$emit('touchend', this.currentTime);
- // this.resumeTimer(); // 重新启动定时器
- this.playerInstance?.handleSeekTo(this.currentTime);
- },
- // 开始定时器
- startTimer() {
- if(this.timer) {
- clearInterval(this.timer);
- this.timer = null;
- }
- this.timer = setInterval(() => {
- if (this.currentTime < this.totalTime) {
- this.currentTime++;
- this.progress = (this.currentTime / this.totalTime) * 100;
- } else {
- this.stopTimer();
- }
- }, 1000);
- },
- // 暂停定时器
- pauseTimer() {
- if (this.timer) {
- clearInterval(this.timer);
- this.timer = null;
- }
- },
- // 重新开始定时器
- resumeTimer() {
- console.log("开始重新定时")
- if (!this.timer) {
- this.startTimer();
- }
- },
- // 停止定时器
- stopTimer() {
- if (this.timer) {
- clearInterval(this.timer);
- this.timer = null;
- }
- }
- },
- computed: {
- currentTimeText() {
- return this.formatTime(this.currentTime);
- },
- totalTimeText() {
- return this.formatTime(this.totalTime);
- }
- },
- }
- </script>
- <template>
- <div class="full-progress">
- <div class="progress-row">
- <span class="progress-time">{{ currentTimeText }}</span>
- <div
- @touch.stop="handleTouch"
- @touchend.stop="handleTouchEnd"
- @touchstart.stop="startDrag"
- ref="fullProgress"
- :style="{'--progress-width': progress + '%'}"
- class="progress"></div>
- <span class="progress-time">{{ totalTimeText }}</span>
- </div>
- <div class="tool-box">
- <img v-if="isPlay" src="@/assets/play.png" class="play-icon" @click.stop="handlePlayOrPause" />
- <img v-else src="@/assets/pause.png" class="play-icon" @click.stop="handlePlayOrPause" />
- <div class="right-tool">
- <div class="tool-item">
- <span class="tool-name">倍速</span>
- </div>
- <div class="tool-item">
- <span class="tool-name">1280P</span>
- </div>
- </div>
- </div>
- </div>
- </template>
- <style scoped>
- .full-progress {
- margin: 0 60px 20px;
- }
- .progress-row {
- display: flex;
- align-items: center;
- justify-content: space-between;
- }
- .progress {
- flex: 1;
- height: 2px;
- background: rgba(255, 255, 255, 0.24);
- background: pink;
- border-radius: 2px;
- margin: 0 24px;
- position: relative;
- }
- .progress::before {
- content: "";
- position: absolute;
- border-radius: 2px;
- height: 2px;
- transition: width 0.01s ease-in-out;
- width: var(--progress-width);
- background: #EB3C37;
- }
- .progress::after {
- content: "";
- width: 8px;
- height: 8px;
- background: #FFFFFF;
- box-shadow: 0px 0px 4px 0px rgba(0,0,0,0.5);
- border-radius: 4px;
- position: absolute;
- transition: left 0.01s ease-in-out;
- left: var(--progress-width);
- top: -4px;
- }
- .progress-time {
- font-weight: bold;
- font-size: 12px;
- color: #FFFFFF;
- min-width: 30px;
- }
- .tool-box {
- display: flex;
- align-items: center;
- justify-content: space-between;
- margin-top: 8px;
- }
- .play-icon {
- width: 44px;
- height: 44px;
- }
- .right-tool {
- display: flex;
- align-items: center;
- column-gap: 24px;
- }
- .tool-name {
- font-weight: 600;
- font-size: 15px;
- color: #FFFFFF;
- }
- </style>
|