vue实现视频播放 video-player

这篇博客介绍了如何在Vue.js应用中实现视频播放功能,使用了vue-video-player插件,详细展示了播放按钮的实现和播放事件处理。在播放页面,根据传入的视频ID动态获取视频源,如果ID为空则使用默认视频。后端通过@RestController接口以流形式提供视频文件。代码中还包含了错误处理和资源释放。这是一个关于前端开发和后端视频流处理的实例。
摘要由CSDN通过智能技术生成

视频上传 这里不多做叙述 今天主要讲视频播放

插件

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();
    }

学无止境,学海无涯 加油,致奋斗的自己

  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Owen_Number_One

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值