换一种videoList的遍历方式<script> import fs from 'fs' import path from 'path' 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() }) // 自动播放 video.play() this.autoplayInterval = setInterval(() => { this.playNextVideo() }, 5000) // 读取本地文件夹中的视频文件 const videoFolderPath = path.join(__dirname, './src/video/') fs.readdir(videoFolderPath, (err, files) => { if (err) { console.error(err) } else { this.videoList = files.filter(file => { return path.extname(file).toLowerCase() === '.mp4' }).map(file => { return { src: `./src/video/${file}`, name: file } }) } }) }, 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>
时间: 2024-02-14 15:23:34 浏览: 110
这段代码是一个Vue组件,用于全屏播放本地视频文件夹中的MP4视频文件。其中,videoList数组存储了所有的视频文件信息,currentVideoIndex表示当前播放的视频索引,currentVideoSrc是根据当前索引获取的当前视频源地址。在mounted钩子函数中,使用Node.js的fs模块读取本地视频文件夹中所有的MP4视频文件,并将视频信息存储到videoList数组中。在播放下一个视频时,会先将当前视频索引值加一,然后判断是否超出了视频列表的范围,如果超出了,则将索引值重置为0,最后根据当前索引获取当前视频源地址,并调用play()方法开始播放。toggleFullScreen()方法用于全屏切换。
相关问题
fs的遍历文件夹方式运行的时候打不开,帮我修改一下,不用fs的遍历方式去遍历本地文件夹里的视频<template> <div class="video-container"> <video ref="videoPlayer" @ended="playNextVideo" @click="toggleFullScreen" controls></video> </div> </template> <script> import fs from 'fs'; export default { name: 'FullScreenVideoPlayer', data() { return { currentVideoIndex: 0, // 当前播放的视频索引 videoList: [], // 视频列表 autoplayInterval: null // 自动播放的定时器 } }, computed: { currentVideoSrc() { return this.videoList[this.currentVideoIndex] } }, mounted() { const video = this.$refs.videoPlayer; fs.readdir('./src/video', (err, files) => { if (err) throw err; const videoFiles = files.filter(file => file.endsWith('.mp4')); const videoList = videoFiles.map(file => './src/video' + file); this.videoList = videoList; video.src = this.currentVideoSrc; video.play(); this.autoplayInterval = setInterval(() => { this.playNextVideo() }, 5000); }); video.addEventListener('ended', () => { this.playNextVideo() }); }, beforeUnmounted() { // 清除定时器 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>
可以使用 Node.js 内置的 `fs` 模块的同步方法 `readdirSync` 来遍历本地文件夹,修改后的代码如下:
```
<template>
<div class="video-container">
<video ref="videoPlayer" @ended="playNextVideo" @click="toggleFullScreen" controls></video>
</div>
</template>
<script>
import fs from 'fs';
export default {
name: 'FullScreenVideoPlayer',
data() {
return {
currentVideoIndex: 0, // 当前播放的视频索引
videoList: [], // 视频列表
autoplayInterval: null // 自动播放的定时器
}
},
computed: {
currentVideoSrc() {
return this.videoList[this.currentVideoIndex]
}
},
mounted() {
const video = this.$refs.videoPlayer;
const videoFiles = fs.readdirSync('./src/video');
const videoList = videoFiles.filter(file => file.endsWith('.mp4')).map(file => './src/video/' + file);
this.videoList = videoList;
video.src = this.currentVideoSrc;
video.play();
this.autoplayInterval = setInterval(() => {
this.playNextVideo()
}, 5000);
video.addEventListener('ended', () => {
this.playNextVideo()
});
},
beforeUnmounted() {
// 清除定时器
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>
```
请注意,`readdirSync` 是同步方法,执行过程中会阻塞主线程,因此对于大文件夹或者需要实时响应的场景不太适用。如果要遍历大文件夹或者需要实时响应,可以使用 `readdir` 方法异步遍历,或者使用第三方的文件遍历库。
你换一种对videoList的遍历本地文件夹的方式,这个fs浏览器不支持<template> <div class="video-container"> <video ref="videoPlayer" @ended="playNextVideo" @click="toggleFullScreen" controls></video> </div> </template> <script> import fs from 'fs'; export default { name: 'FullScreenVideoPlayer', data() { return { currentVideoIndex: 0, // 当前播放的视频索引 videoList: [], // 视频列表 autoplayInterval: null // 自动播放的定时器 } }, computed: { currentVideoSrc() { return this.videoList[this.currentVideoIndex] } }, mounted() { const video = this.$refs.videoPlayer; // 读取本地文件夹内的视频文件 fs.readdir('./src/video', (err, files) => { if (err) throw err; // 过滤非视频文件 const videoFiles = files.filter(file => file.endsWith('.mp4')) // 将视频文件的路径存入 videoList 数组中 this.videoList = videoFiles.map(file => './src/video/' + file) // 自动播放 video.src = this.currentVideoSrc; video.play(); this.autoplayInterval = setInterval(() => { this.playNextVideo() }, 5000); }); video.addEventListener('ended', () => { this.playNextVideo() }); }, beforeUnmounted() { // 清除定时器 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>
可以使用 Node.js 的 fs 模块来实现遍历本地文件夹,代码如下:
```javascript
const fs = require('fs');
fs.readdir('./src/video', (err, files) => {
if (err) throw err;
const videoFiles = files.filter(file => file.endsWith('.mp4'));
const videoList = videoFiles.map(file => './src/video/' + file);
this.videoList = videoList;
});
```
这段代码与之前的差别在于,使用了 require 导入了 fs 模块,并且使用了 fs.readdir 方法来读取指定文件夹中的文件,然后过滤出以 .mp4 结尾的视频文件,最后将视频文件的路径存入 videoList 数组中。
阅读全文