index.js 6.8 KB

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