video.vue 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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://img-cdn-qiniu.dcloud.net.cn/uniapp/doc/poster.png"></video>
  8. </view>
  9. <!-- #ifndef MP-ALIPAY || MP-TOUTIAO -->
  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. </view>
  27. </template>
  28. <script>
  29. export default {
  30. data() {
  31. return {
  32. title: 'video',
  33. src: '',
  34. danmuList: [{
  35. text: '第 1s 出现的弹幕',
  36. color: '#ff0000',
  37. time: 1
  38. },
  39. {
  40. text: '第 3s 出现的弹幕',
  41. color: '#ff00ff',
  42. time: 3
  43. }
  44. ],
  45. danmuValue: '',
  46. showVideo: false
  47. }
  48. },
  49. onReady: function(res) {
  50. // #ifndef MP-ALIPAY || MP-TOUTIAO
  51. this.videoContext = uni.createVideoContext('myVideo')
  52. // #endif
  53. // #ifdef APP-PLUS || MP-BAIDU
  54. setTimeout(()=>{
  55. this.showVideo = true
  56. },350)
  57. // #endif
  58. // #ifndef APP-PLUS || MP-BAIDU
  59. this.showVideo = true
  60. // #endif
  61. },
  62. methods: {
  63. sendDanmu: function() {
  64. this.videoContext.sendDanmu({
  65. text: this.danmuValue,
  66. color: this.getRandomColor()
  67. });
  68. this.danmuValue = '';
  69. },
  70. videoErrorCallback: function(e) {
  71. uni.showModal({
  72. content: e.target.errMsg,
  73. showCancel: false
  74. })
  75. },
  76. getRandomColor: function() {
  77. const rgb = []
  78. for (let i = 0; i < 3; ++i) {
  79. let color = Math.floor(Math.random() * 256).toString(16)
  80. color = color.length == 1 ? '0' + color : color
  81. rgb.push(color)
  82. }
  83. return '#' + rgb.join('')
  84. }
  85. }
  86. }
  87. </script>
  88. <style>
  89. video {
  90. width: 690rpx;
  91. }
  92. </style>