06 整合阿里云播放器.png

一、后端获取播放凭证

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. }


二、前端播放器整合

1、点击播放超链接

course/_id.vue
修改课时目录超链接
046740b7-dccb-4a50-9502-f33f4a589a77.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,从后端获取播放凭证

  1. import request from '@/utils/request'
  2. const api_name = '/vod/video'
  3. export default {
  4. getPlayAuth(vid) {
  5. return request({
  6. url: `${api_name}/get-play-auth/${vid}`,
  7. method: 'get'
  8. })
  9. }
  10. }

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样式

  1. <template>
  2. <div>
  3. <!-- 阿里云视频播放器样式 -->
  4. <link rel="stylesheet" href="https://g.alicdn.com/de/prismplayer/2.8.1/skins/default/aliplayer-min.css" >
  5. <!-- 阿里云视频播放器脚本 -->
  6. <script charset="utf-8" type="text/javascript" src="https://g.alicdn.com/de/prismplayer/2.8.1/aliplayer-min.js" />
  7. <!-- 定义播放器dom -->
  8. <div id="J_prismPlayer" class="prism-player" />
  9. </div>
  10. </template>

(2)获取播放凭证

  1. <script>
  2. import vod from '@/api/vod'
  3. export default {
  4. layout: 'video',//应用video布局
  5. asyncData({ params, error }) {
  6. return vod.getPlayAuth(params.vid).then(response => {
  7. // console.log(response.data.data)
  8. return {
  9. vid: params.vid,
  10. playAuth: response.data.data.playAuth
  11. }
  12. })
  13. }
  14. }
  15. </script>

(3)创建播放器

  1. /**
  2. * 页面渲染完成时:此时js脚本已加载,Aliplayer已定义,可以使用
  3. * 如果在created生命周期函数中使用,Aliplayer is not defined错误
  4. */
  5. mounted() {
  6. new Aliplayer({
  7. id: 'J_prismPlayer',
  8. vid: this.vid, // 视频id
  9. playauth: this.playAuth, // 播放凭证
  10. encryptType: '1', // 如果播放加密视频,则需设置encryptType=1,非加密视频无需设置此项
  11. width: '100%',
  12. height: '500px'
  13. }, function(player) {
  14. console.log('播放器创建成功')
  15. })
  16. }

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

  1. // 以下可选设置
  2. cover: 'http://guli.shop/photo/banner/1525939573202.jpg', // 封面
  3. qualitySort: 'asc', // 清晰度排序
  4. mediaType: 'video', // 返回音频还是视频
  5. autoplay: false, // 自动播放
  6. isLive: false, // 直播
  7. rePlay: false, // 循环播放
  8. preload: true,
  9. controlBarVisibility: 'hover', // 控制条的显示方式:鼠标悬停
  10. useH5Prism: true, // 播放器类型:html5

6、加入播放组件

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