视频上传 这里不多做叙述 今天主要讲视频播放
插件
cnpm install --save video.js
cnpm install --save vue-video-player
播放按钮
<el-button
size="mini"
type="text"
icon="el-icon-video-play"
@click="handlePlay(scope.row)"
>播放
</el-button>
/** 播放按钮操作 */
handlePlay(row) {
this.$router.push({
path: '/pxxj/play',
name: 'bfsp',
params: {
row
}
})
}
就是在这个页面跳到内阁play 播放页面 播放页面的代码如下
<template>
<div>
<video-player class="video-player vjs-custom-skin" ref="videoPlayer" :playsline="false"
:options="playerOptions"></video-player>
<!-- <div v-for="i in 10" :key="i">-->
<!-- <video-player class="video-player vjs-custom-skin" ref="videoPlayer" :playsline="false" :options="playerOptions"></video-player>-->
<!-- <p>{{'视频' + i}}</p>-->
<!-- </div>-->
</div>
</template>
<script>
import VideoPlayer from 'vue-video-player'
import Vue from 'vue'
import 'video.js/dist/video-js.css'
import 'vue-video-player/src/custom-theme.css'
Vue.use(VideoPlayer)
export default {
name: "play",
created() {
this.row = this.$route.params.row;
console.log("row",this.$route.params.row);
// 初始化视频
{
this.num = this.row.videoFileid;
console.log("this.num",this.num)
if (this.num === null) {
this.url = 'http://img.ksbbs.com/asset/Mon_1703/05cacb4e02f9d9e.mp4'
this.playerOptions['sources'][0]['src'] = this.url;
} else {
this.url = process.env.VUE_APP_BASE_API + "/common/video/" + this.num + "/download";
this.playerOptions['sources'][0]['src'] = this.url;
console.log(this.url)
}
return this.num;
}
},
data() {
return {
row: {},
playerOptions: {
playbackRates: [0.7, 1.0, 1.5, 2.0], // 播放速度
autoplay: false, // 如果true,浏览器准备好时开始回放。
muted: false, // 默认情况下将会消除任何音频。
loop: false, // 导致视频一结束就重新开始。
preload: 'auto', // 建议浏览器在<video>加载元素后是否应该开始下载视频数据。auto浏览器选择最佳行为,立即开始加载视频(如果浏览器支持)
language: 'zh-CN',
aspectRatio: '16:9', // 将播放器置于流畅模式,并在计算播放器的动态大小时使用该值。值应该代表一个比例 - 用冒号分隔的两个数字(例如"16:9"或"4:3")
fluid: true, // 当true时,Video.js player将拥有流体大小。换句话说,它将按比例缩放以适应其容器。
sources: [{
type: 'video/mp4', // 这里的种类支持很多种:基本视频格式、直播、流媒体等,具体可以参看git网址项目
src: require('./SpringBoot第三节创建SpringBoot项目以及启动器讲解.mp4') // url地址
// src: require('') // url地址
}],
poster: require('./111.jpg'), // 你的封面地址
width: document.documentElement.clientWidth, // 播放器宽度
height: document.documentElement.clientHeight,
notSupportedMessage: '此视频暂无法播放,请稍后再试', // 允许覆盖Video.js无法播放媒体源时显示的默认信息。
controlBar: {
timeDivider: true,
durationDisplay: true,
remainingTimeDisplay: false,
fullscreenToggle: true // 全屏按钮
}
}
}
}
}
</script>
<style scoped>
.video-js .vjs-icon-placeholder {
width: 80%;
height: 80%;
display: block;
}
/* .video-player {
width: 50%;
} */
</style>
这个后台地址是我自己写的 也很简单 就是把文件取出来然后以流的形式输出
this.url = process.env.VUE_APP_BASE_API + "/common/video/" + this.num + "/download";
后端代码简单如下 第一步就是取文件 你们按照你们自己的来取就行 我是存在mongdb里了 你们也可以存在文件夹下 然后来取 都可以的 不懂得自行百度或者在下面问
@RequestMapping(value = "/common/video/{fileId}/{type}")
public void video(@PathVariable("fileId") String fileId, @PathVariable("type") String type, HttpServletRequest request, HttpServletResponse response) throws Exception {
BizShareFile shareFile = shareFileAPI.getBizShareFile(fileId);
request.setCharacterEncoding("UTF-8");
response.setContentType("video/mpeg4");
OutputStream out = response.getOutputStream();
GridFsResource resource = mongoDbUtil.downloadFile(shareFile.getFilepath());
StreamUtils.copy(resource.getInputStream(), out);
InputStream in = resource.getInputStream();
byte[] b = new byte[1024*1024];
while ((in.read(b)) != -1) {
out.write(b);
}
out.flush();
in.close();
out.close();
}
学无止境,学海无涯 加油,致奋斗的自己