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. _dataIdx
  91. } = this.data
  92. const current = e.detail.current
  93. const diff = current - _last
  94. this._swipering = false
  95. this.playCurrent(current)
  96. const change = current !== this.data._last
  97. if (diff === 0 || !change) {
  98. return
  99. }
  100. const direction = (diff === 1 || diff === -2) ? 'up' : 'down'
  101. console.log('direction', direction)
  102. if (direction === 'up') {
  103. if (this.data._invalidDown === 0) {
  104. this.data._dataIdx++
  105. if (this.data._dataIdx >= this.data._maxCount) {
  106. this.data._dataIdx = 0
  107. }
  108. this.loadCurQueue(this.data._dataIdx, current)
  109. } else {
  110. this.data._invalidDown -= 1
  111. }
  112. }
  113. if (direction === 'down') {
  114. if (this.data._invalidUp === 0) {
  115. this.data._dataIdx--
  116. if (this.data._dataIdx < 0) {
  117. this.data._dataIdx = this.data._maxCount - 1
  118. }
  119. this.loadCurQueue(this.data._dataIdx, current)
  120. } else {
  121. this.data._invalidUp -= 1
  122. }
  123. }
  124. this.triggerEvent('change', {
  125. activeId: curQueue[current].id
  126. })
  127. let circular = true
  128. this.data._last = current
  129. // if (_dataIdx === 0) {
  130. // circular = false
  131. // }
  132. // if (prevQueue.length === 0 && current !== 2) {
  133. // circular = false
  134. // }
  135. this.setData({
  136. curQueue,
  137. circular,
  138. })
  139. },
  140. loadCurQueue(dataIdx, playerIdx) {
  141. const {
  142. _maxCount,
  143. curQueue,
  144. videoList
  145. } = this.data
  146. const current = playerIdx;
  147. let curDataIdx = dataIdx;
  148. if (curDataIdx > _maxCount) {
  149. curDataIdx = _maxCount;
  150. }
  151. let pre = 0,
  152. next = 0,
  153. preV, nextV, curVideo;
  154. pre = current - 1;
  155. if (pre < 0) {
  156. pre = 2;
  157. }
  158. next = current + 1;
  159. if (next > 2) {
  160. next = 0;
  161. }
  162. if (curDataIdx - 1 >= 0) {
  163. preV = videoList[curDataIdx - 1];
  164. } else {
  165. preV = videoList[_maxCount - 1];
  166. }
  167. console.log('curDataIdx + 1 ', curDataIdx + 1)
  168. if (curDataIdx + 1 < _maxCount) {
  169. nextV = videoList[curDataIdx + 1];
  170. } else {
  171. nextV = videoList[0];
  172. }
  173. curVideo = videoList[curDataIdx];
  174. curQueue[pre] = preV
  175. curQueue[next] = nextV
  176. curQueue[current] = curVideo
  177. },
  178. /**
  179. * 指定视频播放
  180. * @param {videoList(index)} dataIdx
  181. */
  182. targetPlay(dataIdx) {
  183. let curQueue = this.data.videoList.slice();
  184. if (dataIdx >= this.data._maxCount) {
  185. this._dataIdx = dataIdx = this.data._maxCount - 1
  186. }
  187. if (this.data._maxCount - dataIdx < 3) {
  188. if (this.data._maxCount - dataIdx === 2) {
  189. const last = this.data.videoList.slice().shift();
  190. curQueue = curQueue.splice(dataIdx, 2).concat(last)
  191. }
  192. if (this.data._maxCount - dataIdx === 1) {
  193. const first = this.data.videoList.slice().pop();
  194. curQueue = [first].concat(curQueue.splice(0, 2));
  195. }
  196. } else {
  197. curQueue = curQueue.splice(dataIdx, 3)
  198. }
  199. this.setData({
  200. curQueue: curQueue,
  201. _dataIdx: dataIdx,
  202. playerIdx: 0
  203. }, () => {
  204. this.playCurrent(0)
  205. })
  206. },
  207. // 避免卡顿
  208. playCurrent(current) {
  209. this.data._videoContexts.forEach((ctx, index) => {
  210. index !== current ? ctx.pause() : ctx.play()
  211. })
  212. this.setData({
  213. playing: true,
  214. playerIdx: current
  215. }, () => {
  216. this.data._videoContexts.forEach((ctx, index) => {
  217. index !== current ? ctx.seek(0) : null
  218. })
  219. })
  220. // // 重置所有视频pause时重置时间
  221. // setTimeout(() => {
  222. // this.data._videoContexts.forEach((ctx, index) => {
  223. // index !== current ? ctx.seek(0) : null
  224. // })
  225. // }, 100)
  226. },
  227. onPlay(e) {
  228. this.trigger(e, 'play')
  229. },
  230. onPause(e) {
  231. this.trigger(e, 'pause')
  232. },
  233. onEnded(e) {
  234. this.trigger(e, 'ended')
  235. },
  236. onError(e) {
  237. this.trigger(e, 'error')
  238. },
  239. onTimeUpdate(e) {
  240. this.trigger(e, 'timeupdate')
  241. },
  242. onWaiting(e) {
  243. this.trigger(e, 'wait')
  244. },
  245. onProgress(e) {
  246. this.trigger(e, 'progress')
  247. },
  248. onLoadedMetaData(e) {
  249. this.trigger(e, 'loadedmetadata')
  250. },
  251. trigger(e, type, ext = {}) {
  252. const detail = e.detail
  253. const activeId = e.target.dataset.id
  254. this.triggerEvent(type, Object.assign({
  255. ...detail,
  256. activeId
  257. }, ext))
  258. },
  259. onVideoOverlayTap() {
  260. if (this.data.playing) {
  261. this.data._videoContexts.forEach((ctx, index) => {
  262. ctx.pause()
  263. })
  264. this.setData({
  265. playing: false
  266. })
  267. } else {
  268. this.data._videoContexts.forEach((ctx, index) => {
  269. index !== this.data.playerIdx ? ctx.pause() : ctx.play()
  270. })
  271. this.setData({
  272. playing: true
  273. })
  274. }
  275. },
  276. onSwiping() {
  277. this._swipering = true
  278. }
  279. }
  280. })