index.vue 5.9 KB
Newer Older
Q
qiang 已提交
1
<template>
Q
qiang 已提交
2
  <uni-video v-on="$listeners">
3 4 5
    <div
      ref="container"
      class="uni-video-container"
fxy060608's avatar
fxy060608 已提交
6
    />
Q
qiang 已提交
7 8 9 10 11 12 13
    <div class="uni-video-slot">
      <slot />
    </div>
  </uni-video>
</template>
<script>
import {
Q
qiang 已提交
14
  subscriber
Q
qiang 已提交
15
} from 'uni-mixins'
Q
qiang 已提交
16
import native from '../../mixins/native'
Q
qiang 已提交
17 18 19

const methods = [
  'play',
20
  'pause',
21
  'stop',
Q
qiang 已提交
22 23 24 25 26 27 28 29 30 31 32 33
  'seek',
  'sendDanmu',
  'playbackRate',
  'requestFullScreen',
  'exitFullScreen'
]

const events = [
  'play',
  'pause',
  'ended',
  'timeupdate',
34
  'fullscreenchange',
35
  'fullscreenclick',
Q
qiang 已提交
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
  'waiting',
  'error'
]

const attrs = [
  'src',
  'duration',
  'controls',
  'danmuList',
  'danmuBtn',
  'enableDanmu',
  'autoplay',
  'loop',
  'muted',
  'objectFit',
  'poster',
  'direction',
  'showProgress',
  'initialTime',
  'showFullscreenBtn',
  'pageGesture',
  'enableProgressGesture',
58 59
  'showPlayBtn',
  'showMuteBtn',
60
  'enablePlayGesture',
61 62
  'vslideGesture',
  'vslideGestureInFullscreen',
63 64 65
  'showCenterPlayBtn',
  'showLoading',
  'codec',
66
  'httpCache',
Q
qiang 已提交
67
  'playStrategy',
Q
qiang 已提交
68 69
  'header',
  'advanced'
Q
qiang 已提交
70 71 72 73
]

export default {
  name: 'Video',
Q
qiang 已提交
74
  mixins: [subscriber, native],
Q
qiang 已提交
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
  props: {
    id: {
      type: String,
      default: ''
    },
    src: {
      type: String,
      default: ''
    },
    duration: {
      type: [Number, String],
      default: ''
    },
    controls: {
      type: [Boolean, String],
      default: true
    },
    danmuList: {
      type: Array,
      default () {
        return []
      }
    },
    danmuBtn: {
      type: [Boolean, String],
      default: false
    },
    enableDanmu: {
      type: [Boolean, String],
      default: false
    },
    autoplay: {
      type: [Boolean, String],
      default: false
    },
    loop: {
      type: [Boolean, String],
      default: false
    },
    muted: {
      type: [Boolean, String],
      default: false
    },
    objectFit: {
      type: String,
      default: 'contain'
    },
    poster: {
      type: String,
      default: ''
    },
    direction: {
      type: [String, Number],
Q
qiang 已提交
128
      default: ''
Q
qiang 已提交
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
    },
    showProgress: {
      type: Boolean,
      default: true
    },
    initialTime: {
      type: [String, Number],
      default: 0
    },
    showFullscreenBtn: {
      type: [Boolean, String],
      default: true
    },
    pageGesture: {
      type: [Boolean, String],
      default: false
    },
    enableProgressGesture: {
      type: [Boolean, String],
      default: true
    },
150 151 152 153 154 155 156 157
    vslideGesture: {
      type: [Boolean, String],
      default: false
    },
    vslideGestureInFullscreen: {
      type: [Boolean, String],
      default: false
    },
158 159 160 161 162 163 164 165
    showPlayBtn: {
      type: [Boolean, String],
      default: true
    },
	showMuteBtn: {
	  type: [Boolean, String],
	  default: true
	},
166 167 168 169
    enablePlayGesture: {
      type: [Boolean, String],
      default: true
    },
Q
qiang 已提交
170 171 172
    showCenterPlayBtn: {
      type: [Boolean, String],
      default: true
173 174 175 176
    },
    showLoading: {
      type: [Boolean, String],
      default: true
177 178 179 180 181 182 183 184
    },
    codec: {
      type: String,
      default: 'hardware'
    },
    httpCache: {
      type: [Boolean, String],
      default: false
185 186 187 188
    },
    playStrategy: {
      type: [Number, String],
      default: 0
Q
qiang 已提交
189 190 191
    },
    header: {
      type: Object,
Q
qiang 已提交
192
      default () {
Q
qiang 已提交
193 194
        return {}
      }
Q
qiang 已提交
195 196 197
    },
    advanced: {
      type: Array,
Q
qiang 已提交
198
      default () {
Q
qiang 已提交
199 200
        return []
      }
Q
qiang 已提交
201 202 203 204 205 206 207 208
    }
  },
  computed: {
    attrs () {
      const obj = {}
      attrs.forEach(key => {
        let val = this.$props[key]
        val = key === 'src' ? this.$getRealPath(val) : val
Q
qiang 已提交
209
        obj[key.replace(/[A-Z]/g, str => '-' + str.toLowerCase())] = val
Q
qiang 已提交
210 211 212 213 214
      })
      return obj
    }
  },
  mounted () {
D
DCloud_LXH 已提交
215 216 217 218 219
    this._onParentReady(() => {
      const video = this.video = plus.video.createVideoPlayer('video' + Date.now(), Object.assign({}, this.attrs, this.position))
      plus.webview.currentWebview().append(video)
      if (this.hidden) {
        video.hide()
220
      }
D
DCloud_LXH 已提交
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
      this.$watch('attrs', () => {
        this.video && this.video.setStyles(this.attrs)
      }, { deep: true })
      this.$watch('position', () => {
        this.video && this.video.setStyles(this.position)
      }, { deep: true })
      this.$watch('hidden', (val) => {
        const video = this.video
        if (video) {
          video[val ? 'hide' : 'show']()
          // iOS 隐藏状态设置 setStyles 不生效
          if (!val) {
            video.setStyles(this.position)
          }
        }
      })
      events.forEach(key => {
        video.addEventListener(key, (e) => {
          this.$trigger(key, {}, { ...e.detail })
        })
Q
qiang 已提交
241 242 243 244 245 246 247 248 249 250 251 252 253
      })
    })
  },
  beforeDestroy () {
    this.video && this.video.close()
    delete this.video
  },
  methods: {
    _handleSubscribe ({
      type,
      data = {}
    }) {
      if (methods.includes(type)) {
Q
qiang 已提交
254 255 256 257 258 259 260
        if (typeof data === 'object') {
          switch (type) {
            case 'seek':
              data = data.position
              break
            case 'playbackRate':
              data = data.rate
261 262 263
              break
            case 'requestFullScreen':
              data = data.direction
Q
qiang 已提交
264 265
              break
          }
Q
qiang 已提交
266
        }
Q
qiang 已提交
267
        this.video && this.video[type](data)
Q
qiang 已提交
268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306
      }
    }
  }
}
</script>

<style>
uni-video {
  width: 300px;
  height: 225px;
  display: inline-block;
  line-height: 0;
  overflow: hidden;
  position: relative;
}

uni-video[hidden] {
  display: none;
}

.uni-video-container {
  width: 100%;
  height: 100%;
  position: absolute;
  top: 0;
  left: 0;
  overflow: hidden;
  background-color: black;
}

.uni-video-slot {
  position: absolute;
  top: 0;
  width: 100%;
  height: 100%;
  overflow: hidden;
  pointer-events: none;
}
</style>