前言
近期有个直播项目(vue框架做的)。过程中记录一些碰到的坑。
正题
1.安装vue-video-player(我的版本是5.0.2)
npm install vue-video-player --save
// 安装直播插件vue-video-player(注意用npm不要cnpm,会有The "flash" tech is undefined. Skipped browser support check for that tech.坑)
// 注意不要再安装videojs-flash和video.js这两个插件,因为vue-video-player自带有,不然会报错(视频暂时无法播放)
2.局部使用vue-video-player插件(需要的地方(.vue文件中)引入下面文件)
import 'video.js/dist/video-js.css'
import 'vue-video-player/src/custom-theme.css'
import { videoPlayer } from 'vue-video-player'
import 'videojs-flash'
3.组件中引用
使用vue-video-player组件
components: {
videoPlayer
}
4.配置基本的直播一些信息(在data中定义)
playerOptions: {
playbackRates: [0.7, 1.0, 1.5, 2.0], //播放速度
autoplay: true, //如果true,浏览器准备好时开始回放。
controls: true,
muted: false, // 默认情况下将会消除任何音频。
loop: false, // 导致视频一结束就重新开始。
preload: 'auto', // 建议浏览器在<video>加载元素后是否应该开始下载视频数据。
language: 'zh-CN',
aspectRatio: '16:9', // 将播放器置于流畅模式,并在计算播放器的动态大小时使用该值。值应该代表一个比例 - // // 用冒号分隔的两个数字(例如"16:9"或"4:3")
fluid: true, // 当true时,Video.js player将拥有流体大小。换句话说,它将按比例缩放以适应其容器。
techOrder: ['flash', 'html5'],
// flash: {
// hls: { withCredentials: false },
// swf: '../../static/js/video-js.swf' // video-js.swf本地文件(自行百度搜索下载)
// },
html5: { hls: { withCredentials: false } },
sources: [{
type: 'rtmp/flv', // 播放rtmp视频
src: 'rtmp://' // 你直播视频的路径
// type: 'video/mp4', // 播放mp4视频
// src: 'http://ga.xm.gov.cn/xmjx/ztgd/wldspd/202005/P020200508411020251876.mp4'
}],
poster: '', //你的封面地址
width: document.documentElement.clientWidth,
notSupportedMessage: '此视频暂无法播放,请稍后再试', //允许覆盖Video.js无法播放媒体源时显示的默认信息。
controlBar: {
timeDivider: true,
durationDisplay: true,
remainingTimeDisplay: false,
fullscreenToggle: true //全屏按钮
}
}
5.使用
<video-player class="video-player vjs-custom-skin" ref="videoPlayer" :playsinline="true" :options="playerOptions"></video-player>