index.js 2.95 KB
Newer Older
侯体倬 committed
1
const app = getApp();
李嘉林 committed
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
const componentOptions = {
  // 组件选项
  options: {
    multipleSlots: true
  },
  behaviors: [],
  properties: {
    datas: {
      type: Object
    },
    indexs: {
      type:Number
    }
  },
  // 组件数据
  data: {
    isPageHidden: false, // 页面是否处于隐藏状态
    isPlay: false,
20 21
    videoContext: "",
    firstPlay: false,
李嘉林 committed
22 23 24 25 26
  },
  // 数据监听器
  observers: {},
  // 组件方法
  methods: {
李嘉林 committed
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
    init() { },
    getScroll(el) {
      let { autoPlay, scrollPlay } = this.data.datas.componentData;
      if (!scrollPlay) return;
      let query = wx.createSelectorQuery();
      query.select('.VideoPlayer'+this.data.indexs).boundingClientRect(rect=>{
        console.log(rect,'----------rect')
        let videoPlayerTop = Math.ceil(rect.top);
        let videoPlayerHeight = rect.height;
        const sysInfo = wx.getSystemInfoSync()
        let windowHeight = sysInfo.windowHeight / 2;
        if ((videoPlayerTop <= windowHeight) && (videoPlayerTop + videoPlayerHeight > windowHeight)) {
          console.log("开始播放");
          this.autoVideo(0);
        } else {
          // 距离顶部高度绝对值大于组件高度即为在可视范围外
          console.log("暂停播放");
          this.autoVideo(1);
        }
      }).exec();
    },
    autoVideo(type) {
      if (type == 0) {
        // 开始播放
        this.videoContext.play();
52
        this.setData({ isPlay: true,firstPlay:true });
李嘉林 committed
53 54 55 56 57 58
      } else if (type == 1) {
        // 暂停播放
        this.videoContext.pause();
        this.setData({ isPlay: false });
      }
    },
侯体倬 committed
59 60
    playVideo(e) {
      app.trackCpn(e, this.data.datas.componentName)
李嘉林 committed
61 62 63 64 65 66 67 68 69 70 71 72 73
      // 视频播放
      if (this.data.datas.componentData['nativeControl']) {
        return
      }
      let { isPlay } = this.data;
      if (isPlay) {
        console.log("暂停")
        this.videoContext.pause();
      } else {
        console.log("播放")
        this.videoContext.play();
      }
      isPlay = !isPlay;
74
      this.setData({ isPlay,firstPlay: true });
李嘉林 committed
75 76 77 78
    }
  },
  // 组件生命周期
  lifetimes: {
李嘉林 committed
79 80
    created() {
    },
李嘉林 committed
81 82 83 84 85
    attached() {
      this.init();
    },
    ready() {
      this.videoContext = wx.createVideoContext(`videoId${this.data.indexs}`,this);
李嘉林 committed
86 87 88 89
      if (this.data.datas.componentData['autoPlay']) {
        this.setData({isPlay: true})
      }
      console.log(this.videoContext,'---this.videoContext')
李嘉林 committed
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
    },
    moved() {},
    detached() {}
  },
  definitionFilter() {},
  // 页面生命周期
  pageLifetimes: {
    // 页面被展示
    show() {
      const { isPageHidden } = this.data;

      // show事件发生前,页面不是处于隐藏状态时
      if (!isPageHidden) {
        return;
      }

      // 重新执行定时器等操作
    },
    // 页面被隐藏
    hide() {
      this.setData({
        isPageHidden: true
      });

      // 清除定时器等操作
    },
    // 页面尺寸变化时
    resize() {}
  }
};

Component(componentOptions);