当前位置:   article > 正文

Vue2使用easyplayer_vue项目中使用easyplay报错

vue项目中使用easyplay报错

说一下easyplayer在vue2中的使用,vue3中没测试,估计应该差不多,大家可自行验证。

安装:

pnpm i @easydarwin/easyplayer

组件封装

习惯性将其封装为单独的组件

  1. <template>
  2. <div class="EasyPlayer">
  3. <easy-player
  4. style="width: 100%;height: 100%;"
  5. @error="restartPlayer"
  6. @ended="restartPlayer"
  7. @play="videoPlay"
  8. ref="EasyPlayerRef"
  9. :videoUrl="url"
  10. :aspect="aspect"
  11. :showEnterprise="false"
  12. :show-custom-button="false"
  13. :alt="alt"
  14. :autoplay="autoplay"
  15. :loop="loop"
  16. :muted="muted"
  17. fluent
  18. stretch
  19. >
  20. </easy-player>
  21. </div>
  22. </template>
  23. <script>
  24. import EasyPlayer from '@easydarwin/easyplayer'
  25. export default {
  26. data() {
  27. return {
  28. timer: null
  29. }
  30. },
  31. components: { EasyPlayer },
  32. props: {
  33. url: {
  34. type: String,
  35. default: ''
  36. },
  37. aspect: {
  38. type: String,
  39. default: '16:9'
  40. },
  41. alt: {
  42. type: String,
  43. default: '无信号'
  44. },
  45. autoplay: {
  46. type: Boolean,
  47. default: true
  48. },
  49. loop: {
  50. type: Boolean,
  51. default: true
  52. },
  53. muted: {
  54. type: Boolean,
  55. default: true
  56. }
  57. },
  58. destroyed() {
  59. if(this.timer) {
  60. clearInterval(this.timer)
  61. this.timer = null
  62. }
  63. },
  64. methods: {
  65. restartPlayer(e) {
  66. console.log(e,'播放异常或播放结束或直播断流------->>>>>>>>>')
  67. this.$refs.EasyPlayerRef.initPlayer() //初始化播放器
  68. this.$emit('pullFlow')
  69. this.timer = setInterval(() => {
  70. this.$refs.EasyPlayerRef.initPlayer() //初始化播放器
  71. this.$emit('pullFlow')
  72. }, 3000)
  73. },
  74. // 播放事件
  75. videoPlay(a) {
  76. // console.log(a,'视频播放------>>')
  77. if(this.timer) {
  78. clearInterval(this.timer)
  79. this.timer = null
  80. }
  81. },
  82. },
  83. }
  84. </script>
  85. <style scoped>
  86. .EasyPlayer {
  87. width: 100%;
  88. height: 100%;
  89. }
  90. /* 阻止单击双击视频全屏或者跳转官网 */
  91. /* /deep/ .vjs-tech {
  92. pointer-events: none!important;
  93. } */
  94. /* /deep/ .video-js.vjs-fullscreen::backdrop,:not(:root):fullscreen::backdrop {
  95. position: fixed!important;
  96. top: 0!important;
  97. left: 0!important;
  98. width: 50%!important;
  99. height: 50%!important;
  100. right: 50%!important;
  101. bottom: 50%!important;
  102. background-color: transparent!important;
  103. }
  104. /deep/ .video-js.vjs-fullscreen .vjs-tech {
  105. position: absolute;
  106. top: 50%;
  107. left: 50%;
  108. width: 50%!important;
  109. height: 50%!important;
  110. transform: translateX(-50%)!important;
  111. }
  112. /deep/ .video-js {
  113. background-color: transparent!important;
  114. } */
  115. </style>

 引入使用

  1. <template>
  2. <div class="container">
  3. <div class="easy-player">
  4. <EasyPlayer
  5. :url="playerUrl"
  6. @pullFlow="pullFlow"
  7. />
  8. </div>
  9. </div>
  10. </template>
  11. <script>
  12. import EasyPlayer from './EasyPlayer/index.vue'
  13. export default {
  14. data() {
  15. return {
  16. playerUrl: 'http://playertest.longtailvideo.com/adaptive/bipbop/gear4/prog_index.m3u8'
  17. }
  18. },
  19. components: { EasyPlayer },
  20. methods:{
  21. // 播放异常 重新拉流
  22. pullFlow() {
  23. // .....
  24. }
  25. },
  26. }
  27. </script>
  28. <style scoped>
  29. .container {
  30. width: 100%;
  31. height: 100%;
  32. padding: 100px 0 0 100px;
  33. box-sizing: border-box;
  34. }
  35. .easy-player {
  36. width: 450px;
  37. height: 300px;
  38. border: 1px solid red;
  39. }
  40. </style>

报错处理

会发现控制台有下面报错 

看到报错不要慌,大家跟着我处理

首先我们安装个插件(注意:不要大于6.0版本的,不然会有bug ,还会有乱七八槽的报错): 

pnpm add copy-webpack-plugin@5.1.2 --save-dev

然后在vue.config.js中: 

  1. const { defineConfig } = require("@vue/cli-service");
  2. const CopyWebpackPlugin = require('copy-webpack-plugin')
  3. module.exports = defineConfig({
  4. // easy-player 相关
  5. configureWebpack: {
  6. plugins: [
  7. new CopyWebpackPlugin([
  8. {
  9. from: 'node_modules/@easydarwin/easyplayer/dist/component/EasyPlayer.swf',
  10. to: './libs/EasyPlayer/'
  11. },
  12. {
  13. from: 'node_modules/@easydarwin/easyplayer/dist/component/crossdomain.xml',
  14. to: './libs/EasyPlayer/'
  15. },
  16. {
  17. from: 'node_modules/@easydarwin/easyplayer/dist/component/EasyPlayer-lib.min.js',
  18. to: './libs/EasyPlayer/'
  19. }
  20. ])
  21. ]
  22. },
  23. transpileDependencies: true,
  24. lintOnSave: false,
  25. productionSourceMap: false
  26. });

配置上之后还没完,还需要在public/index.html 引入EasyPlayer-element.min.js,可以直接在node_modules里找到@easydarwin/easyplayer下的dist/element/EasyPlayer-element.min.js复制到pubilc目录下,还有需要EasyPlayer.wasm同样放到public目录下如下所示:

 

然后在public/index.html下再引入即可 

<script src="/lib/EasyPlayer-element.min.js"></script>

这样就OK了!

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

闽ICP备14008679号