一、后端获取播放凭证

1、VideoController

service-vod微服务中创建 VideoController.java
controller中创建 getVideoPlayAuth 接口方法

  1. package com.guli.vod.controller;
  2. @Api(description="阿里云视频点播微服务")
  3. @CrossOrigin //跨域
  4. @RestController
  5. @RequestMapping("/vod/video")
  6. public class VideoController {
  7. @GetMapping("get-play-auth/{videoId}")
  8. public R getVideoPlayAuth(@PathVariable("videoId") String videoId) throws Exception {
  9. //获取阿里云存储相关常量
  10. String accessKeyId = ConstantPropertiesUtil.ACCESS_KEY_ID;
  11. String accessKeySecret = ConstantPropertiesUtil.ACCESS_KEY_SECRET;
  12. //初始化
  13. DefaultAcsClient client = AliyunVodSDKUtils.initVodClient(accessKeyId, accessKeySecret);
  14. //请求
  15. GetVideoPlayAuthRequest request = new GetVideoPlayAuthRequest();
  16. request.setVideoId(videoId);
  17. //响应
  18. GetVideoPlayAuthResponse response = client.getAcsResponse(request);
  19. //得到播放凭证
  20. String playAuth = response.getPlayAuth();
  21. //返回结果
  22. return R.ok().message("获取凭证成功").data("playAuth", playAuth);
  23. }
  24. }

2、Swagger测试

**

二、前端播放器整合

1、点击播放超链接

course/_id.vue
修改课时目录超链接
image.png

  1. <a
  2. :href="'/player/'+video.videoSourceId"
  3. :title="video.title"
  4. target="_blank">

2、layout

因为播放器的布局和其他页面的基本布局不一致,因此创建新的布局容器 layouts/video.vue

  1. <template>
  2. <div class="guli-player">
  3. <div class="head">
  4. <a href="#" title="谷粒学院">
  5. <img class="logo" src="~/assets/img/logo.png" lt="谷粒学院">
  6. </a></div>
  7. <div class="body">
  8. <div class="content"><nuxt/></div>
  9. </div>
  10. </div>
  11. </template>
  12. <script>
  13. export default {}
  14. </script>
  15. <style>
  16. html,body{
  17. height:100%;
  18. }
  19. </style>
  20. <style scoped>
  21. .head {
  22. height: 50px;
  23. position: absolute;
  24. top: 0;
  25. left: 0;
  26. width: 100%;
  27. }
  28. .head .logo{
  29. height: 50px;
  30. margin-left: 10px;
  31. }
  32. .body {
  33. position: absolute;
  34. top: 50px;
  35. left: 0;
  36. right: 0;
  37. bottom: 0;
  38. overflow: hidden;
  39. }
  40. </style>

3、api

创建api模块 api/vod.js,从后端获取播放凭证

import request from '@/utils/request'
const api_name = '/vod/video'
export default {


  getPlayAuth(vid) {
    return request({
      url: `${api_name}/get-play-auth/${vid}`,
      method: 'get'
    })
  }



}

4、播放组件相关文档

集成文档:https://help.aliyun.com/document_detail/51991.html?spm=a2c4g.11186623.2.39.478e192b8VSdEn
在线配置:https://player.alicdn.com/aliplayer/setting/setting.html
功能展示:https://player.alicdn.com/aliplayer/presentation/index.html

5、创建播放页面

创建 pages/player/_vid.vue
(1)引入播放器js库和css样式

<template>
  <div>


    <!-- 阿里云视频播放器样式 -->
    <link rel="stylesheet" href="https://g.alicdn.com/de/prismplayer/2.8.1/skins/default/aliplayer-min.css" >
    <!-- 阿里云视频播放器脚本 -->
    <script charset="utf-8" type="text/javascript" src="https://g.alicdn.com/de/prismplayer/2.8.1/aliplayer-min.js" />


    <!-- 定义播放器dom -->
    <div id="J_prismPlayer" class="prism-player" />
  </div>

</template>

(2)获取播放凭证

<script>
import vod from '@/api/vod'
export default {

  layout: 'video',//应用video布局
  asyncData({ params, error }) {
    return vod.getPlayAuth(params.vid).then(response => {
      // console.log(response.data.data)
      return {
        vid: params.vid,
        playAuth: response.data.data.playAuth
      }
    })
  }

}
</script>

(3)创建播放器

 /**
 * 页面渲染完成时:此时js脚本已加载,Aliplayer已定义,可以使用
 * 如果在created生命周期函数中使用,Aliplayer is not defined错误
 */
mounted() {

    new Aliplayer({
        id: 'J_prismPlayer',
        vid: this.vid, // 视频id
        playauth: this.playAuth, // 播放凭证
        encryptType: '1', // 如果播放加密视频,则需设置encryptType=1,非加密视频无需设置此项
        width: '100%',
        height: '500px'
    }, function(player) {
        console.log('播放器创建成功')
    })

}

(4)其他常见的可选配置


// 以下可选设置
cover: 'http://guli.shop/photo/banner/1525939573202.jpg', // 封面
qualitySort: 'asc', // 清晰度排序

mediaType: 'video', // 返回音频还是视频
autoplay: false, // 自动播放
isLive: false, // 直播
rePlay: false, // 循环播放
preload: true,
controlBarVisibility: 'hover', // 控制条的显示方式:鼠标悬停
useH5Prism: true, // 播放器类型:html5

6、加入播放组件

功能展示:https://player.alicdn.com/aliplayer/presentation/index.html