video-player.vue 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <template>
  2. <view class="videoPlayer">
  3. <video :id="videoId" ref="myVideo" class="video" height="100%" 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. },
  14. data(){
  15. return{
  16. progress: 0, // 进度条进度
  17. isPlaying:null,
  18. isPlay:null,
  19. controls: true,
  20. isFullscreen: null,
  21. imgsrc:defaultConfig.ossImgUrl + '/playVideo/video/ic_play.png',
  22. }
  23. },
  24. onReady: function(res){
  25. console.log(res,8989);
  26. },
  27. watch:{
  28. isPlaying(newVal,oldVal){
  29. if(newVal){
  30. this.isPlay = true
  31. }else{
  32. this.isPlay = false
  33. }
  34. }
  35. },
  36. methods: {
  37. // 播放 &暂停
  38. togglePlay() {
  39. var video = this.$refs.myVideo;
  40. if (video.playing) {
  41. this.isPlaying = true
  42. video.pause();
  43. } else {
  44. this.isPlaying = false
  45. video.play();
  46. }
  47. },
  48. controlstoggle(event){
  49. console.log(event,'event');
  50. this.controls = !this.controls
  51. },
  52. updateProgress() {
  53. // const video = this.$refs.video;
  54. // const duration = video.duration;
  55. // const currentTime = video.currentTime;
  56. // this.progress = (currentTime / duration) * 100;
  57. },
  58. // 当开始/继续播放时触发play事件
  59. play() {
  60. // this.isPlaying = false;
  61. // uni.createVideoContext('myVideo',this).play()
  62. // console.log(uni.createVideoContext('videoId').pageVm,'pla2y');
  63. // this.$refs.progressBar.style.display = 'block'; 2
  64. },
  65. // 当暂停播放时触发 pause 事件
  66. pause() {
  67. // this.isPlaying = true;
  68. // uni.createVideoContext('myVideo',this).pause()
  69. // console.log(uni.createVideoContext('videoId').pageVm,'pease');
  70. // const video = this.$refs.video;
  71. // if (video.currentTime === video.duration) {
  72. // this.$refs.progressBar.style.display = 'none';
  73. // }
  74. },
  75. onFullscreenChange(event){
  76. this.isFullscreen = event.detail.fullScreen;
  77. console.log(event,this.isFullscreen,'ff你看');
  78. }
  79. },
  80. mounted(){
  81. setTimeout(() => {
  82. console.log(uni.createVideoContext('myVideo'),'video');
  83. })
  84. this.isFullscreen = uni.createVideoContext('myVideo')
  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>