video.js 5.0 KB

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