video.vue 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <template>
  2. <view>
  3. <page-head :title="title"></page-head>
  4. <view class="uni-padding-wrap uni-common-mt" v-if="showVideo">
  5. <view>
  6. <video id="myVideo" src="https://img.cdn.aliyun.dcloud.net.cn/guide/uniapp/%E7%AC%AC1%E8%AE%B2%EF%BC%88uni-app%E4%BA%A7%E5%93%81%E4%BB%8B%E7%BB%8D%EF%BC%89-%20DCloud%E5%AE%98%E6%96%B9%E8%A7%86%E9%A2%91%E6%95%99%E7%A8%8B@20181126-lite.m4v"
  7. @error="videoErrorCallback" :danmu-list="danmuList" enable-danmu danmu-btn controls poster="https://vkceyugu.cdn.bspapp.com/VKCEYUGU-dc-site/b1476d40-4e5f-11eb-b997-9918a5dda011.png"></video>
  8. </view>
  9. <!-- #ifndef MP-ALIPAY || MP-TOUTIAO || MP-KUAISHOU || MP-LARK || MP-JD -->
  10. <view class="uni-list uni-common-mt">
  11. <view class="uni-list-cell">
  12. <view>
  13. <view class="uni-label">弹幕内容</view>
  14. </view>
  15. <view class="uni-list-cell-db">
  16. <input v-model="danmuValue" class="uni-input" type="text" placeholder="在此处输入弹幕内容" />
  17. </view>
  18. </view>
  19. </view>
  20. <view class="uni-btn-v">
  21. <button @click="sendDanmu" class="page-body-button">发送弹幕</button>
  22. </view>
  23. <!-- #endif -->
  24. </view>
  25. </view>
  26. </template>
  27. <script>
  28. export default {
  29. data() {
  30. return {
  31. title: 'video',
  32. src: '',
  33. danmuList: [{
  34. text: '第 1s 出现的弹幕',
  35. color: '#ff0000',
  36. time: 1
  37. },
  38. {
  39. text: '第 3s 出现的弹幕',
  40. color: '#ff00ff',
  41. time: 3
  42. }
  43. ],
  44. danmuValue: '',
  45. showVideo: false
  46. }
  47. },
  48. onReady: function(res) {
  49. // #ifndef MP-ALIPAY || MP-TOUTIAO
  50. this.videoContext = uni.createVideoContext('myVideo')
  51. // #endif
  52. // #ifdef APP-PLUS || MP-BAIDU
  53. setTimeout(()=>{
  54. this.showVideo = true
  55. },350)
  56. // #endif
  57. // #ifndef APP-PLUS || MP-BAIDU
  58. this.showVideo = true
  59. // #endif
  60. },
  61. methods: {
  62. sendDanmu: function() {
  63. this.videoContext.sendDanmu({
  64. text: this.danmuValue,
  65. color: this.getRandomColor()
  66. });
  67. this.danmuValue = '';
  68. },
  69. videoErrorCallback: function(e) {
  70. uni.showModal({
  71. content: e.target.errMsg,
  72. showCancel: false
  73. })
  74. },
  75. getRandomColor: function() {
  76. const rgb = []
  77. for (let i = 0; i < 3; ++i) {
  78. let color = Math.floor(Math.random() * 256).toString(16)
  79. color = color.length == 1 ? '0' + color : color
  80. rgb.push(color)
  81. }
  82. return '#' + rgb.join('')
  83. }
  84. }
  85. }
  86. </script>
  87. <style>
  88. video {
  89. width: 690rpx;
  90. width: 100%;
  91. height: 400px;
  92. }
  93. </style>