video.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. let innerAudioContext
  2. module.exports = Behavior({
  3. data: {
  4. currentId: '',
  5. sliderValue: 0
  6. },
  7. properties: {},
  8. pageLifetimes: {
  9. hide() {
  10. this.resetAudio()
  11. },
  12. },
  13. lifetimes: {
  14. attached() {
  15. this.innerAudioContext = wx.createInnerAudioContext()
  16. this.innerAudioContext.onTimeUpdate(res => {
  17. console.log(Math.round(this.innerAudioContext.currentTime / this.innerAudioContext.duration * 100));
  18. this.setData({
  19. sliderValue: Math.round(this.innerAudioContext.currentTime / this.innerAudioContext.duration * 100)
  20. })
  21. })
  22. this.innerAudioContext.onEnded(res => {
  23. this.resetAudio()
  24. })
  25. }
  26. },
  27. methods: {
  28. // 开始播放
  29. playVideo({
  30. currentTarget
  31. }) {
  32. this.setData({
  33. currentId: currentTarget.dataset.id
  34. })
  35. },
  36. // 播放音频
  37. playAudio({
  38. currentTarget
  39. }) {
  40. if (this.data.currentId == currentTarget.dataset.id) {
  41. return this.resetAudio()
  42. }
  43. if (this.innerAudioContext) {
  44. this.resetAudio()
  45. }
  46. /* else {
  47. this.innerAudioContext = wx.createInnerAudioContext()
  48. this.innerAudioContext.onEnded(res => {
  49. this.resetAudio()
  50. })
  51. } */
  52. this.innerAudioContext.src = currentTarget.dataset.audio
  53. // this.innerAudioContext.onCanplay(() => {
  54. setTimeout(() => {
  55. this.innerAudioContext.play();
  56. }, 100)
  57. // })
  58. this.setData({
  59. currentId: currentTarget.dataset.id
  60. })
  61. },
  62. // 重置音频
  63. resetAudio() {
  64. if (this.innerAudioContext) {
  65. this.innerAudioContext.stop();
  66. }
  67. this.setData({
  68. currentId: ''
  69. })
  70. },
  71. // 打开评论
  72. openComment({
  73. target
  74. }) {
  75. this.selectComponent('#comment').open(target.dataset.id)
  76. },
  77. // 分享
  78. creatShare(video) {
  79. return new Promise((resolve, reject) => {
  80. let context = wx.createSelectorQuery();
  81. context
  82. .select('#share')
  83. .fields({
  84. node: true,
  85. size: true
  86. }).exec((res) => {
  87. const canvas = res[0].node;
  88. const ctx = canvas.getContext('2d');
  89. const dpr = wx.getSystemInfoSync().pixelRatio;
  90. canvas.width = res[0].width * dpr;
  91. canvas.height = res[0].height * dpr;
  92. ctx.scale(dpr, dpr);
  93. ctx.font = '14px PingFang';
  94. let pic = canvas.createImage();
  95. pic.src = video.userRead.coverImg; //可以是本地,也可以是网络图片
  96. pic.onload = () => {
  97. ctx.drawImage(pic, 0, 0, 375, 211);
  98. }
  99. let peiyin = canvas.createImage();
  100. peiyin.src = '/static/peiyin.jpg';
  101. peiyin.onload = () => {
  102. ctx.drawImage(peiyin, 0, 211, 375, 89);
  103. // 收藏,一个一个渲染
  104. let sc = canvas.createImage();
  105. sc.src = '/static/no_collect.png'
  106. sc.onload = () => {
  107. ctx.drawImage(sc, 12, 220, 20, 20)
  108. ctx.fillText('收藏', 36, 238)
  109. //分享
  110. let fx = canvas.createImage();
  111. fx.src = '/static/share.png'
  112. fx.onload = () => {
  113. ctx.drawImage(fx, 78, 220, 22, 22)
  114. ctx.fillText('分享', 104, 238)
  115. //点赞
  116. let dz = canvas.createImage();
  117. dz.src = video.isLike ? '/static/heart_colored.png' : '/static/heart.png'
  118. dz.onload = () => {
  119. ctx.drawImage(dz, 258, 222, 22, 22)
  120. ctx.fillText(video.likes, 284, 238)
  121. //评论
  122. let pl = canvas.createImage();
  123. pl.src = '/static/comment.png'
  124. pl.onload = () => {
  125. ctx.drawImage(pl, 318, 222, 22, 22)
  126. ctx.fillText(video.commentAmount, 340, 238)
  127. setTimeout(() => {
  128. wx.canvasToTempFilePath({
  129. canvas: canvas,
  130. success(res) {
  131. resolve({
  132. title: '我的新作品发布啦,快来捧场点赞!',
  133. path: `/pages/index?readId=${video.id}&uid=${wx.getStorageSync('uid')}`,
  134. imageUrl: res.tempFilePath
  135. })
  136. },
  137. fail(res) {
  138. reject()
  139. }
  140. }, this)
  141. }, 500)
  142. }
  143. }
  144. }
  145. }
  146. }
  147. })
  148. })
  149. },
  150. }
  151. })