vue3下src下面有个video的文件夹,把这个文件夹里mp4格式的视频遍历到videoList里<template> <div class="video-container"> <video ref="videoPlayer" @ended="playNextVideo" @click="toggleFullScreen" controls></video> </div> </template> <script> export default { name: 'FullScreenVideoPlayer', data() { return { currentVideoIndex: 0, // 当前播放的视频索引 videoList: [ // 视频列表 { src: '/src/video/666.mp4' }, { src: '/src/video/777.mp4' }, ], autoplayInterval: null // 自动播放的定时器 } }, computed: { currentVideoSrc() { return this.videoList[this.currentVideoIndex].src } }, mounted() { const video = this.$refs.videoPlayer video.addEventListener('ended', () => { this.playNextVideo() }) // 自动播放 video.play() this.autoplayInterval = setInterval(() => { this.playNextVideo() }, 5000) }, beforeUnmount() { // 清除定时器 clearInterval(this.autoplayInterval) }, methods: { playNextVideo() { const video = this.$refs.videoPlayer this.currentVideoIndex++ if (this.currentVideoIndex >= this.videoList.length) { this.currentVideoIndex = 0 } video.src = this.currentVideoSrc video.play() }, toggleFullScreen() { const video = this.$refs.videoPlayer if (document.fullscreenElement) { document.exitFullscreen() } else { video.requestFullscreen() } } } } </script> <style scoped> .video-container { width: 100%; height: 100%; } video { width: 100%; height: 100%; object-fit: contain; } </style>
时间: 2024-02-14 11:14:42 浏览: 134
你可以使用之前代码中获取视频列表的方法,将获取到的视频路径动态添加到 `videoList` 数组中。修改后的代码如下:
```html
<template>
<div class="video-container">
<video ref="videoPlayer" @ended="playNextVideo" @click="toggleFullScreen" controls></video>
</div>
</template>
<script>
// 引入 fs 模块
const fs = require('fs');
export default {
name: 'FullScreenVideoPlayer',
data() {
return {
currentVideoIndex: 0, // 当前播放的视频索引
videoList: [], // 视频列表,初始化为空数组
autoplayInterval: null // 自动播放的定时器
}
},
computed: {
currentVideoSrc() {
return this.videoList[this.currentVideoIndex].src
}
},
mounted() {
const video = this.$refs.videoPlayer;
video.addEventListener('ended', () => {
this.playNextVideo()
});
// 获取视频列表
const videoDirPath = './src/video';
const fileNames = fs.readdirSync(videoDirPath);
const mp4FileNames = fileNames.filter(fileName => fileName.endsWith('.mp4'));
this.videoList = mp4FileNames.map(mp4FileName => {
return {
src: `${videoDirPath}/${mp4FileName}`
};
});
// 自动播放
video.play();
this.autoplayInterval = setInterval(() => {
this.playNextVideo()
}, 5000);
},
beforeUnmount() {
// 清除定时器
clearInterval(this.autoplayInterval)
},
methods: {
playNextVideo() {
const video = this.$refs.videoPlayer;
this.currentVideoIndex++;
if (this.currentVideoIndex >= this.videoList.length) {
this.currentVideoIndex = 0
}
video.src = this.currentVideoSrc;
video.play()
},
toggleFullScreen() {
const video = this.$refs.videoPlayer;
if (document.fullscreenElement) {
document.exitFullscreen()
} else {
video.requestFullscreen()
}
}
}
}
</script>
<style scoped>
.video-container {
width: 100%;
height: 100%;
}
video {
width: 100%;
height: 100%;
object-fit: contain;
}
</style>
```
这样就可以将视频列表动态遍历到 `videoList` 数组中,然后在页面中渲染出视频列表。
阅读全文