vue实现flvvideo
Vue 中实现 FLV 视频播放
在 Vue 项目中实现 FLV 视频播放,通常需要使用 flv.js 库。flv.js 是一个纯 JavaScript 实现的 FLV 播放器,支持 HTTP FLV 和 WebSocket FLV 流媒体协议。
安装 flv.js
通过 npm 或 yarn 安装 flv.js 依赖:
npm install flv.js --save
# 或
yarn add flv.js
基本实现步骤
在 Vue 组件中引入 flv.js 并创建播放器实例:
import flvjs from 'flv.js';
export default {
data() {
return {
player: null,
videoElement: null
};
},
mounted() {
this.initPlayer();
},
beforeDestroy() {
this.destroyPlayer();
},
methods: {
initPlayer() {
if (flvjs.isSupported()) {
this.videoElement = this.$refs.videoPlayer;
this.player = flvjs.createPlayer({
type: 'flv',
url: '你的flv视频地址'
});
this.player.attachMediaElement(this.videoElement);
this.player.load();
this.player.play();
}
},
destroyPlayer() {
if (this.player) {
this.player.pause();
this.player.unload();
this.player.detachMediaElement();
this.player.destroy();
this.player = null;
}
}
}
}
模板部分
在模板中添加 video 元素:
<template>
<div>
<video ref="videoPlayer" controls autoplay muted playsinline></video>
</div>
</template>
样式调整
根据需要调整视频播放器的样式:
video {
width: 100%;
max-width: 800px;
background-color: #000;
}
高级配置选项
flv.js 支持多种配置选项,可以根据需要进行调整:
this.player = flvjs.createPlayer({
type: 'flv',
url: '你的flv视频地址',
isLive: true, // 是否为直播流
hasAudio: true, // 是否有音频
hasVideo: true, // 是否有视频
enableStashBuffer: false, // 是否启用缓冲区
stashInitialSize: 128, // 初始缓冲区大小
}, {
enableWorker: true, // 启用WebWorker
enableStashBuffer: false,
autoCleanupSourceBuffer: true,
autoCleanupMaxBackwardDuration: 3,
autoCleanupMinBackwardDuration: 2
});
错误处理
添加错误处理逻辑以增强用户体验:
this.player.on(flvjs.Events.ERROR, (errorType, errorDetail, errorInfo) => {
console.error('播放错误:', errorType, errorDetail, errorInfo);
// 可以在这里添加错误处理逻辑,如显示错误提示等
});
性能优化
对于直播场景,可以添加以下优化:
// 设置低延迟模式
this.player._config.referrerPolicy = 'origin';
this.player._config.loadingTimeout = 1000;
this.player._config.autoCleanupSourceBuffer = true;
注意事项
- 跨域问题:确保视频服务器配置了正确的 CORS 策略
- 浏览器兼容性:flv.js 需要浏览器支持 Media Source Extensions
- 内存管理:及时销毁播放器实例避免内存泄漏
- 移动端适配:添加 playsinline 属性确保在移动端正常播放
完整示例
<template>
<div>
<video ref="videoPlayer" controls autoplay muted playsinline></video>
<button @click="play">播放</button>
<button @click="pause">暂停</button>
</div>
</template>
<script>
import flvjs from 'flv.js';
export default {
data() {
return {
player: null,
videoElement: null
};
},
mounted() {
this.initPlayer();
},
beforeDestroy() {
this.destroyPlayer();
},
methods: {
initPlayer() {
if (flvjs.isSupported()) {
this.videoElement = this.$refs.videoPlayer;
this.player = flvjs.createPlayer({
type: 'flv',
url: 'https://example.com/live.flv',
isLive: true
});
this.player.attachMediaElement(this.videoElement);
this.player.load();
this.player.on(flvjs.Events.ERROR, (errorType, errorDetail, errorInfo) => {
console.error('Error:', errorType, errorDetail, errorInfo);
});
} else {
console.error('Browser not supported');
}
},
play() {
if (this.player) {
this.player.play();
}
},
pause() {
if (this.player) {
this.player.pause();
}
},
destroyPlayer() {
if (this.player) {
this.player.pause();
this.player.unload();
this.player.detachMediaElement();
this.player.destroy();
this.player = null;
}
}
}
}
</script>
<style scoped>
video {
width: 100%;
max-width: 800px;
background-color: #000;
}
</style>






