video-player.vue 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <template>
  2. <view class="videoPlayer">
  3. <video :id="videoId" ref="myVideo" class="video" height="100%" :autoplay="autoplay" :loop="true" :src="videoUrl" :show-center-play-btn="false" :enable-play-gesture="true" :controls="controls" :vslide-gesture="true" :show-play-btn="false" :show-fullscreen-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. },
  18. data(){
  19. return{
  20. progress: 0, // 进度条进度
  21. isPlaying:null,
  22. isPlay:null,
  23. controls: true,
  24. isFullscreen: null,
  25. imgsrc:defaultConfig.ossImgUrl + '/playVideo/video/ic_play.png',
  26. }
  27. },
  28. onReady: function(res){
  29. console.log(res,8989);
  30. },
  31. watch:{
  32. isPlaying(newVal,oldVal){
  33. if(newVal){
  34. this.isPlay = true
  35. }else{
  36. this.isPlay = false
  37. }
  38. }
  39. },
  40. methods: {
  41. // 播放 &暂停
  42. togglePlay() {
  43. var video = this.$refs.myVideo;
  44. if (video.playing) {
  45. this.isPlaying = true
  46. video.pause();
  47. } else {
  48. this.isPlaying = false
  49. video.play();
  50. }
  51. },
  52. controlstoggle(event){
  53. console.log(event,'event');
  54. this.controls = !this.controls
  55. },
  56. updateProgress() {
  57. // const video = this.$refs.video;
  58. // const duration = video.duration;
  59. // const currentTime = video.currentTime;
  60. // this.progress = (currentTime / duration) * 100;
  61. },
  62. // 当开始/继续播放时触发play事件
  63. play() {
  64. // this.isPlaying = false;
  65. // uni.createVideoContext('myVideo',this).play()
  66. // console.log(uni.createVideoContext('videoId').pageVm,'pla2y');
  67. // this.$refs.progressBar.style.display = 'block'; 2
  68. },
  69. // 当暂停播放时触发 pause 事件
  70. pause() {
  71. // this.isPlaying = true;
  72. // uni.createVideoContext('myVideo',this).pause()
  73. // console.log(uni.createVideoContext('videoId').pageVm,'pease');
  74. // const video = this.$refs.video;
  75. // if (video.currentTime === video.duration) {
  76. // this.$refs.progressBar.style.display = 'none';
  77. // }
  78. },
  79. onFullscreenChange(event){
  80. this.isFullscreen = event.detail.fullScreen;
  81. console.log(event,this.isFullscreen,'ff你看');
  82. }
  83. },
  84. mounted(){
  85. this.isFullscreen = uni.createVideoContext('myVideo')
  86. setTimeout(() => {
  87. console.log(99);
  88. // this.togglePlay()
  89. })
  90. // this.$refs.video.addEventListener('loadedmetadata', () => {
  91. // this.updateProgress(); // 初始化时设置进度为0%
  92. // });
  93. }
  94. }
  95. </script>
  96. <style lang="scss">
  97. .videoPlayer{
  98. width: 100%;
  99. height: 100vh;
  100. position: relative;
  101. .video{
  102. width: 100%;
  103. height: 100%;
  104. }
  105. video::-webkit-media-controls-timeline{
  106. display: none;
  107. }
  108. .uni-video-bar .uni-video-bar-full{
  109. margin-bottom: calc(100rpx + constant(safe-area-inset-bottom)) !important;
  110. margin-bottom: calc(100rpx + env(safe-area-inset-bottom)) !important;
  111. }
  112. .play{
  113. width: 204rpx;
  114. height: 204rpx;
  115. position: absolute;
  116. left: calc(50% - 102rpx);
  117. bottom: 204rpx;
  118. image{
  119. width: 100%;
  120. height: 100%;
  121. }
  122. }
  123. }
  124. </style>