<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 video.addEventListener('ended', () => { this.playNextVideo() }) // 遍历本地视频文件夹,添加到videoList数组中 const videoFolder = './src/video' // 视频文件夹的路径 fs.readdirSync(videoFolder).forEach(file => { if (file.endsWith('.mp4')) { // 只添加后缀为.mp4的视频文件 this.videoList.push({ src: `${videoFolder}/${file}` }) } }) // 自动播放 video.play() this.autoplayInterval = setInterval(() => { this.playNextVideo() }, 5000) }, 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>这串代码为什么运行不了

时间: 2024-03-30 20:33:41 浏览: 12
这段代码中使用了Node.js的fs模块,但是它只能在服务端运行,无法在浏览器中使用。因此,需要在浏览器中使用视频时,需要将视频文件的路径硬编码到videoList数组中,而不是使用fs模块动态读取本地视频文件夹中的视频文件。另外,beforeUnmounted钩子函数应该是beforeUnmounted而不是beforeUnmounted。同时,需要在组件中引入fs模块,这也是不可行的。
相关问题

我想在vue3环境下在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>

你可以使用 Node.js 的 fs 模块来读取本地文件夹内的视频文件,然后将视频文件的路径存入 videoList 数组中。具体实现代码如下: ``` <template> <div class="video-container"> <video ref="videoPlayer" @ended="playNextVideo" @click="toggleFullScreen" controls></video> </div> </template> <script> const fs = require('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('/path/to/folder', (err, files) => { if (err) throw err; // 过滤非视频文件 const videoFiles = files.filter(file => file.endsWith('.mp4') || file.endsWith('.avi')) // 将视频文件的路径存入 videoList 数组中 this.videoList = videoFiles.map(file => `/path/to/folder/${file}`) // 自动播放 video.play(); this.autoplayInterval = setInterval(() => { this.playNextVideo() }, 5000); }); video.addEventListener('ended', () => { this.playNextVideo() }); }, 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> ``` 请将 `/path/to/folder` 替换为实际视频文件夹的路径。

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>

你可以使用之前代码中获取视频列表的方法,将获取到的视频路径动态添加到 `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` 数组中,然后在页面中渲染出视频列表。

相关推荐

fs的遍历文件夹方式运行的时候打不开,帮我修改一下,不用fs的遍历方式去遍历本地文件夹里的视频<template> </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>

<template> </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>这里面视频播放的路径修改要在哪里改

这个遍历后的路径要改哪些才能播放啊<template> </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') || file.endsWith('.avi')) // 将视频文件的路径存入 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() }); }, 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>

为什么放不了本地路径下的视频啊。帮我看看遍历用的有没有什么问题<template> </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() }); }, 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>

最新推荐

recommend-type

华为OD机试D卷 - 用连续自然数之和来表达整数 - 免费看解析和代码.html

私信博主免费获取真题解析以及代码
recommend-type

Screenshot_2024-05-10-20-21-01-857_com.chaoxing.mobile.jpg

Screenshot_2024-05-10-20-21-01-857_com.chaoxing.mobile.jpg
recommend-type

数字图像处理|Matlab-频域增强实验-彩色图像的频域滤波.zip

数字图像处理|Matlab-频域增强实验-彩色图像的频域滤波.zip
recommend-type

2024-2030中国定向转向膜市场现状研究分析与发展前景预测报告.docx

2024-2030中国定向转向膜市场现状研究分析与发展前景预测报告
recommend-type

开源工时填报管理系统安装包

开源工时填报管理系统安装包
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

spring添加xml配置文件

1. 创建一个新的Spring配置文件,例如"applicationContext.xml"。 2. 在文件头部添加XML命名空间和schema定义,如下所示: ``` <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。