videoPlayer.vue 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <template>
  2. <view class="videoPlayer">
  3. <video :id="'myVideo'+current" @error="showErr" @loadedmetadata='loaddata' class="video" :controls="true"
  4. style="object-fit: cover !important;" :src="video.src" @ended="next" :show-fullscreen-btn="false"
  5. :poster="video.poster" :muted="current == 0 && ismuted" :autoplay="current == 0" show-mute-btn
  6. @play="openS"></video>
  7. <view class="video err" v-if="is_err&&load=='erro'">
  8. 当前视频链接加载失败,请检查网络设置
  9. </view>
  10. <view class="video err" v-if="is_err&&load=='loding'">
  11. 加载中...
  12. </view>
  13. </view>
  14. </template>
  15. <script>
  16. export default {
  17. props: ['video', 'current'],
  18. name: "videoPlayer",
  19. data() {
  20. return {
  21. play: false,
  22. ismuted: true,
  23. videoContext: '',
  24. is_err: false,
  25. load:"loding",//未加载数据
  26. };
  27. },
  28. onReady() {
  29. this.videoContext = uni.createVideoContext("myVideo", this)
  30. },
  31. mounted() {},
  32. methods: {
  33. click() {
  34. if (this.play === false) {
  35. this.playthis()
  36. } else {
  37. this.pauseVideo()
  38. }
  39. },
  40. playVideo(cu) {
  41. this.videoContext = uni.createVideoContext("myVideo" + cu, this);
  42. // console.log(this.play === false && this.is_err == false, "this.play === false");
  43. if (this.play === false) {
  44. this.videoContext.seek(0)
  45. try {
  46. this.videoContext.play()
  47. // console.log(cu,this.current,'cu');
  48. } catch (e) {
  49. console.log(e, 'err');
  50. }
  51. this.play = true
  52. }
  53. },
  54. openS() {
  55. console.log('播放成功')
  56. this.ismuted = false
  57. this.is_err = false
  58. this.$emit('playVideoSuccess');
  59. },
  60. pauseVideo() {
  61. if (this.play === true) {
  62. this.videoContext.pause()
  63. this.play = false
  64. }
  65. },
  66. playthis() {
  67. let that = this
  68. this.videoContext = uni.createVideoContext("myVideo", this)
  69. if (this.play === false) {
  70. this.videoContext.play()
  71. this.play = true
  72. }
  73. },
  74. next() {
  75. this.play = false
  76. this.$emit('goNext');
  77. },
  78. loaddata(){
  79. this.load='success';
  80. this.$emit('playVideoSuccess');
  81. },
  82. showErr(err) {
  83. console.log(err, '报错',this.current)
  84. const that = this;
  85. if (that.load!=='success') {
  86. that.is_err = true
  87. that.load='erro'
  88. that.$emit('errorVideo');
  89. }
  90. }
  91. }
  92. }
  93. </script>
  94. <style lang="scss">
  95. $bl: 10.8;
  96. .videoPlayer {
  97. width: (1080vw/$bl);
  98. height: (540vw/$bl);
  99. position: relative;
  100. }
  101. .video {
  102. width: 100%;
  103. height: 100%;
  104. }
  105. .err {
  106. text-align: center;
  107. position: absolute;
  108. width: (1080vw/$bl);
  109. height: (540vw/$bl);
  110. font-size: (32vw/$bl);
  111. top: 0;
  112. // letter-spacing: ;
  113. left: 0;
  114. z-index: 99;
  115. line-height: (540vw/$bl);
  116. color: #fff;
  117. background-color: #999;
  118. }
  119. .video:focus {
  120. outline: none;
  121. }
  122. /deep/ .uni-video-video {
  123. object-fit: cover !important;
  124. }
  125. </style>