video.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. function getRandomColor() {
  2. const rgb = []
  3. for (let i = 0; i < 3; ++i) {
  4. let color = Math.floor(Math.random() * 256).toString(16)
  5. color = color.length === 1 ? '0' + color : color
  6. rgb.push(color)
  7. }
  8. return '#' + rgb.join('')
  9. }
  10. Page({
  11. onShareAppMessage() {
  12. return {
  13. title: 'video',
  14. path: 'page/component/pages/video/video'
  15. }
  16. },
  17. onReady() {
  18. this.videoContext = wx.createVideoContext('myVideo')
  19. },
  20. inputValue: '',
  21. data: {
  22. src: '',
  23. danmuList:
  24. [{
  25. text: '第 1s 出现的弹幕',
  26. color: '#ff0000',
  27. time: 1
  28. }, {
  29. text: '第 3s 出现的弹幕',
  30. color: '#ff00ff',
  31. time: 3
  32. }]
  33. },
  34. bindInputBlur(e) {
  35. this.inputValue = e.detail.value
  36. },
  37. bindButtonTap() {
  38. const that = this
  39. wx.chooseVideo({
  40. sourceType: ['album', 'camera'],
  41. maxDuration: 60,
  42. camera: ['front', 'back'],
  43. success(res) {
  44. that.setData({
  45. src: res.tempFilePath
  46. })
  47. }
  48. })
  49. },
  50. bindSendDanmu() {
  51. this.videoContext.sendDanmu({
  52. text: this.inputValue,
  53. color: getRandomColor()
  54. })
  55. },
  56. videoErrorCallback(e) {
  57. console.log('视频错误信息:')
  58. console.log(e.detail.errMsg)
  59. }
  60. })