1.安装video.js依赖npm install --save video.js2.全局引入import Video from 'video.js'import 'video.js/dist/video-js.css'3.使用 (封装组件)实现效果:._vue2 mp3 mp4播放">
当前位置:   article > 正文

Vue 视频音频播放_vue2 mp3 mp4播放

vue2 mp3 mp4播放
  1. <hlsPlayer :rowData="rowData" ref="child" />
  2. <videoPlayer :rowData="rowData" ref="childTwo" />

1.安装video.js依赖

npm install --save video.js

2.全局引入

  1. import Video from 'video.js'
  2. import 'video.js/dist/video-js.css'

3.使用 (封装组件)

实现效果:

 音频组件:

  1. <template>
  2. <Modal
  3. v-model="modals"
  4. width="400"
  5. :styles="{height:'400px',overflow:'hidden'}"
  6. scrollable
  7. footer-hide
  8. closable
  9. :title="titleFrom"
  10. :mask-closable="false"
  11. :z-index="1"
  12. @on-cancel="handleReset"
  13. >
  14. <div>
  15. <aplayer :playerOptions="aOption" v-if="isAudio"></aplayer>
  16. <vplayer :playerOptions="vOption" style="height: 300px;" v-if="!isAudio"></vplayer>
  17. </div>
  18. </Modal>
  19. </template>
  20. <script>
  21. import {aplayer,vplayer} from 'vue-hls-player'
  22. export default {
  23. name: "hlsPlayer",
  24. props:{
  25. rowData:Object,
  26. default:null
  27. },
  28. comments:{aplayer,vplayer},
  29. data(){
  30. return {
  31. modals:false,
  32. isAudio:true,
  33. titleFrom:'媒体',
  34. aOption:{
  35. type: "application/x-mpegURL",
  36. src: 'http://ph1vqefl0.bkt.clouddn.com/hlsnature2',
  37. preload: false,
  38. autoplay: false,
  39. isLoop: false,
  40. controls: 'progress,current,durration',
  41. },
  42. vOption:{
  43. type: "application/x-mpegURL",
  44. src: 'http://ph1vqefl0.bkt.clouddn.com/hlsnature2',
  45. preload: false,
  46. autoplay: false,
  47. isLoop: false,
  48. playsinline: false,
  49. poster: 'https://oimdztrab.qnssl.com/Frp4SyVe5PosdkUKRaE-krjK7B5z',
  50. controls: 'progress,current,durration,volume',
  51. crossOrigin: false
  52. }
  53. }
  54. },
  55. watch:{
  56. rowData(node){
  57. this.aOption['src'] = node.playUrl;
  58. }
  59. },
  60. created() {
  61. },
  62. methods:{
  63. handleReset(){}
  64. }
  65. }
  66. </script>
  67. <style scoped>
  68. </style>

 视频组件:

  1. <template>
  2. <Modal
  3. v-model="modals"
  4. width="400"
  5. :styles="{height:'400px',overflow:'hidden'}"
  6. scrollable
  7. footer-hide
  8. closable
  9. :title="titleFrom"
  10. :mask-closable="false"
  11. :z-index="1"
  12. @on-cancel="handleReset"
  13. >
  14. <div>
  15. <video-player
  16. class="video-player vjs-custom-skin"
  17. ref="videoPlayer"
  18. :playsinline="true"
  19. :options="playerOptions"
  20. ></video-player>
  21. </div>
  22. </Modal>
  23. </template>
  24. <script>
  25. export default {
  26. name: "videoPlayers",
  27. props:{
  28. rowData:Object,
  29. default:null
  30. },
  31. data () {
  32. return {
  33. modals:false,
  34. titleFrom:'媒体',
  35. // 视频播放
  36. playerOptions: {
  37. playbackRates: [0.7, 1.0, 1.5, 2.0],
  38. autoplay: false,
  39. muted: false,
  40. loop: false,
  41. preload: 'auto',
  42. language: 'zh-CN',
  43. aspectRatio: '16:9',
  44. fluid: true,
  45. sources: [{
  46. type: "",
  47. src: 'http://vjs.zencdn.net/v/oceans.mp4'
  48. }],
  49. poster: "",
  50. // width: document.documentElement.clientWidth,
  51. notSupportedMessage: '此视频暂无法播放,请稍后再试',
  52. controlBar: {
  53. timeDivider: true,
  54. durationDisplay: true,
  55. remainingTimeDisplay: false,
  56. fullscreenToggle: true //全屏按钮
  57. }
  58. }
  59. }
  60. },
  61. watch:{
  62. rowData(node){
  63. this.playerOptions['sources'][0]['src'] = node.playUrl;
  64. this.playerOptions['poster'] = node.opusUrl;
  65. }
  66. },
  67. created() {
  68. // this.playerOptions['sources'][0]['src'] = '接口地址';
  69. },
  70. methods:{
  71. handleReset(){}
  72. }
  73. }
  74. </script>
  75. <style scoped>
  76. .video-js .vjs-icon-placeholder {
  77. width: 100%;
  78. height: 100%;
  79. display: block;
  80. }
  81. </style>

4.详细使用教程

(1)引入

  1. import hlsPlayer from "@/components/uplayVideo/hlsPlayer";
  2. import videoPlayer from "@/components/uplayVideo/videoPlayer";

(2)注册

  1. components: {
  2. hlsPlayer,
  3. videoPlayer,
  4. },

(3)页面中使用

(4)绑定事件(在这里我是通过插槽绑定在表格上的)

 为了方便复制也为你准备了现成的CV  但是不要忘了修改请求头 slot: "content",这里需要自己加上  

  1. <template slot-scope="{ row }" slot="content">
  2. <div @click="audioClick(row)">
  3. <span>{{ row.content }} </span>
  4. </div>
  5. </template>

(5)方法函数

  1. audioClick(row) {
  2. this.rowData = {};
  3. if (row.contentType == 6) {
  4. this.rowData.playUrl = row.content;
  5. this.$refs.child.modals = true;
  6. } else if (row.contentType == 1) {
  7. this.rowData.playUrl = row.content;
  8. this.rowData.opusUrl = row.showUrl;
  9. this.$refs.childTwo.modals = true;
  10. }
  11. },

函数详解

this.rowData = {};  是防止组件封装的watch  监听不到

 if (row.contentType == 6)  是判断你当前点击的元素是什么类型,比如作者这里就有mp3 mp4  音频和视频两种资源,这个都是通过你表格的当前行row 这个属性去判断的,如果您发现后端提供给你的数据没有判断类型的东西,请与后端进行沟通

 this.rowData.playUrl = row.content;  是把你当前的视频音频地址传入子组件

 this.$refs.child.modals = true;  是通过父传子的方式控制子组件的显示隐藏的

作者封装组件时默认都是统一做了隐藏

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/79534
推荐阅读
相关标签