飞道的博客

微信小程序 视频列表滑动无限循环(仿抖音)

1155人阅读  评论(0)

一、写在前面:

1:安卓ios表现基本一致,不是swiper组件实现,滑动效果流畅不卡顿,实现了列表无限循环。不是使用官方的腾讯视频播放组件,完整代码在下面

2:实现功能:支持位置导航、拨打电话、复制微信号、分享、联系客服、解析腾讯视频地址和拖动视频进度功能,暂未添加评论留言功能,后期会逐步增加

3:关于视频存储的问题,这里增加了腾讯视频方式,可以减少自身存储的相关费用及性能问题,这里要说明一下,并非使用腾讯视频播放组件,那个会有几秒的广告,体验太差,这里是直接拿的腾讯视频源播放地址的mp4地址,并且没有腾讯视频的logo,如下图(将视频上传至腾讯视频,然后存储播放页面地址进行解析就可以了)

先上效果图

 二、开发背景说明

微信小程序中,同一个页面最多支持添加三个video组件,所以就通过数据处理的方式更新显示播放,有人说只写一个也行,如果只有一个那么上下滑动到一半位置的时候是看不到下一个视频内容的,衔接效果不好

三、代码(父级页面)

/*
*   navbar是我自己封装的头部导航组件
    avideo-swiper 为视频列表组件
*/
<navbar parameter='{ {parameter}}' showTitle="{ {showTitle}}" ></navbar>
<avideo-swiper 
  video-list="{ {videoList}}" 
  initial-index="{ {videoIndex}}" 
  bind:change="onChange" 
  duration="{ {duration}}" 
  bind:play="onPlay" 
  bind:wait="onWait"
></avideo-swiper>

他们官方让多写一些内容,要不然就提示内容不佳,我就把代码拿出来了

const app = getApp();

