videoUni.vue 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  1. <template>
  2. <view class="es-view-w-x">
  3. <es-nav-title title="视频播放" mode="cover"></es-nav-title>
  4. <video class="video" ref="video" id="video" :header="header" :src=src :autoplay="autoplay" :loop="loop"
  5. :muted="muted" :initial-time="initialTime" :duration="duration" :controls="controls" :danmu-btn="danmuBtn"
  6. :enable-danmu="enableDanmu" :page-gesture="pageGesture" :direction="direction" :show-progress="showProgress"
  7. :show-fullscreen-btn="showFullscreenBtn" :show-play-btn="showPlayBtn" :show-center-play-btn="showCenterPlayBtn"
  8. :show-loading="showLoading" :enable-progress-gesture="enableProgressGesture" :object-fit="objectFit"
  9. :poster="poster" :show-mute-btn="showMuteBtn" :title="title" :enable-play-gesture="enablePlayGesture"
  10. :vslide-gesture="vslideGesture" :vslide-gesture-in-fullscreen="vslideGestureInFullscreen" :codec="codec"
  11. :http-cache="httpCache" :play-strategy="playStrategy" :danmu-list="danmuList" @play="onPlay" @pause="onPause"
  12. @ended="onEnded" @timeupdate="onTimeUpdate" @waiting="onWaiting" @error="onError" @progress="onProgress"
  13. @fullscreenclick="onFullScreenClick" @controlstoggle="onControlsToggle" @fullscreenchange="onFullScreenChange">
  14. </video>
  15. <scroll-view class="uni-padding-wrap uni-common-mt uni-flex-item">
  16. <input class="input" placeholder="输入视频地址播放" @confirm="onSrcInputConfirm" />
  17. <view class="uni-btn-v">
  18. <navigator url="/pages/component/video/video-format">
  19. <button type="primary" @click="pause">视频格式示例</button>
  20. </navigator>
  21. </view>
  22. <view class="uni-title">
  23. <text class="uni-title-text">API示例</text>
  24. </View>
  25. <view class="uni-btn-v">
  26. <button type="primary" @click="play">播放</button>
  27. </view>
  28. <view class="uni-btn-v">
  29. <button type="primary" @click="pause">暂停</button>
  30. </view>
  31. <view class="uni-btn-v">
  32. <button type="primary" @click="seek(pos)">跳转到指定位置</button>
  33. </view>
  34. <view class="uni-btn-v">
  35. <button type="primary" @click="requestFullScreen(requestFullScreenOptions)">进入全屏</button>
  36. </view>
  37. <view class="uni-btn-v">
  38. <button type="primary" @click="exitFullScreen">退出全屏</button>
  39. </view>
  40. <view class="uni-btn-v">
  41. <button type="primary" @click="stop">停止</button>
  42. </view>
  43. <view class="uni-btn-v">
  44. <button type="primary" @click="sendDanmu(danmu)">发送弹幕</button>
  45. </view>
  46. <view class="uni-btn-v">
  47. <button type="primary" @click="playbackRate(rate)">设置倍速</button>
  48. </view>
  49. <view class="uni-title">
  50. <text class="uni-title-text">属性示例</text>
  51. </view>
  52. <view class="uni-btn-v">
  53. <button type="primary" @click="setSrc(_src)">设置播放资源</button>
  54. </view>
  55. <view class="uni-btn-v">
  56. <button type="primary" @click="setAutoplay()">设置是否自动播放(未播放时设置有效)</button>
  57. </view>
  58. <view class="uni-btn-v">
  59. <button type="primary" @click="setLoop()">设置是否循环播放(本次播放完成后生效)</button>
  60. </view>
  61. <view class="uni-btn-v">
  62. <button type="primary" @click="setMuted()">设置是否静音播放</button>
  63. </view>
  64. <view class="uni-btn-v">
  65. <button type="primary" @click="setInitialTime(_initialTime)">设置初始播放位置(本次播放完成后生效)</button>
  66. </view>
  67. <view class="uni-btn-v">
  68. <button type="primary" @click="setDuration(_duration)">设置视频时长(未播放时设置有效)</button>
  69. </view>
  70. <view class="uni-btn-v">
  71. <button type="primary" @click="setControls()">设置是否显示默认播放控件</button>
  72. </view>
  73. <view class="uni-btn-v">
  74. <button type="primary" @click="setDanmuBtn()">设置是否显示弹幕按钮</button>
  75. </view>
  76. <!-- #ifndef WEB -->
  77. <view class="uni-btn-v">
  78. <button type="primary" @click="setPageGesture()">非全屏模式下,设置是否开启亮度与音量调节手势</button>
  79. </view>
  80. <!-- #endif -->
  81. <view class="uni-btn-v">
  82. <button type="primary" @click="setDirection(direction)">设置全屏时视频的方向</button>
  83. </view>
  84. <view class="uni-btn-v">
  85. <button type="primary" @click="setShowProgress()">设置是否显示进度条</button>
  86. </view>
  87. <view class="uni-btn-v">
  88. <button type="primary" @click="setShowFullscreenBtn()">设置是否显示全屏按钮</button>
  89. </view>
  90. <view class="uni-btn-v">
  91. <button type="primary" @click="setShowPlayBtn()">设置是否显示视频底部控制栏的播放按钮</button>
  92. </view>
  93. <view class="uni-btn-v">
  94. <button type="primary" @click="setShowCenterPlayBtn()">设置是否显示视频中间的播放按钮</button>
  95. </view>
  96. <view class="uni-btn-v">
  97. <button type="primary" @click="setShowLoading()">设置是否显示loading控件</button>
  98. </view>
  99. <view class="uni-btn-v">
  100. <button type="primary" @click="setEnableProgressGesture()">设置是否开启控制进度的手势</button>
  101. </view>
  102. <view class="uni-btn-v">
  103. <button type="primary" @click="setObjectFit(objectFit)">设置视频大小与video容器大小不一致时,视频的表现形式</button>
  104. </view>
  105. <view class="uni-btn-v">
  106. <button type="primary" @click="setPoster(poster)">设置视频封面</button>
  107. </view>
  108. <view class="uni-btn-v">
  109. <button type="primary" @click="setShowMuteBtn()">设置是否显示静音按钮(仅限非 Web 平台)</button>
  110. </view>
  111. <view class="uni-btn-v">
  112. <button type="primary" @click="setTitle(title)">设置视频标题(仅限非 Web 平台)</button>
  113. </view>
  114. <view class="uni-btn-v">
  115. <button type="primary" @click="setEnablePlayGesture()">设置是否开启播放手势(仅限非 Web 平台)</button>
  116. </view>
  117. <view class="uni-btn-v">
  118. <button type="primary" @click="setVslideGesture()">非全屏模式下,设置是否开启亮度与音量调节手势(仅限非 Web 平台)</button>
  119. </view>
  120. <view class="uni-btn-v">
  121. <button type="primary" @click="setVslideGestureInFullscreen()">全屏模式下,设置是否开启亮度与音量调节手势(仅限非 Web 平台)</button>
  122. </view>
  123. <view class="uni-btn-v">
  124. <button type="primary" @click="setCodec(codec)">设置解码器(仅 App 平台,未播放时设置有效)</button>
  125. </view>
  126. <view class="uni-btn-v">
  127. <button type="primary" @click="setHttpCache()">设置是否对http、https视频源开启本地缓存(仅 App 平台,未播放时设置有效)</button>
  128. </view>
  129. <view class="uni-btn-v">
  130. <button type="primary" @click="setPlayStrategy(playStrategy)">设置播放策略(仅 App 平台,未播放时设置有效)</button>
  131. </view>
  132. <view class="uni-btn-v">
  133. <button type="primary" @click="setHeader(header)">设置header</button>
  134. </view>
  135. </scroll-view>
  136. <video v-if="autoTest" :src="localSrc" @error="onError"></video>
  137. </view>
  138. </template>
  139. <script>
  140. export default {
  141. onReady() {
  142. this.videoContext = uni.createVideoContext('video', this);
  143. },
  144. data() {
  145. return {
  146. videoContext: null,
  147. // 属性
  148. src: "https://qiniu-web-assets.dcloud.net.cn/video/sample/2minute-demo.mp4",
  149. autoplay: false,
  150. loop: false,
  151. muted: false,
  152. initialTime: 0,
  153. duration: 60,
  154. controls: true,
  155. danmuList: [
  156. {
  157. text: '要显示的文本',
  158. color: '#FF0000',
  159. time: 3
  160. }, {
  161. text: '要显示的文本2',
  162. color: '#31ff23',
  163. time: 5
  164. }, {
  165. text: '要显示的文本3',
  166. color: '#f13ef8',
  167. time: 7
  168. }, {
  169. text: '要显示的文本4',
  170. color: '#4972f8',
  171. time: 9
  172. }, {
  173. text: '要显示的文本5',
  174. color: '#000000',
  175. time: 11
  176. }
  177. ],
  178. danmuBtn: false,
  179. enableDanmu: true,
  180. pageGesture: false,
  181. direction: -1,
  182. requestFullScreenOptions: {
  183. direction: -90
  184. },
  185. showProgress: true,
  186. showFullscreenBtn: true,
  187. showPlayBtn: true,
  188. showCenterPlayBtn: true,
  189. showLoading: true,
  190. enableProgressGesture: true,
  191. objectFit: "contain",
  192. poster: "https://fs-1319721001.cos.ap-chongqing.myqcloud.com/fs/20240515/23b10c32d51a4bebb6f76f5543430359.jpg",
  193. showMuteBtn: false,
  194. title: "video-component",
  195. enablePlayGesture: false,
  196. vslideGesture: false,
  197. vslideGestureInFullscreen: true,
  198. codec: "hardware",
  199. httpCache: true,
  200. playStrategy: 0,
  201. header: {
  202. 'User-Agent': 'User-Agent test',
  203. 'header': 'header test',
  204. 'cookie': 'cookie test'
  205. },
  206. // API
  207. pos: 10,
  208. rate: 1.5,
  209. danmu: {
  210. text: '要显示的文本',
  211. color: '#FF0000'
  212. },
  213. // 自动化测试
  214. autoTest: false,
  215. isPlaying: false,
  216. isPause: false,
  217. isError: false,
  218. localSrc: ''
  219. }
  220. },
  221. onLoad() {
  222. },
  223. methods: {
  224. // API
  225. play: function () {
  226. console.log("play");
  227. this.videoContext?.play();
  228. },
  229. pause: function () {
  230. console.log("pause");
  231. (uni.getElementById("video"))?.pause(); //as写法测试。注意id不对时as会崩溃
  232. // this.videoContext?.pause();
  233. },
  234. seek: function (pos) {
  235. console.log("seek -> " + pos);
  236. this.videoContext?.seek(pos);
  237. },
  238. requestFullScreen: function (options) {
  239. console.log("requestFullScreen -> " + options);
  240. this.videoContext?.requestFullScreen(options);
  241. },
  242. exitFullScreen: function () {
  243. console.log("exitFullScreen");
  244. this.videoContext?.exitFullScreen();
  245. },
  246. stop: function () {
  247. console.log("stop");
  248. uni.getElementById<UniVideoElement>("video")?.stop(); //泛型写法测试
  249. // this.videoContext?.stop();
  250. },
  251. sendDanmu: function (danmu) {
  252. console.log("sendDanmu -> " + danmu);
  253. this.videoContext?.sendDanmu(danmu);
  254. },
  255. playbackRate: function (rate) {
  256. console.log("playbackRate -> " + rate);
  257. this.videoContext?.playbackRate(rate);
  258. },
  259. // 属性
  260. setSrc: function (src) {
  261. this.src = src;
  262. console.log("src -> " + this.src)
  263. },
  264. setAutoplay: function () {
  265. this.autoplay = !this.autoplay;
  266. console.log("autoplay -> " + this.autoplay)
  267. },
  268. setLoop: function () {
  269. this.loop = !this.loop;
  270. console.log("loop -> " + this.loop)
  271. },
  272. setMuted: function () {
  273. this.muted = !this.muted;
  274. console.log("muted -> " + this.muted)
  275. },
  276. setInitialTime: function (initialTime) {
  277. this.initialTime = initialTime;
  278. console.log("initialTime -> " + this.initialTime)
  279. },
  280. setDuration: function (duration) {
  281. this.duration = duration;
  282. console.log("duration -> " + this.duration)
  283. },
  284. setControls: function () {
  285. this.controls = !this.controls;
  286. console.log("controls -> " + this.controls)
  287. },
  288. setDanmuBtn: function () {
  289. this.danmuBtn = !this.danmuBtn;
  290. console.log("danmuBtn -> " + this.danmuBtn)
  291. },
  292. setPageGesture: function () {
  293. this.pageGesture = !this.pageGesture;
  294. console.log("pageGesture -> " + this.pageGesture)
  295. },
  296. setDirection: function (direction) {
  297. this.direction = direction;
  298. console.log("direction -> " + this.direction)
  299. },
  300. setShowProgress: function () {
  301. this.showProgress = !this.showProgress;
  302. console.log("showProgress -> " + this.showProgress)
  303. },
  304. setShowFullscreenBtn: function () {
  305. this.showFullscreenBtn = !this.showFullscreenBtn;
  306. console.log("showFullscreenBtn -> " + this.showFullscreenBtn)
  307. },
  308. setShowPlayBtn: function () {
  309. this.showPlayBtn = !this.showPlayBtn;
  310. console.log("showPlayBtn -> " + this.showPlayBtn)
  311. },
  312. setShowCenterPlayBtn: function () {
  313. this.showCenterPlayBtn = !this.showCenterPlayBtn;
  314. console.log("showCenterPlayBtn -> " + this.showCenterPlayBtn)
  315. },
  316. setShowLoading: function () {
  317. this.showLoading = !this.showLoading;
  318. console.log("showLoading -> " + this.showLoading)
  319. },
  320. setEnableProgressGesture: function () {
  321. this.enableProgressGesture = !this.enableProgressGesture;
  322. console.log("enableProgressGesture -> " + this.enableProgressGesture)
  323. },
  324. setObjectFit: function (objectFit) {
  325. this.objectFit = objectFit;
  326. console.log("objectFit -> " + this.objectFit)
  327. },
  328. setPoster: function (poster) {
  329. this.poster = poster;
  330. console.log("poster -> " + this.poster)
  331. },
  332. setShowMuteBtn: function () {
  333. this.showMuteBtn = !this.showMuteBtn;
  334. console.log("showMuteBtn -> " + this.showMuteBtn)
  335. },
  336. setTitle: function (title) {
  337. this.title = title;
  338. console.log("title -> " + this.title)
  339. },
  340. setEnablePlayGesture: function () {
  341. this.enablePlayGesture = !this.enablePlayGesture;
  342. console.log("enablePlayGesture -> " + this.enablePlayGesture)
  343. },
  344. setVslideGesture: function () {
  345. this.vslideGesture = !this.vslideGesture;
  346. console.log("vslideGesture -> " + this.vslideGesture)
  347. },
  348. setVslideGestureInFullscreen: function () {
  349. this.vslideGestureInFullscreen = !this.vslideGestureInFullscreen;
  350. console.log("vslideGestureInFullscreen -> " + this.vslideGestureInFullscreen)
  351. },
  352. setCodec: function (codec) {
  353. this.codec = codec;
  354. console.log("codec -> " + this.codec)
  355. },
  356. setHttpCache: function () {
  357. this.httpCache = !this.httpCache;
  358. console.log("httpCache -> " + this.httpCache)
  359. },
  360. setPlayStrategy: function (playStrategy) {
  361. this.playStrategy = playStrategy;
  362. console.log("playStrategy -> " + this.playStrategy)
  363. },
  364. setHeader: function (header) {
  365. this.header = header;
  366. console.log("header -> " + JSON.stringify(this.header))
  367. },
  368. // 事件
  369. onPlay: function (res) {
  370. console.log(res.type);
  371. this.isPlaying = true;
  372. this.isPause = false;
  373. },
  374. onPause: function (res) {
  375. console.log(res.type);
  376. this.isPlaying = false;
  377. this.isPause = true;
  378. },
  379. onEnded: function (res) {
  380. console.log(res.type);
  381. },
  382. onTimeUpdate: function (res) {
  383. console.log(res.type + " -> " + JSON.stringify(res.detail));
  384. },
  385. onFullScreenChange: function (res) {
  386. console.log(res.type + " -> " + JSON.stringify(res.detail));
  387. },
  388. onWaiting: function (res) {
  389. console.log(res.type);
  390. },
  391. onError: function (res) {
  392. console.log(res.type + " -> " + JSON.stringify(res.detail));
  393. this.isError = true;
  394. },
  395. onProgress: function (res) {
  396. console.log(res.type + " onProgress -> " + JSON.stringify(res.detail));
  397. },
  398. onFullScreenClick: function (res) {
  399. console.log(res.type + " -> " + JSON.stringify(res.detail));
  400. },
  401. onControlsToggle: function (res) {
  402. console.log(res.type + " -> " + JSON.stringify(res.detail));
  403. },
  404. onSrcInputConfirm(event) {
  405. this.src = event.detail.value;
  406. },
  407. // 自动化测试
  408. downloadSource() {
  409. uni.downloadFile({
  410. url: 'https://qiniu-web-assets.dcloud.net.cn/video/sample/2minute-demo.mp4',
  411. success: (res) => {
  412. this.localSrc = res.tempFilePath;
  413. this.autoTest = true;
  414. },
  415. fail: (_) => {
  416. this.isError = true;
  417. }
  418. })
  419. }
  420. }
  421. }
  422. </script>
  423. <style>
  424. .video {
  425. width: 100%;
  426. height: 200px;
  427. }
  428. .input {
  429. height: 40px;
  430. background: #FFF;
  431. margin: 5px 0;
  432. }
  433. .uni-btn-v{
  434. margin: 20rpx;
  435. button{
  436. height: 88rpx;
  437. line-height: 88rpx;
  438. border-radius: 10rpx;
  439. }
  440. }
  441. </style>