Video 视频

基础用法

Video 视频 - 图1

  1. <div style="width:400px; height: 225px; margin-bottom: 100px;">
  2. <se-video
  3. src="http://chimee.org/vod/1.mp4"
  4. kernel="mp4"
  5. title="小森林·春秋"
  6. controls
  7. show-fullscreen-btn
  8. show-mute-btn
  9. muted
  10. @ended="onEnded"
  11. @timeupdate="onTimeupdate"
  12. @fullscreenchange="onFullscreenchange"
  13. @waiting="onWaiting"
  14. @progress="onProgress"
  15. @error="onError"
  16. ></se-video>
  17. </div>
  18. <se-video
  19. ref="myVideo"
  20. src="https://node.imgio.in/demo/birds.m3u8"
  21. kernel="hls"
  22. :poster="poster"
  23. :title="title"
  24. :controls="controls"
  25. :muted="muted"
  26. :loop="loop"
  27. :autoplay="autoplay"
  28. :initial-time="initTime"
  29. :object-fit="objectFit"
  30. :show-fullscreen-btn="showFullscreenBtn"
  31. :show-mute-btn="showMuteBtn"
  32. @play="onPlay"
  33. @pause="onPause"
  34. @ended="onEnded"
  35. @timeupdate="onTimeupdate"
  36. @fullscreenchange="onFullscreenchange"
  37. @waiting="onWaiting"
  38. @progress="onProgress"
  39. @error="onError"
  40. ></se-video>
  41. <br />
  42. <se-button @click="emitPlay">播放</se-button>
  43. <se-button @click="emitPause">暂停</se-button>
  44. <se-button @click="emitStop">停止</se-button>
  45. <se-button @click="emitSeek">跳转到3s</se-button>
  46. <se-button @click="requestFullScreen">进入全屏</se-button>
  47. <se-button @click="exitFullScreen">退出全屏</se-button>
  48. <br />
  49. <br />
  50. <label>
  51. 设置视频标题:
  52. <se-input :value="title" @blur="titleChange" />
  53. </label>
  54. <label>
  55. 是否显示控制面板:
  56. <se-switch :checked="controls" @change="controlsChange" />
  57. </label>
  58. <label>
  59. 是否静音:
  60. <se-switch :checked="muted" @change="mutedChange" />
  61. </label>
  62. <label>
  63. 是否显示音量按钮:
  64. <se-switch :checked="showMuteBtn" @change="showMuteBtnChange" />
  65. </label>
  66. <label>
  67. 是否显示全屏按钮:
  68. <se-switch :checked="showFullscreenBtn" @change="showFullscreenBtnChange" />
  69. </label>
  70. <label>
  71. 是否循环播放:
  72. <se-switch :checked="loop" @change="loopChange" />
  73. </label>
  74. <label>
  75. 设置倍速:
  76. <se-select :value="rate" @change="rateChange" size="mini">
  77. <se-option
  78. v-for="(option, index) in options"
  79. :key="index"
  80. :value="option.value"
  81. :label="option.label"
  82. ></se-option>
  83. </se-select>
  84. </label>
  85. <label>
  86. 设置播放地址:
  87. <se-select :value="src" @change="srcChange" size="mini">
  88. <se-option
  89. v-for="(option, index) in srcOptions"
  90. :key="index"
  91. :value="option.value"
  92. :label="option.label"
  93. ></se-option>
  94. </se-select>
  95. </label>
  96. <label>
  97. 设置视频封面:
  98. <se-select :value="poster" @change="posterChange" size="mini">
  99. <se-option
  100. v-for="(option, index) in posterOptions"
  101. :key="index"
  102. :value="option.value"
  103. :label="option.label"
  104. ></se-option>
  105. </se-select>
  106. </label>
  107. <label>
  108. 设置视频的表现形式:
  109. <se-select :value="objectFit" @change="objectFitChange" size="mini">
  110. <se-option
  111. v-for="(option, index) in objectFitOptions"
  112. :key="index"
  113. :value="option.value"
  114. :label="option.label"
  115. ></se-option>
  116. </se-select>
  117. </label>
  118. <script>
  119. export default {
  120. data() {
  121. return {
  122. videoContext: null,
  123. rate: 1,
  124. options: [
  125. { value: 0.5, label: 0.5 },
  126. { value: 0.8, label: 0.8 },
  127. { value: 1.0, label: 1.0 },
  128. { value: 1.25, label: 1.25 },
  129. { value: 1.5, label: 1.5 },
  130. { value: 2, label: 2 }
  131. ],
  132. src: 'https://www.w3cschool.cn/statics/demosource/mov_bbb.mp4',
  133. srcOptions: [
  134. { value: 'https://www.w3cschool.cn/statics/demosource/mov_bbb.mp4', label: '影片1' },
  135. { value: 'https://www.w3school.com.cn/i/movie.ogg', label: '影片2' }
  136. ],
  137. poster: 'https://p3.ssl.qhimg.com/t01202f3bb176275d06.png',
  138. posterOptions: [
  139. { value: 'https://p3.ssl.qhimg.com/t01202f3bb176275d06.png', label: '封面1' },
  140. { value: 'https://p0.ssl.qhimg.com/t011ac6ee181d99574e.jpg', label: '封面2' }
  141. ],
  142. objectFit: 'contain',
  143. objectFitOptions: [
  144. { value: 'contain', label: '默认' },
  145. { value: 'fill', label: '填充' },
  146. { value: 'cover', label: '等比例填充' }
  147. ],
  148. controls: true,
  149. title: '小森林·春秋',
  150. muted: false,
  151. loop: false,
  152. autoplay: false,
  153. initTime: 0,
  154. showFullscreenBtn: true,
  155. showMuteBtn: true
  156. }
  157. },
  158. methods: {
  159. onPlay(e) {
  160. console.log('on play')
  161. },
  162. onPause() {
  163. console.log('on pause')
  164. },
  165. onEnded() {
  166. console.log('on end')
  167. },
  168. onTimeupdate({ detail }) {
  169. console.log('on timeupdate', detail)
  170. },
  171. onFullscreenchange({ detail }) {
  172. console.log('on fullscreen', detail)
  173. },
  174. onWaiting() {
  175. console.log('on waiting')
  176. },
  177. onProgress({ detail }) {
  178. console.log('on progress', detail)
  179. },
  180. onError(error) {
  181. console.log(`on error`, error)
  182. },
  183. emitPlay() {
  184. this.videoContext.play()
  185. },
  186. emitPause() {
  187. this.videoContext.pause()
  188. },
  189. emitStop() {
  190. this.videoContext.stop()
  191. },
  192. emitSeek() {
  193. this.videoContext.seek(3)
  194. },
  195. requestFullScreen() {
  196. this.videoContext.requestFullScreen()
  197. },
  198. exitFullScreen() {
  199. this.videoContext.exitFullScreen()
  200. },
  201. rateChange(v) {
  202. this.rate = v
  203. this.videoContext.playbackRate(v)
  204. },
  205. srcChange(v) {
  206. this.src = v
  207. },
  208. controlsChange(v) {
  209. this.controls = v
  210. },
  211. titleChange(e) {
  212. this.title = e.detail.value
  213. },
  214. mutedChange(v) {
  215. this.muted = v
  216. },
  217. loopChange(v) {
  218. this.loop = v
  219. },
  220. posterChange(v) {
  221. this.poster = v
  222. },
  223. objectFitChange(v) {
  224. this.objectFit = v
  225. },
  226. showMuteBtnChange(v) {
  227. this.showMuteBtn = v
  228. },
  229. showFullscreenBtnChange(v) {
  230. this.showFullscreenBtn = v
  231. }
  232. },
  233. mounted() {
  234. this.videoContext = this.$createVideoContext('myVideo')
  235. }
  236. }
  237. </script>
  238. <style>
  239. label + label {
  240. display: block;
  241. margin-top: 15px;
  242. margin-bottom: 15px;
  243. }
  244. </style>

