index.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. import {
  2. getreadInfo
  3. } from '~/api/video'
  4. let rowHeight = 0
  5. let videoContext = null
  6. let stl = null
  7. let setTimeoutObj = null
  8. Page({
  9. data: {
  10. videoInfo: {},
  11. currentRow: null,
  12. state: false,
  13. countDown: {
  14. state: false,
  15. num: 3,
  16. },
  17. scrollTop: 0,
  18. article: [{
  19. id: 1,
  20. text: '传说在很久很久以前,',
  21. time: '0'
  22. }, {
  23. id: 2,
  24. text: '天和地还没有分开,',
  25. time: '4010'
  26. }, {
  27. id: 3,
  28. text: '整个宇宙混沌一团,',
  29. time: '7080'
  30. }, {
  31. id: 4,
  32. text: '像个大鸡蛋。',
  33. time: '10150'
  34. }, {
  35. id: 5,
  36. text: '有个叫盘古的大神,',
  37. time: '13150'
  38. }, {
  39. id: 6,
  40. text: '昏睡了一万八千年。',
  41. time: '16190'
  42. }, {
  43. id: 7,
  44. text: '一天,大神醒来,睁眼一看,',
  45. time: '20030'
  46. }, {
  47. id: 8,
  48. text: '周围黑乎乎一片,',
  49. time: '24210'
  50. }, {
  51. id: 9,
  52. text: '什么也看不见。',
  53. time: '27300'
  54. }, {
  55. id: 10,
  56. text: '他一使劲翻身坐了起来,',
  57. time: '29210'
  58. }, {
  59. id: 11,
  60. text: '只听“咔嚓”一声,',
  61. time: '32700'
  62. }, {
  63. id: 12,
  64. text: '“大鸡蛋”裂开了一条缝,',
  65. time: '35320'
  66. }, {
  67. id: 13,
  68. text: '一丝微光透了进来。',
  69. time: '38270'
  70. }, ]
  71. },
  72. onLoad(options) {
  73. let videoId = options.videoId
  74. this.getreadInfo(videoId)
  75. let data = this.data.article
  76. data = data.map((item, index) => {
  77. item.readTime = data[index + 1] ? data[index + 1].time - item.time : ''
  78. return item
  79. })
  80. this.setData({
  81. article: data
  82. })
  83. var query = wx.createSelectorQuery();
  84. query.select('.row').boundingClientRect((rect) => {
  85. this.rowHeight = rect.height
  86. console.log(rect);
  87. }).exec()
  88. this.videoContext = wx.createVideoContext('myVideo')
  89. },
  90. async getreadInfo(videoId) {
  91. let videoInfo = await getreadInfo(videoId)
  92. console.log(videoInfo);
  93. wx.setNavigationBarTitle({
  94. title: videoInfo.userRead.title
  95. })
  96. this.setData({
  97. videoInfo
  98. })
  99. },
  100. // 开始录制
  101. setCountDown() {
  102. if (this.data.state) {
  103. return
  104. }
  105. this.setData({
  106. 'countDown.state': true
  107. })
  108. this.stl = setInterval(() => {
  109. if (this.data.countDown.num == 0) {
  110. clearInterval(this.stl)
  111. this.setData({
  112. state: true,
  113. countDown: {
  114. state: false,
  115. num: 3
  116. }
  117. })
  118. this.videoContext.play()
  119. this.startRecording()
  120. } else {
  121. this.setData({
  122. 'countDown.num': --this.data.countDown.num
  123. })
  124. }
  125. }, 1000)
  126. },
  127. startRecording() {
  128. console.log('递归');
  129. if (this.data.currentRow == null) {
  130. this.setData({
  131. currentRow: 0
  132. })
  133. }
  134. let row = this.data.article[this.data.currentRow]
  135. if (!row.readTime) {
  136. return
  137. }
  138. this.setTimeoutObj = setTimeout(() => {
  139. this.setData({
  140. currentRow: ++this.data.currentRow
  141. })
  142. this.setData({
  143. scrollTop: this.rowHeight * this.data.currentRow
  144. })
  145. this.startRecording()
  146. },
  147. row.readTime);
  148. },
  149. // 视频播放结束
  150. videoEnd() {
  151. this.setData({
  152. currentRow: null,
  153. state: false,
  154. scrollTop: 0,
  155. })
  156. },
  157. /**
  158. * 生命周期函数--监听页面卸载
  159. */
  160. onUnload() {
  161. clearTimeout(this.setTimeoutObj)
  162. clearInterval(this.stl)
  163. },
  164. onShareAppMessage() {
  165. }
  166. })