这里是取得部分代码,里面有几个点注意,一个是用两个标签后,视频播放顺序,用变量index来获取,还有就是需要判断视频是否只有一个,循环播放时记得把index++,
@ended="playNextVideo()" 是播放完成后的操作,区分两个标签
:style="{ display: !play ? 'unset' : 'none' }"来控制显示,预加载
<template>
<video
ref="playerSceneRef"
class="video-player"
:style="{ display: play ? 'unset' : 'none' }"
autoplay
@ended="playNextVideo('v')"
></video>
<video
ref="playerSceneRef1"
class="video-player"
:style="{ display: !play ? 'unset' : 'none' }"
autoplay
@ended="playNextVideo('v1')"
></video>
</template>
<script setup lang="ts">
import { ref, onMounted, computed, watch } from 'vue'
import { VideoInfo } from '@src/common/types'
const isAlwaysOnTop = ref<boolean>(false)
const play = ref<boolean>(true)
const { ipcRenderer } = window.require('electron')
const videoWaitingList = ref<VideoInfo[]>([])
const playerSceneRef = ref<HTMLVideoElement>()
const playerSceneRef1 = ref<HTMLVideoElement>()
const curVideoIndex = ref<number>(0)
const parentNodeList = ref([])
const videoIndex = ref(0)
onMounted(() => {
curVideoIndex.value = 0
ipcRenderer.on('message-from-main', (_, message: string, type: string) => {
if (type == 'init') {
parentNodeList.value = JSON.parse(message)
videoIndex.value = 0
handleRandom()
playNextVideo('init')
} else {
insertVideo(JSON.parse(message))
}
})
})
watch(videoIndex, (newVal) => {
if (newVal >= videoWaitingList.value.length) {
videoIndex.value = 0
handleRandom()
}
})
const playNextVideo = (type: string): void => {
if (type == 'v') {
console.log('v-------------', videoIndex.value)
let src = ''
process.platform == 'darwin'
? (src = 'file://' + videoWaitingList.value[videoIndex.value].path)
: (src = videoWaitingList.value[videoIndex.value].path)
videoIndex.value++
console.log('src', src)
playerSceneRef!.value!.src = src
play.value = false
playerSceneRef?.value?.pause()
playerSceneRef1?.value?.play()
} else if (type == 'v1') {
console.log('v1-------------', videoIndex.value)
let src1 = ''
process.platform == 'darwin'
? (src1 = 'file://' + videoWaitingList.value[videoIndex.value].path)
: (src1 = videoWaitingList.value[videoIndex.value].path)
videoIndex.value++
console.log('src1', src1)
playerSceneRef1!.value!.src = src1
play.value = true
playerSceneRef1?.value?.pause()
playerSceneRef?.value?.play()
} else if (type == 'init') {
if (videoWaitingList.value.length > 1) {
let src = ''
process.platform == 'darwin'
? (src = 'file://' + videoWaitingList.value[videoIndex.value].path)
: (src = videoWaitingList.value[videoIndex.value].path)
videoIndex.value++
let src1 = ''
process.platform == 'darwin'
? (src1 = 'file://' + videoWaitingList.value[videoIndex.value].path)
: (src1 = videoWaitingList.value[videoIndex.value].path)
videoIndex.value++
console.log('src,src1', src, src1)
playerSceneRef!.value!.src = src
playerSceneRef1!.value!.src = src1
play.value = true
playerSceneRef?.value?.play()
playerSceneRef1?.value?.pause()
} else {
let src = ''
process.platform == 'darwin'
? (src = 'file://' + videoWaitingList.value[videoIndex.value].path)
: (src = videoWaitingList.value[videoIndex.value].path)
videoIndex.value++
playerSceneRef!.value!.src = src
playerSceneRef1!.value!.src = src
play.value = true
playerSceneRef?.value?.play()
playerSceneRef1?.value?.pause()
}
}
</script>