• UniApp video 使用(自定义进度条,及微信无法暂停播放设置进度问题)


    • 在小程序开发的时候,其他小程序能够正常暂停、播放、切换进度,但是在微信小程序发现没有生效,原因是平台差异

      // 官网案例是这样获取 videoContext 的
      this.videoContext = uni.createVideoContext('myVideo')
      
      // 但是如果需要处理微信小程序的差异,则需要将 this 传入
      this.videoContext = uni.createVideoContext('myVideo', this)
      
      • 1
      • 2
      • 3
      • 4
      • 5
    • 案例效果

      请添加图片描述

      <template>
          <view class="video-view">
              
              <video
                  autoplay
                  id="myVideo"
                  class="video-content"
                  :src="drama.limit_url"
                  :controls="false"
                  @loadedmetadata="loadedmetadata"
                  @timeupdate="timeUpdate"
              >
              video>
              
              <view class="video-cover" @click="touchPlay">
                  <view class="view-cover-content">
                      
                      <view class="video-info">
                          
                          <view style="flex: 1;">view>
                          
                          <view class="video-time" v-if="isDrag">
                              <view class="video-time-current">{{ dragStarTime }}view>
                              <view style="padding: 0 20rpx;">/view>
                              <text>{{ dragEndTime }}text>
                          view>
                          
                          <view class="slider-view" @click.stop>
                              <u-slider
                                  v-model="currentTime"
                                  min="0"
                                  :max="duration"
                                  inactiveColor="rgba(255, 255, 255, 0.2)"
                                  activeColor="#F8DD52"
                                  @changing="sliderChanging"
                                  @change="sliderChange"
                              >u-slider>
                          view>
                          
                          <view class="video-info-current" @click.stop>
                              
                              <view class="video-info-title text-ell" v-if="drama.project_drama_name">{{ drama.project_drama_name }}-第{{ drama.eq_number }}集view>
                              
                              <view style="flex: 1;">view>
                              
                              <view class="video-info-btn" @click="touchSwitch">
                                  <view class="arrow-right-title">选集view>
                                  <u-icon name="arrow-right" color="#fff">u-icon>
                              view>
                          view>
                      view>
                  view>
              view>
          view>
      template>
      
      <script>
      export default {
          props: {
              // 当前剧集
              drama: {
                  type: Object,
                  default: () => {}
              }
          },
          data() {
              return {
                  // 播放器上下文
                  videoContext: undefined,
                  // 播放状态
                  isPlay: true,
                  // 当前时长
                  currentTime: 0,
                  // 总时间
                  duration: 0.1,
                  // 是否正在拖拽进度
                  isDrag: false,
                  // 拖拽时视频时间
                  dragStarTime: '',
                  dragEndTime: '',
                  // 当前还没显示过提示消息
                  isShowMsg: true
              }
          },
          mounted () {
              // 获取播放器上下文(后面的 this 需要传入,在微信小程序上无法暂停播放拖拽精度,所以需要传入这个)
              this.videoContext = uni.createVideoContext('myVideo', this)
          },
          methods: {
              // 播放
              play () {
                  this.videoContext.play()
              },
              // 暂停
              pause () {
                  this.videoContext.pause()
              },
              // 播放状态切换
              touchPlay () {
                  this.isPlay = !this.isPlay
                  if (this.isPlay) {
                      this.play ()
                  } else {
                      this.pause()
                  }
              },
              // 播放进度发生变化
              timeUpdate (e) {
                  // 拖拽时不需要进行更新
                  if (!this.isDrag) {
                      // 更新进度
                      const { currentTime } = e.detail
                      this.currentTime = currentTime
                      // 播放完成
                      if (Math.trunc(currentTime) === Math.trunc(duration)) {
                          this.$emit('playcomplete', e)
                      }
                      // 返回当前播放时间
                      this.$emit('timeupdate', e)
                  }
              },
              // 拖拽结束
              sliderChange (value) {
                  // 停止拖拽
                  this.isDrag = false
                  // 判断一下是否大于基础时间
                  if (this.duration > 0.1) {
                      // 跳到指定时间点
                      this.videoContext.seek(value)
                      // 并调用播放
                      this.play()
                  }
              },
              // 正在拖拽
              sliderChanging (value) {
                  // 开始拖拽
                  this.isDrag = true
                  // 刷新时间
                  this.reloadVideoTime()
              },
              // 刷新显示时间
              reloadVideoTime () {
                  // 当前时间
                  this.dragStarTime = this.$pub.TIME_CONVERT(this.currentTime)
                  // 总时间
                  this.dragEndTime = this.$pub.TIME_CONVERT(this.duration)
              },
              // 加载完成
              loadedmetadata (e) {
                  const { duration } = e.detail
                  // 记录视频总时间
                  this.duration = duration
                  // 回调
                  this.$emit('loadcomplete')
              },
              // 切换选集
              touchSwitch () {
                  this.$emit('switch')
              }
          }
      }
      script>
      
      <style lang="scss">
      .video-view {
          position: relative;
          width: 100%;
          height: 100%;
          .video-content {
              width: 100%;
              height: 100%;
          }
          .video-cover {
              position: absolute;
              left: 0;
              top: 0;
              width: 100%;
              height: 100%;
              .view-cover-content {
                  position: relative;
                  width: 100%;
                  height: 100%;
                  .video-info {
                      display: flex;
                      flex-direction: column;
                      position: absolute;
                      bottom: 0;
                      left: 0;
                      width: 100%;
                      height: 246rpx;
                      background: linear-gradient(180deg, rgba(0,0,0,0.00) 0%, rgba(0,0,0,0.00) 0%, rgba(0,0,0,0.40) 100%, rgba(0,0,0,0.40) 100%);
                      .slider-view {
                          flex-shrink: 0;
                      }
                      .video-info-current {
                          flex-shrink: 0;
                          display: flex;
                          align-items: center;
                          width: 100%;
                          color: #fff;
                          font-size: 34rpx;
                          padding: 0 40rpx 40rpx;
                          width: calc(100% - 80rpx);
                          .video-info-btn {
                              flex-shrink: 0;
                              display: flex;
                              align-items: center;
                              padding-left: 40rpx;
                              .arrow-right-title {
                                  margin-right: 10rpx;
                              }
                          }
                      }
                      .video-time {
                          display: flex;
                          align-items: center;
                          justify-content: center;
                          font-size: 64rpx;
                          color: rgba(255, 255, 255, 0.7);
                          .video-time-current {
                              color: #fff;
                          }
                      }
                  }
              }
          }
      }
      style>
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15
      • 16
      • 17
      • 18
      • 19
      • 20
      • 21
      • 22
      • 23
      • 24
      • 25
      • 26
      • 27
      • 28
      • 29
      • 30
      • 31
      • 32
      • 33
      • 34
      • 35
      • 36
      • 37
      • 38
      • 39
      • 40
      • 41
      • 42
      • 43
      • 44
      • 45
      • 46
      • 47
      • 48
      • 49
      • 50
      • 51
      • 52
      • 53
      • 54
      • 55
      • 56
      • 57
      • 58
      • 59
      • 60
      • 61
      • 62
      • 63
      • 64
      • 65
      • 66
      • 67
      • 68
      • 69
      • 70
      • 71
      • 72
      • 73
      • 74
      • 75
      • 76
      • 77
      • 78
      • 79
      • 80
      • 81
      • 82
      • 83
      • 84
      • 85
      • 86
      • 87
      • 88
      • 89
      • 90
      • 91
      • 92
      • 93
      • 94
      • 95
      • 96
      • 97
      • 98
      • 99
      • 100
      • 101
      • 102
      • 103
      • 104
      • 105
      • 106
      • 107
      • 108
      • 109
      • 110
      • 111
      • 112
      • 113
      • 114
      • 115
      • 116
      • 117
      • 118
      • 119
      • 120
      • 121
      • 122
      • 123
      • 124
      • 125
      • 126
      • 127
      • 128
      • 129
      • 130
      • 131
      • 132
      • 133
      • 134
      • 135
      • 136
      • 137
      • 138
      • 139
      • 140
      • 141
      • 142
      • 143
      • 144
      • 145
      • 146
      • 147
      • 148
      • 149
      • 150
      • 151
      • 152
      • 153
      • 154
      • 155
      • 156
      • 157
      • 158
      • 159
      • 160
      • 161
      • 162
      • 163
      • 164
      • 165
      • 166
      • 167
      • 168
      • 169
      • 170
      • 171
      • 172
      • 173
      • 174
      • 175
      • 176
      • 177
      • 178
      • 179
      • 180
      • 181
      • 182
      • 183
      • 184
      • 185
      • 186
      • 187
      • 188
      • 189
      • 190
      • 191
      • 192
      • 193
      • 194
      • 195
      • 196
      • 197
      • 198
      • 199
      • 200
      • 201
      • 202
      • 203
      • 204
      • 205
      • 206
      • 207
      • 208
      • 209
      • 210
      • 211
      • 212
      • 213
      • 214
      • 215
      • 216
      • 217
      • 218
      • 219
      • 220
      • 221
      • 222
      • 223
      • 224
      • 225
      • 226
      • 227
      • 228
  • 相关阅读:
    用Navicat备份Mysql演示系统数据库的时候出:Too Many Connections
    ArrayBuffer 如何使用 以及跟webGl 的关系
    Stream常用操作以及原理探索
    ChatGPT突然上线APP!iPhone可用、速度更快,GPT-4用量限制疑似取消
    redis 学习指南
    LabVIEW应用开发——基本函数(一)
    【Golang】简记操作:Centos安装、卸载、升级Golang运行环境
    cdp集群Hbase组件HRegionServer服务停止原因以及排查
    ASP.NET按钮OnClick事件无法用JS触发
    阿里工作8年,肝到P7就剩这份学习笔记了,已助朋友拿到10个Offer
  • 原文地址:https://blog.csdn.net/zz00008888/article/details/126290538