Props

属性类型默认值必填说明
srcstringdefault要播放视频的资源地址
kernelstringmp4解码器核心 mp4/hls
controlsbooleantrue是否显示默认播放控件(播放/暂停按钮、播放进度、时间)
autoplaybooleanfalse是否自动播放
loopbooleanfalse是否循环播放
mutedbooleanfalse是否静音播放
initial-timenumber0指定视频初始播放位置
show-fullscreen-btnbooleantrue是否显示全屏按钮
object-fitstringcontain当视频大小与 video 容器大小不一致时,视频的表现形式, 可选值:contain / fill / cover
posterstring-视频封面的图片网络资源地址
show-mute-btnbooleanfalse是否显示静音按钮
titlestring-视频的标题,全屏时在顶部展示

Events

事件名称描述回调函数参数
play当开始/继续播放时触发 play 事件e: Event
pause当暂停播放时触发 pause 事件e: Event
ended当播放到末尾时触发 ended 事件e: Event
timeupdate播放进度变化时触发,event.detail = {currentTime, duration}e: Event
fullscreenchange视频进入和退出全屏时触发,event.detail = {fullScreen, direction}direction 有效值为 horizontale: Event
waiting视频出现缓冲时触发e: Event
error视频播放出错时触发e: Event
progress加载进度变化时触发,只支持一段加载。event.detail = { buffered },百分比e: Event

Global Methods

事件名称描述参数返回值
$createVideoContext创建 video 上下文 VideoContext 对象$createVideoContext(id: string, this: Object)id: video 组件的 ref;this: 在自定义组件下,当前组件实例的 this,以操作组件内 video 组件VideoContext

VideoContext

事件名称描述参数
VideoContext.play播放视频-
VideoContext.pause暂停视频-
VideoContext.stop停止视频-
VideoContext.seek跳转到指定位置position: number
VideoContext.playbackRate设置倍速播放rate: number
VideoContext.requestFullScreen进入全屏-
VideoContext.exitFullScreen退出全屏-

对微信小程序属性和方法的支持程度如下:

属性是否支持
src
duration
controls
danmu-list
danmu-btn
enable-danmu
autoplay
loop
muted
initial-time
show-progress
show-fullscreen-btn
show-play-btn
show-center-play-btn
object-fit
poster
show-mute-btn
title
play-btn-position
方法是否支持
VideoContext.play
VideoContext.pause
VideoContext.stop
VideoContext.seek
VideoContext.playbackRate
VideoContext.requestFullScreen
VideoContext.exitFullScreen
VideoContext.hideStatusBar
VideoContext.showStatusBar
VideoContext.sendDanmu