video-player.vue 4.0 KB

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