fullVideo.nvue 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. <template>
  2. <view :style="'width: '+ windowWidth +'px; height: '+ windowHeight +'px;'" class="container" >
  3. <video
  4. id="video"
  5. :src="videoUrl"
  6. :controls="true"
  7. :autoplay="true"
  8. @timeupdate="onTimeUpdate"
  9. @loadedmetadata="onLoadedMetadata"
  10. @fullscreenchange="onFullscreenChange"
  11. class="video-player">
  12. <cover-view class="toolbar" :style="'width: '+ windowWidth +'px; height: '+ 40 +'px;'" v-if="isFullscreen || !isPlaying">
  13. <cover-image
  14. :src="isPlaying ? '/static/images/pause.png' : '/static/images/play.png'"
  15. class="play-pause-btn"
  16. @tap="togglePlay"
  17. ></cover-image>
  18. <cover-view class="time-display">
  19. <text>{{ formatTime(currentTime) }} / {{ formatTime(duration) }}</text>
  20. </cover-view>
  21. <cover-view class="progress-container">
  22. <cover-view class="progress-bar" :style="{ width: progress + '%' }"></cover-view>
  23. </cover-view>
  24. <cover-view class="fullscreen-btn" @tap="toggleFullscreen" style="margin-right: 30rpx;background-color: red;">
  25. <cover-image src="@/static/image/course/full_screen.png"></cover-image>
  26. </cover-view>
  27. </cover-view>
  28. </video>
  29. </view>
  30. </template>
  31. <script>
  32. export default {
  33. data() {
  34. return {
  35. videoUrl: 'https://1319721001.vod-qcloud.com/3525603bvodcq1319721001/2b3e76ac1253642698811158287/Hmod7JMfAowA.mp4',
  36. isPlaying: false,
  37. isFullscreen: false,
  38. duration: 0,
  39. currentTime: 0,
  40. progress: 0,
  41. videoContext:null,
  42. windowWidth:0,
  43. windowHeight:0,
  44. };
  45. },
  46. onLoad() {
  47. const systemInfo = uni.getSystemInfoSync();
  48. this.windowWidth = systemInfo.screenWidth //获取屏幕宽度
  49. this.windowHeight=systemInfo.screenHeight;
  50. },
  51. onReady() {
  52. this.videoContext = uni.createVideoContext('video');
  53. },
  54. methods: {
  55. onLoadedMetadata(event) {
  56. this.duration = event.target.duration;
  57. },
  58. onTimeUpdate(event) {
  59. this.currentTime = event.target.currentTime;
  60. this.progress = (this.currentTime / this.duration) * 100;
  61. },
  62. togglePlay() {
  63. if (this.isPlaying) {
  64. this.videoContext.pause();
  65. } else {
  66. this.videoContext.play();
  67. }
  68. this.isPlaying = !this.isPlaying;
  69. },
  70. toggleFullscreen() {
  71. console.log("toggleFullscreen");
  72. if (this.isFullscreen) {
  73. this.videoContext.exitFullScreen();
  74. } else {
  75. this.videoContext.requestFullScreen();
  76. }
  77. },
  78. onFullscreenChange(event) {
  79. this.isFullscreen = event.detail.fullScreen;
  80. },
  81. formatTime(seconds) {
  82. const minutes = Math.floor(seconds / 60);
  83. const secs = Math.floor(seconds % 60);
  84. return `${minutes.toString().padStart(2, '0')}:${secs.toString().padStart(2, '0')}`;
  85. },
  86. },
  87. };
  88. </script>
  89. <style>
  90. page{
  91. background: #000;
  92. }
  93. .container {
  94. position: relative;
  95. width: 375px;
  96. height: 700px;
  97. background-color: black;
  98. }
  99. .video-player {
  100. width:300px;
  101. height:300px;
  102. border-radius: 20rpx;
  103. background: red;
  104. position: relative;
  105. background-color: red;
  106. position: relative;
  107. }
  108. .toolbar {
  109. position: absolute;
  110. bottom: 0px;
  111. left: 0;
  112. background-color: rgba(0, 0, 0, 0.5);
  113. display: flex;
  114. align-items: center;
  115. justify-content: space-around;
  116. z-index: 10;
  117. }
  118. .play-pause-btn {
  119. width: 30px;
  120. height: 30px;
  121. }
  122. .time-display {
  123. color: #fff;
  124. font-size: 14px;
  125. }
  126. .progress-container {
  127. flex: 1;
  128. height: 5px;
  129. background-color: #ccc;
  130. margin: 0 10px;
  131. position: relative;
  132. }
  133. .progress-bar {
  134. position: absolute;
  135. height: 100%;
  136. background-color: #ff5722;
  137. }
  138. .fullscreen-btn {
  139. width: 30px;
  140. height: 30px;
  141. }
  142. .fullscreen-btn cover-image {
  143. width: 100%;
  144. height: 100%;
  145. }
  146. </style>