index.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. Component({
  2. options: {
  3. addGlobalClass: true,
  4. // 指定所有 _ 开头的数据字段为纯数据字段
  5. pureDataPattern: /^_/
  6. },
  7. properties: {
  8. duration: {
  9. type: Number,
  10. value: 500
  11. },
  12. easingFunction: {
  13. type: String,
  14. value: 'default'
  15. },
  16. loop: {
  17. type: Boolean,
  18. value: true
  19. },
  20. targetPlayIndex: {
  21. type: Number,
  22. value: -1,
  23. observer: function (newVal) {
  24. if (newVal >= 0) {
  25. this.targetPlay(newVal)
  26. }
  27. }
  28. },
  29. // {id, url, objectFit}
  30. videoList: {
  31. type: Array,
  32. value: [],
  33. observer: function (newVal = []) {
  34. this._videoListChanged(newVal)
  35. }
  36. }
  37. },
  38. data: {
  39. nextQueue: [], // 待播放列表
  40. prevQueue: [], // 已播放列表
  41. curQueue: [], // 当前播放列表
  42. circular: false, // 是否循环
  43. _last: 0, // 上一次显示
  44. _maxCount: -1,
  45. _dataIdx: 0,
  46. initIndex: 0,
  47. playerIdx: 0,
  48. playing: false,
  49. _change: -1,
  50. _invalidUp: 0,
  51. _invalidDown: 0,
  52. _videoContexts: []
  53. },
  54. lifetimes: {
  55. attached() {
  56. this.data._videoContexts = [
  57. wx.createVideoContext('video_0', this),
  58. wx.createVideoContext('video_1', this),
  59. wx.createVideoContext('video_2', this)
  60. ]
  61. }
  62. },
  63. created() {
  64. this._swipering = false
  65. },
  66. methods: {
  67. // 添加新的视频源
  68. _videoListChanged(newVal) {
  69. const data = this.data
  70. newVal.forEach(item => {
  71. data.nextQueue.push(item)
  72. })
  73. const maxCount = newVal.length
  74. if (data.curQueue.length === 0) {
  75. this.setData({
  76. curQueue: data.nextQueue.splice(0, 3),
  77. _maxCount: maxCount
  78. }, () => {
  79. this.playCurrent(0)
  80. })
  81. }
  82. },
  83. animationfinish(e) {
  84. const {
  85. _last,
  86. _change,
  87. curQueue,
  88. prevQueue,
  89. nextQueue
  90. } = this.data
  91. const current = e.detail.current
  92. const diff = current - _last
  93. this._swipering = false
  94. this.data._last = current
  95. this.playCurrent(current)
  96. const direction = (diff === 1 || diff === -2) ? 'up' : 'down'
  97. console.log('direction', direction)
  98. if (direction === 'up') {
  99. if (this.data._invalidDown === 0) {
  100. this.data._dataIdx++
  101. if (this.data._dataIdx >= this.data._maxCount) {
  102. this.data._dataIdx = 0
  103. }
  104. this.loadCurQueue(this.data._dataIdx, current)
  105. } else {
  106. this.data._invalidDown -= 1
  107. }
  108. }
  109. if (direction === 'down') {
  110. if (this.data._invalidUp === 0) {
  111. this.data._dataIdx--
  112. if (this.data._dataIdx < 0) {
  113. this.data._dataIdx = this.data._maxCount - 1
  114. }
  115. this.loadCurQueue(this.data._dataIdx, current)
  116. } else {
  117. this.data._invalidUp -= 1
  118. }
  119. }
  120. this.triggerEvent('change', {
  121. activeId: curQueue[current].id
  122. })
  123. let circular = true
  124. // if (nextQueue.length === 0 && current !== 0) {
  125. // circular = false
  126. // }
  127. // if (prevQueue.length === 0 && current !== 2) {
  128. // circular = false
  129. // }
  130. this.setData({
  131. curQueue,
  132. circular,
  133. })
  134. },
  135. loadCurQueue(dataIdx, playerIdx) {
  136. const {
  137. _maxCount,
  138. curQueue,
  139. videoList
  140. } = this.data
  141. const current = playerIdx;
  142. let curDataIdx = dataIdx;
  143. if (curDataIdx > _maxCount) {
  144. curDataIdx = _maxCount;
  145. }
  146. let pre = 0,
  147. next = 0,
  148. preV, nextV, curVideo;
  149. pre = current - 1;
  150. if (pre < 0) {
  151. pre = 2;
  152. }
  153. next = current + 1;
  154. if (next > 2) {
  155. next = 0;
  156. }
  157. if (curDataIdx - 1 >= 0) {
  158. preV = videoList[curDataIdx - 1];
  159. } else {
  160. preV = videoList[_maxCount - 1];
  161. }
  162. console.log('curDataIdx + 1 ', curDataIdx + 1)
  163. if (curDataIdx + 1 < _maxCount) {
  164. nextV = videoList[curDataIdx + 1];
  165. } else {
  166. nextV = videoList[0];
  167. }
  168. curVideo = videoList[curDataIdx];
  169. curQueue[pre] = preV
  170. curQueue[next] = nextV
  171. curQueue[current] = curVideo
  172. console.log('preV', preV)
  173. console.log('curVideo', curVideo)
  174. console.log('nextV', nextV)
  175. console.log('curQueue', curQueue)
  176. },
  177. /**
  178. * 指定视频播放
  179. * @param {videoList(index)} dataIdx
  180. */
  181. targetPlay(dataIdx) {
  182. let curQueue = this.data.videoList.slice();
  183. if (dataIdx >= this.data._maxCount) {
  184. this._dataIdx = dataIdx = this.data._maxCount - 1
  185. }
  186. if (this.data._maxCount - dataIdx < 3) {
  187. if (this.data._maxCount - dataIdx === 2) {
  188. const last = this.data.videoList.slice().shift();
  189. curQueue = curQueue.splice(dataIdx, 2).concat(last)
  190. }
  191. if (this.data._maxCount - dataIdx === 1) {
  192. const first = this.data.videoList.slice().pop();
  193. curQueue = [first].concat(curQueue.splice(0, 2));
  194. }
  195. } else {
  196. curQueue = curQueue.splice(dataIdx, 3)
  197. }
  198. this.setData({
  199. curQueue: curQueue,
  200. _dataIdx: dataIdx,
  201. playerIdx: 0
  202. }, () => {
  203. this.playCurrent(0)
  204. })
  205. },
  206. // 避免卡顿
  207. playCurrent(current) {
  208. this.data._videoContexts.forEach((ctx, index) => {
  209. index !== current ? ctx.pause() : ctx.play()
  210. })
  211. this.setData({
  212. playing: true,
  213. playerIdx: current
  214. }, () => {
  215. // this.data._videoContexts.forEach((ctx, index) => {
  216. // index !== current ? ctx.seek(0) : null
  217. // })
  218. })
  219. // // 重置所有视频pause时重置时间
  220. // setTimeout(() => {
  221. // this.data._videoContexts.forEach((ctx, index) => {
  222. // index !== current ? ctx.seek(0) : null
  223. // })
  224. // }, 100)
  225. },
  226. onPlay(e) {
  227. this.trigger(e, 'play')
  228. },
  229. onPause(e) {
  230. this.trigger(e, 'pause')
  231. },
  232. onEnded(e) {
  233. this.trigger(e, 'ended')
  234. },
  235. onError(e) {
  236. this.trigger(e, 'error')
  237. },
  238. onTimeUpdate(e) {
  239. this.trigger(e, 'timeupdate')
  240. },
  241. onWaiting(e) {
  242. this.trigger(e, 'wait')
  243. },
  244. onProgress(e) {
  245. this.trigger(e, 'progress')
  246. },
  247. onLoadedMetaData(e) {
  248. this.trigger(e, 'loadedmetadata')
  249. },
  250. trigger(e, type, ext = {}) {
  251. const detail = e.detail
  252. const activeId = e.target.dataset.id
  253. this.triggerEvent(type, Object.assign({
  254. ...detail,
  255. activeId
  256. }, ext))
  257. },
  258. onVideoOverlayTap() {
  259. if (this.data.playing) {
  260. this.data._videoContexts.forEach((ctx, index) => {
  261. ctx.pause()
  262. })
  263. this.setData({
  264. playing: false
  265. })
  266. } else {
  267. this.data._videoContexts.forEach((ctx, index) => {
  268. index !== this.data.playerIdx ? ctx.pause() : ctx.play()
  269. })
  270. this.setData({
  271. playing: true
  272. })
  273. }
  274. },
  275. onSwiping() {
  276. this._swipering = true
  277. }
  278. }
  279. })