Page({
  data: {
    showTitle:true,
    parameter: {
      'navbar': '1',
      'return': '1',
      'title': '乡村线报',
      'color': true,
      'class': 'trans',
    },
    url: app.globalData.url,
    videoList: [],
    pageNum: 1,
    pageSize: 5,
    videoIndex: 0, //初始播放第几个
    duration: 500, //滑动切换时常
    dataIdxNow:0,  //当前播放第几个,获取分享内容用的
  },
  onLoad(options) {
    if(options.id){
      this.setData({
        shareId:options.id
      })
      // 获取视频详情
      this.getVideoDetail()

    } else {
      // 获取视频列表
      this.getVideoList()
    }
  },
  // 获取视频列表
  getVideoList() {
    let that = this,
      {
        pageSize,
        pageNum
      } = that.data
    wx.request({
      url: that.data.url + 'xxxxxxx&page=' + pageNum + '&limit=' + pageSize,
      header: {
        'content-type': 'application/json'
      },
      success(res){
        if (res.data.code == 0) {
          let list = res.data.data.list;
          list.push({})
          
          let LoadEnd = res.data.data.page_count <= pageNum ? true : false

          that.setData({
            LoadEnd,
            videoList:[...that.data.videoList, ...list]
          })
        }
      }
    })
  },
  // 获取视频详情
  getVideoDetail() {

    let that = this,shareId = that.data.shareId
    wx.request({
      url: that.data.url + 'xxxxxx&id=' + shareId,
      header: {
        'content-type': 'application/json'
      },
      success(res){
        if (res.data.code == 0) {
          let detail = res.data.data.data

          that.setData({
            videoList:[...[],...[detail]]
          })
        }
        that.getVideoList()
      }
    })
  },
  
  onChange(e) {
    let dataIdx = e.detail.dataIdx,
      {
        videoList,
        LoadEnd
      } = this.data

    // 剩余2个时加载下一页
    if ((dataIdx + 2) == videoList.length) {
      this.setData({
        pageNum: LoadEnd ? 1 : this.data.pageNum + 1
      })

      this.getVideoList()
    }

    this.setData({
      dataIdxNow:dataIdx
    })
  },
  onPlay(e) {
  },
  onWait(e) {
  },

  //分享,这里区分点击的是右上角三个点还是页面分享按钮
  onShareAppMessage (res) {
    let title = '',shareInfo=''

    if(res.from == 'button'){
      shareInfo = res.target.dataset.shareinfo;
      
    } else {
      let {videoList,dataIdxNow} = this.data
      shareInfo = videoList[dataIdxNow]
    }

    title = shareInfo.content
    if(shareInfo.province){
        title = title+"ꔷ"+shareInfo.province+""+shareInfo.position
    }

    return {
      title: title,
      path: '/pages/video_swiper/video-swiper?id='+shareInfo.id,
      imageUrl: shareInfo.pic_url
    }
  },
})

 四、代码(avideo-swiper组件)


  
  1. <wxs module= "touch" src= "./touch.wxs"></wxs>
  2. <view class="aswiper">
  3. <view
  4. id= "aswiper__track"
  5. class= "aswiper__track"
  6. bind:touchstart= "{{touch.touchstart}}"
  7. catch:touchmove= "{{touch.touchmove}}"
  8. bind:touchend = "{{touch.touchend}}"
  9. change:trackData= "{{touch.trackDataObserver}}"
  10. trackData= "{{trackData}}"
  11. bind:transitionend= "{{touch.onTransitionEnd}}"
  12. >
  13. <view
  14. wx:for= "{{players}}"
  15. wx:for-item= "player"
  16. wx:for-index= "idx"
  17. wx:key= "id"
  18. class= "aswiper-item aswiper-item--hidden"
  19. >
  20. <!-- 有视频 -->
  21. <view class="aswiper-content" wx:if="{{player.src}}">
  22. <video
  23. id= "{{player.id}}"
  24. class= "aswiper-item-video"
  25. data-player-idx= "{{idx}}"
  26. src= "{{player.src}}"
  27. loop= "{{loop}}"
  28. autoplay= "{{playerIdx == idx ? true :false}}"
  29. object-fit= "{{objectFit}}"
  30. enable-play-gesture= "{{false}}"
  31. enable-progress-gesture= "{{true}}"
  32. show-center-play-btn= "{{false}}"
  33. show-fullscreen-btn= "{{false}}"
  34. show-progress= "{{true}}"
  35. controls= "{{true}}"
  36. bindplay= "onPlay"
  37. bindpause= "onPause"
  38. bindended= "onEnded"
  39. binderror= "onError"
  40. bindtimeupdate= "onTimeUpdate"
  41. bindwaiting= "onWaiting"
  42. bindprogress= "onProgress"
  43. bindloadedmetadata= "onLoadedMetaData"
  44. > </video>
  45. <!-- 视频覆盖层 -->
  46. <view class="video-overlay" data-player-idx="{{idx}}" bind:tap="onVideoOverlayTap">
  47. <view class="aswiper-item-panel" hidden="{{delayShowPanel && !player.scene}}">
  48. <default-panel video="{{curQueue[idx]}}" player-idx="{{idx}}" cur-player-idx="{{playerIdx}}"> </default-panel>
  49. </view>
  50. <!-- 暂停播放按钮 -->
  51. <image hidden="{{!player.scene || player.status !== 2}}" data-player-idx="{{idx}}" class="video-play-btn" src="./image/play-btn.png" mode="aspectFit" catch:tap="onVideoPlayBtnTap" />
  52. </view>
  53. </view>
  54. <!-- 无视频展示广告 -->
  55. <view class="aswiper-content" wx:if="{{player && !player.src}}" >
  56. <ad-custom unit-id="adunit-33e6e9f20e115550" style="height: 100%;"> </ad-custom>
  57. </view>
  58. <view class="aswiper-content__overlay"> </view>
  59. </view>
  60. </view>
  61. </view>

  
  1. const app = getApp()
  2. Component({
  3. properties: {
  4. vertical: {
  5. type: Boolean,
  6. value: true
  7. },
  8. duration: {
  9. type: Number,
  10. value: 500
  11. },
  12. videoList: {
  13. type: Array,
  14. value: []
  15. },
  16. initialIndex: {
  17. type: Number,
  18. value: 0
  19. },
  20. objectFit: {
  21. type: String,
  22. value: 'contain'
  23. },
  24. loop: {
  25. type: Boolean,
  26. value: true
  27. },
  28. autoPlay: {
  29. type: Boolean,
  30. value: true
  31. },
  32. panelType: {
  33. type: String,
  34. value: 'default'
  35. },
  36. width: {
  37. type: Number,
  38. value: 0
  39. },
  40. height: {
  41. type: Number,
  42. value: 0
  43. }
  44. },
  45. data: {
  46. players: [
  47. {
  48. id: 'video_0',
  49. scene: false,
  50. status: 0, // 0: initial; 1: play; 2: pause
  51. src: null,
  52. },
  53. {
  54. id: 'video_1',
  55. scene: false,
  56. status: 0,
  57. src: null,
  58. },
  59. {
  60. id: 'video_2',
  61. scene: false,
  62. status: 0,
  63. src: null,
  64. }
  65. ],
  66. playerIdx: 0,
  67. trackData: {
  68. width: 0,
  69. height: 0,
  70. vertical: true,
  71. duration: 500,
  72. operation: {}
  73. },
  74. curQueue: [{}, {}, {}],
  75. curVideo: null,
  76. navH: 0,
  77. },
  78. observers: {
  79. initialIndex( index) {
  80. if (index < 0) {
  81. throw new Error( 'initialIndex can not be less than 0.');
  82. }
  83. },
  84. videoList( videoList) {
  85. if (! Array. isArray(videoList)) {
  86. throw new Error( 'videoList is expected an array.');
  87. }
  88. },
  89. 'initialIndex, videoList': function ( initialIndex, videoList) {
  90. const operation = {};
  91. if (initialIndex !== this. _initialIndex && videoList. length > 0) {
  92. this. _initialIndex = initialIndex;
  93. this. _dataIdx = initialIndex;
  94. operation. dataIdx = initialIndex;
  95. }
  96. operation. dataCount = videoList. length;
  97. if (! this. _videoList) {
  98. this. _playing = this. data. autoPlay;
  99. }
  100. this. setData(
  101. {
  102. 'trackData.operation': operation
  103. },
  104. () => {
  105. this. loadCurQueue( this. _dataIdx, this. _playing);
  106. }
  107. );
  108. }
  109. },
  110. created( ) {
  111. this. _rect = null;
  112. this. _videoList = null;
  113. this. _initialIndex = - 1;
  114. this. _dataIdx = 0;
  115. this. _lastDataIdx = - 1;
  116. this. _lastVideo = null;
  117. this. _playing = true;
  118. this. _pausing = {
  119. idx: - 1,
  120. timmer: null
  121. };
  122. this. _savedPlayerIdx = - 1;
  123. this. _playerIdx = 0;
  124. this. _isAndroid = wx. getSystemInfoSync(). platform === 'android';
  125. },
  126. attached( ) {
  127. // 创建视频对象
  128. this. _videoContexts = [];
  129. this. data. players. forEach( (item) => {
  130. this. _videoContexts. push(wx. createVideoContext(item. id, this));
  131. })
  132. },
  133. ready( ) {
  134. this. initialize();
  135. },
  136. methods: {
  137. play( ) {
  138. const { curVideo } = this. data;
  139. if (curVideo) {
  140. this. playCurrent( this. _playerIdx);
  141. }
  142. },
  143. pause( ) {
  144. this. _videoContexts. forEach( (ctx) => {
  145. ctx. pause();
  146. });
  147. },
  148. swiperChange( args) {
  149. const dataIdx = args. dataIdx;
  150. this. _dataIdx = dataIdx;
  151. this. loadCurQueue(dataIdx, false);
  152. },
  153. loadCurQueue( dataIdx, playing = false) {
  154. const curQueue = this. data. curQueue. slice( 0);
  155. const { videoList, players } = this. data;
  156. const maxIdx = videoList. length - 1;
  157. let curVideo = null;
  158. let curDataIdx = dataIdx;
  159. let cur = 0;
  160. if (maxIdx < 0) {
  161. curQueue. forEach( (video) => {
  162. video = {};
  163. });
  164. } else {
  165. if (curDataIdx > maxIdx) {
  166. curDataIdx = maxIdx;
  167. }
  168. let preV = {},
  169. nextV = {};
  170. let pre = 0,
  171. next = 0;
  172. cur = curDataIdx % 3;
  173. pre = cur - 1;
  174. if (pre < 0) {
  175. pre = 2;
  176. }
  177. next = cur + 1;
  178. if (next > 2) {
  179. next = 0;
  180. }
  181. if (curDataIdx - 1 >= 0) {
  182. preV = videoList[curDataIdx - 1];
  183. }
  184. if (curDataIdx + 1 <= maxIdx) {
  185. nextV = videoList[curDataIdx + 1];
  186. }
  187. curQueue[pre] = preV;
  188. curQueue[next] = nextV;
  189. curVideo = videoList[curDataIdx];
  190. curQueue[cur] = curVideo;
  191. curVideo = videoList[curDataIdx];
  192. }
  193. for ( let i = 0; i < 3; i++) {
  194. const video = curQueue[i];
  195. const player = players[i];
  196. const src = video. url || null;
  197. player. src = src;
  198. }
  199. this. setData({
  200. players,
  201. curQueue,
  202. curVideo
  203. });
  204. this. _playerIdx = cur;
  205. this. _savedPlayerIdx = - 1;
  206. if (curVideo) {
  207. this. _videoList = videoList;
  208. if (curDataIdx !== this. _lastDataIdx) {
  209. this. _lastDataIdx = curDataIdx;
  210. this. triggerEvent( 'change', {
  211. dataIdx: curDataIdx,
  212. })
  213. }
  214. this. _lastVideo = curVideo;
  215. if (playing && curVideo) {
  216. wx. nextTick( () => {
  217. this. _savedPlayerIdx = cur;
  218. this. playCurrent(cur);
  219. });
  220. }
  221. }
  222. },
  223. // 点击暂停播放
  224. onVideoOverlayTap( e) {
  225. const idx = e. currentTarget. dataset. playerIdx;
  226. const ctx = this. _videoContexts[idx];
  227. const player = this. data. players[idx];
  228. if (player. status === 2) {
  229. if (player. src) {
  230. ctx. play();
  231. }
  232. } else {
  233. ctx. pause();
  234. const status = `players[${idx}].status`;
  235. const scene = `players[${idx}].scene`;
  236. this. setData({
  237. [status]: 2,
  238. [scene]: true
  239. });
  240. }
  241. },
  242. // 点击开始播放
  243. onVideoPlayBtnTap( e) {
  244. const idx = e. currentTarget. dataset. playerIdx;
  245. const ctx = this. _videoContexts[idx];
  246. const player = this. data. players[idx];
  247. if (player. src) {
  248. ctx. play();
  249. }
  250. },
  251. onPlay( e) {
  252. const idx = e. currentTarget. dataset. playerIdx;
  253. const player = this. data. players[idx];
  254. const _pausing = this. _pausing;
  255. const lastStatus = player. status;
  256. this. _playing = true;
  257. if (idx === _pausing. idx) {
  258. clearTimeout(_pausing. timmer);
  259. this. _pausing = {
  260. idx: - 1,
  261. timmer: null
  262. };
  263. }
  264. if (lastStatus !== 1) {
  265. const scene = `players[${idx}].scene`;
  266. const status = `players[${idx}].status`;
  267. this. setData({
  268. [scene]: true,
  269. [status]: 1
  270. });
  271. if (lastStatus === 2) {
  272. this. trigger(e, 'replay');
  273. } else {
  274. this. trigger(e, 'play');
  275. }
  276. }
  277. },
  278. onPause( e) {
  279. const idx = e. currentTarget. dataset. playerIdx;
  280. const player = this. data. players[idx];
  281. this. _playing = false;
  282. if (player. status !== 2) {
  283. const status = `players[${idx}].status`;
  284. this. _pausing = {
  285. idx,
  286. timmer: setTimeout( () => {
  287. this. setData({
  288. [status]: 2
  289. });
  290. this. _pausing = {
  291. idx: - 1,
  292. timmer: null
  293. };
  294. }, 200)
  295. };
  296. }
  297. this. trigger(e, 'pause');
  298. },
  299. onEnded( e) {
  300. this. trigger(e, 'ended');
  301. },
  302. onError( e) {
  303. this. trigger(e, 'error');
  304. },
  305. onTimeUpdate( e) {
  306. this. trigger(e, 'timeupdate');
  307. },
  308. onWaiting( e) {
  309. this. trigger(e, 'wait');
  310. },
  311. onProgress( e) {
  312. this. trigger(e, 'progress');
  313. },
  314. onLoadedMetaData( e) {
  315. this. trigger(e, 'loadedmetadata');
  316. },
  317. trigger( e, type) {
  318. let ext = arguments. length > 2 && arguments[ 2] !== undefined ? arguments[ 2] : {};
  319. let detail = e. detail;
  320. const { curVideo } = this. data;
  321. this. triggerEvent(type, Object. assign( Object. assign({}, detail), { video: curVideo }, ext));
  322. },
  323. playCurrent( cur) {
  324. const { players } = this. data;
  325. this. _videoContexts. forEach( (ctx, idx) => {
  326. const player = players[idx];
  327. if (cur === idx) {
  328. if (player. src) {
  329. ctx. play();
  330. }
  331. } else {
  332. player. scene = false;
  333. player. status = 0;
  334. ctx. stop();
  335. }
  336. });
  337. this. setData({
  338. playerIdx: cur,
  339. players
  340. });
  341. },
  342. onTransitionEnd( ) {
  343. const { curVideo } = this. data;
  344. if ( this. _playerIdx !== this. _savedPlayerIdx) {
  345. if (curVideo) {
  346. this. _savedPlayerIdx = this. _playerIdx;
  347. this. playCurrent( this. _playerIdx);
  348. }
  349. }
  350. },
  351. initialize( ) {
  352. this. getRect( '#aswiper__track'). then( (rect) => {
  353. const { width, height } = this. data;
  354. this. _rect = rect;
  355. this. setData({
  356. 'trackData.width': width,
  357. 'trackData.height': height,
  358. 'trackData.operation': {
  359. rect
  360. }
  361. });
  362. });
  363. },
  364. getRect( selector, all) {
  365. var _this = this;
  366. return new Promise( function ( resolve) {
  367. wx
  368. . createSelectorQuery()
  369. . in(_this)
  370. [all ? 'selectAll' : 'select'](selector)
  371. . boundingClientRect( function ( rect) {
  372. if (all && Array. isArray(rect) && rect. length) {
  373. resolve(rect);
  374. }
  375. if (!all && rect) {
  376. resolve(rect);
  377. }
  378. })
  379. . exec();
  380. });
  381. },
  382. noop( ) {
  383. }
  384. }
  385. });

 有问题请留言


转载:https://blog.csdn.net/fanhu6816/article/details/128081444
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场