#### video
視頻。
| 屬性名 | 類型 | 默認值 | 說明 | 最低版本 |
| ----------------------- | ------------ | ------- | ---------------------------------------- | ---------------------------------------- |
| src | String | | 要播放視頻的資源地址 | |
| initial-time | Number | | 指定視頻初始播放位置 | [1.6.0](https://mp.weixin.qq.com/debug/wxadoc/dev/framework/compatibility.html) |
| duration | Number | | 指定視頻時長 | [1.1.0](https://mp.weixin.qq.com/debug/wxadoc/dev/framework/compatibility.html) |
| controls | Boolean | true | 是否顯示默認播放控件(播放/暫停按鈕、播放進度、時間) | |
| danmu-list | Object Array | | 彈幕列表 | |
| danmu-btn | Boolean | false | 是否顯示彈幕按鈕,只在初始化時有效,不能動態變更 | |
| enable-danmu | Boolean | false | 是否展示彈幕,只在初始化時有效,不能動態變更 | |
| autoplay | Boolean | false | 是否自動播放 | |
| loop | Boolean | false | 是否循環播放 | [1.4.0](https://mp.weixin.qq.com/debug/wxadoc/dev/framework/compatibility.html) |
| muted | Boolean | false | 是否靜音播放 | [1.4.0](https://mp.weixin.qq.com/debug/wxadoc/dev/framework/compatibility.html) |
| page-gesture | Boolean | false | 在非全屏模式下,是否開啟亮度與音量調節手勢 | [1.6.0](https://mp.weixin.qq.com/debug/wxadoc/dev/framework/compatibility.html) |
| direction | Number | | 設置全屏時視頻的方向,不指定則根據寬高比自動判斷。有效值為 0(正常豎向), 90(屏幕逆時針90度), -90(屏幕順時針90度) | [1.7.0](https://mp.weixin.qq.com/debug/wxadoc/dev/framework/compatibility.html) |
| show-progress | Boolean | true | 若不設置,寬度大于240時才會顯示 | [1.9.0](https://mp.weixin.qq.com/debug/wxadoc/dev/framework/compatibility.html) |
| show-fullscreen-btn | Boolean | true | 是否顯示全屏按鈕 | [1.9.0](https://mp.weixin.qq.com/debug/wxadoc/dev/framework/compatibility.html) |
| show-play-btn | Boolean | true | 是否顯示視頻底部控制欄的播放按鈕 | [1.9.0](https://mp.weixin.qq.com/debug/wxadoc/dev/framework/compatibility.html) |
| show-center-play-btn | Boolean | true | 是否顯示視頻中間的播放按鈕 | [1.9.0](https://mp.weixin.qq.com/debug/wxadoc/dev/framework/compatibility.html) |
| enable-progress-gesture | Boolean | true | 是否開啟控制進度的手勢 | [1.9.0](https://mp.weixin.qq.com/debug/wxadoc/dev/framework/compatibility.html) |
| objectFit | String | contain | 當視頻大小與 video 容器大小不一致時,視頻的表現形式。contain:包含,fill:填充,cover:覆蓋 | |
| poster | String | | 視頻封面的圖片網絡資源地址,如果 controls 屬性值為 false 則設置 poster 無效 | |
| bindplay | EventHandle | | 當開始/繼續播放時觸發play事件 | |
| bindpause | EventHandle | | 當暫停播放時觸發 pause 事件 | |
| bindended | EventHandle | | 當播放到末尾時觸發 ended 事件 | |
| bindtimeupdate | EventHandle | | 播放進度變化時觸發,event.detail = {currentTime, duration} 。觸發頻率 250ms 一次 | |
| bindfullscreenchange | EventHandle | | 當視頻進入和退出全屏是觸發,event.detail = {fullScreen, direction},direction取為 vertical 或 horizontal | [1.4.0](https://mp.weixin.qq.com/debug/wxadoc/dev/framework/compatibility.html) |
| bindwaiting | EventHandle | | 視頻出現緩沖時觸發 | [1.7.0](https://mp.weixin.qq.com/debug/wxadoc/dev/framework/compatibility.html) |
| binderror | EventHandle | | 視頻播放出錯時觸發 | [1.7.0](https://mp.weixin.qq.com/debug/wxadoc/dev/framework/compatibility.html) |
`<video />` 默認寬度300px、高度225px,可通過wxss設置寬高。
**示例代碼:**
```
<view class="section tc">
<video src="{{src}}" controls ></video>
<view class="btn-area">
<button bindtap="bindButtonTap">獲取視頻</button>
</view>
</view>
<view class="section tc">
<video id="myVideo" src="http://wxsnsdy.tc.qq.com/105/20210/snsdyvideodownload?filekey=30280201010421301f0201690402534804102ca905ce620b1241b726bc41dcff44e00204012882540400&bizid=1023&hy=SH&fileparam=302c020101042530230204136ffd93020457e3c4ff02024ef202031e8d7f02030f42400204045a320a0201000400" danmu-list="{{danmuList}}" enable-danmu danmu-btn controls></video>
<view class="btn-area">
<button bindtap="bindButtonTap">獲取視頻</button>
<input bindblur="bindInputBlur"/>
<button bindtap="bindSendDanmu">發送彈幕</button>
</view>
</view>
function getRandomColor () {
let rgb = []
for (let i = 0 ; i < 3; ++i){
let color = Math.floor(Math.random() * 256).toString(16)
color = color.length == 1 ? '0' + color : color
rgb.push(color)
}
return '#' + rgb.join('')
}
Page({
onReady: function (res) {
this.videoContext = wx.createVideoContext('myVideo')
},
inputValue: '',
data: {
src: '',
danmuList: [
{
text: '第 1s 出現的彈幕',
color: '#ff0000',
time: 1
},
{
text: '第 3s 出現的彈幕',
color: '#ff00ff',
time: 3
}]
},
bindInputBlur: function(e) {
this.inputValue = e.detail.value
},
bindButtonTap: function() {
var that = this
wx.chooseVideo({
sourceType: ['album', 'camera'],
maxDuration: 60,
camera: ['front','back'],
success: function(res) {
that.setData({
src: res.tempFilePath
})
}
})
},
bindSendDanmu: function () {
this.videoContext.sendDanmu({
text: this.inputValue,
color: getRandomColor()
})
}
})
```
相關api:[wx.createVideoContext](https://mp.weixin.qq.com/debug/wxadoc/dev/api/api-video.html)
##### 注意事項
1. `tip`: `video` 組件是由客戶端創建的原生組件,它的層級是最高的,不能通過 z-index 控制層級。
2. `tip`: 請勿在 `scroll-view`、`swiper`、`picker-view`、`movable-view` 中使用 `video` 組件。
3. `tip`: `css` 動畫對 `video` 組件無效。
- 簡介
- 第一章 公眾號開發
- 使用微信JSSDK
- 接口權限配置
- 分享接口
- 隱藏按鈕項
- 微信支付
- 第二章 小程序開發
- 基礎知識
- 分包加載
- WXSS樣式表
- 配置
- app.json配置
- window
- tabBar
- page.json配置
- 邏輯層
- app.js
- 場景值
- page.js
- 初始化數據
- 生命周期函數
- 頁面相關事件處理函數
- 事件處理函數
- 頁面實例方法
- 路由
- 文件作用域
- 模塊化
- 視圖層
- 模板語法
- 列表渲染
- 條件渲染
- 模板
- 事件
- 引用
- WXS語法規范
- WXS數據類型
- WXS控制流程
- WXS基礎類庫
- 組件
- 視圖容器
- view
- scroll-view
- swiper
- movable-view
- cover-view
- 基礎組件
- icon
- text
- rich-text
- progress
- 表單組件
- button
- checkbox
- form
- input
- label
- picker
- picker-view
- radio
- slider
- switch
- textarea
- 導航組件
- navigator
- 媒體組件
- audio
- image
- video
- camera
- 地圖組件
- map
- 畫布組件
- canvas
- 開放能力
- web-view
- 自定義組件
- 組件模版和樣式
- Component
- 組件傳值
- 組件事件
- Behaviors
- 組件間關系
- 網絡請求
- wx.request
- 微信登錄
- 獲取 openid 和 unionid
- 獲取用戶信息
- 將 wx.request 封裝為 promise
- 上傳圖片接口封裝
- 數據存儲
- 存儲數據和讀取數據
- 獲取數據緩存信息
- 移除數據緩存
- 獲取用戶設置
- openSetting
- getSetting
- 第三章 小游戲開發
- 參考資料