video-player.vue 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <template>
  2. <view class="videoPlayer">
  3. <video :id="videoId" ref="myVideo" class="video" height="100%" :autoplay="autoplay" :initial-time="initialTime" :loop="true" :src="videoUrl" :show-center-play-btn="false" :enable-play-gesture="true" :controls="controls" :vslide-gesture="true" :show-play-btn="false" @controlstoggle="controlstoggle($event)" @timeupdate="updateProgress" @play="play" @pause="pause" @fullscreenchange="onFullscreenChange" @click="togglePlay"></video>
  4. <view v-if="isPlay" class="play" @click="togglePlay"><image :src="imgsrc"></image></view>
  5. </view>
  6. </template>
  7. <script>
  8. import defaultConfig from '@/config/default.js'
  9. export default {
  10. props:{
  11. videoUrl: String,
  12. videoId: String,
  13. autoplay: {
  14. type: Boolean,
  15. default: true
  16. },
  17. initialTime:{
  18. type: [ String,Number ],
  19. default: ''
  20. },
  21. selanShow:{
  22. type: Boolean,
  23. default: true
  24. }
  25. },
  26. data(){
  27. return{
  28. progress: 0, // 进度条进度
  29. isPlay:null,
  30. controls: true,
  31. isFullscreen: null,
  32. imgsrc:defaultConfig.ossImgUrl + '/playVideo/video/ic_play.png',
  33. }
  34. },
  35. onReady: function(res){
  36. console.log(res,8989);
  37. },
  38. watch:{
  39. },
  40. methods: {
  41. // 播放 &暂停
  42. togglePlay() {
  43. var video = this.$refs.myVideo;
  44. if (video.playing) {
  45. // this.isPlay = true
  46. video.pause();
  47. } else {
  48. // this.isPlay = false
  49. video.play();
  50. }
  51. },
  52. // 控制播放按钮
  53. controlstoggle(event){
  54. console.log(event,'event');
  55. this.controls = !this.controls
  56. },
  57. updateProgress(event) {
  58. // 通过自定义事件传递当前时间
  59. this.$emit('timeUpdate', event.target.currentTime);
  60. },
  61. // 当开始/继续播放时触发play事件
  62. play() {
  63. this.isPlay = false
  64. // uni.createVideoContext('myVideo',this).play()
  65. // console.log(uni.createVideoContext('videoId').pageVm,'pla2y');
  66. // this.$refs.progressBar.style.display = 'block'; 2
  67. },
  68. // 当暂停播放时触发 pause 事件
  69. pause() {
  70. this.isPlay = true
  71. // uni.createVideoContext('myVideo',this).pause()
  72. // console.log(uni.createVideoContext('videoId').pageVm,'pease');
  73. // const video = this.$refs.video;
  74. // if (video.currentTime === video.duration) {
  75. // this.$refs.progressBar.style.display = 'none';
  76. // }
  77. },
  78. onFullscreenChange(event){
  79. this.isFullscreen = event.detail.fullScreen;
  80. if(this.isFullscreen){
  81. this.$emit('chectSelanShow',false)
  82. }else{
  83. this.$emit('chectSelanShow',true)
  84. }
  85. console.log(event,this.isFullscreen,'ff你看');
  86. }
  87. },
  88. mounted(){
  89. this.isFullscreen = uni.createVideoContext('myVideo')
  90. setTimeout(() => {
  91. console.log(99);
  92. // this.togglePlay()
  93. })
  94. // this.$refs.video.addEventListener('loadedmetadata', () => {
  95. // this.updateProgress(); // 初始化时设置进度为0%
  96. // });
  97. }
  98. }
  99. </script>
  100. <style lang="scss">
  101. .videoPlayer{
  102. width: 100%;
  103. height: 100vh;
  104. position: relative;
  105. .video{
  106. width: 100%;
  107. height: 100%;
  108. }
  109. video::-webkit-media-controls-timeline{
  110. display: none;
  111. }
  112. .uni-video-bar .uni-video-bar-full{
  113. margin-bottom: calc(100rpx + constant(safe-area-inset-bottom)) !important;
  114. margin-bottom: calc(100rpx + env(safe-area-inset-bottom)) !important;
  115. }
  116. .play{
  117. width: 204rpx;
  118. height: 204rpx;
  119. position: absolute;
  120. left: calc(50% - 102rpx);
  121. bottom: 204rpx;
  122. image{
  123. width: 100%;
  124. height: 100%;
  125. }
  126. }
  127. }
  128. </style>