game-recorder.js 2.4 KB
Newer Older
J
junkunzhang 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 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 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
import moduleHelper from './module-helper'
import { uid } from './utils';

const GameRecorderList = {};

export default {
  WX_GetGameRecorder() {
    let obj = wx.getGameRecorder()
    const id = uid()
    GameRecorderList[id] = obj
    return id
  },
  WX_GameRecorderOff(id, eventType) {
    var obj = GameRecorderList[id]
    if(!obj || typeof obj.onList === 'undefined' || typeof obj.onList[eventType] === 'undefined') {
      return;
    }
    for (let key in Object.keys(obj.onList[eventType])) {
      const callback = obj.onList[eventType][key];
      if (callback) {
        obj.off(eventType, callback)
      }
    }
    obj.onList[eventType] = {}
  },
  WX_GameRecorderOn(id, eventType) {
    var obj = GameRecorderList[id]
    if (!obj) return '';
    if (!obj.onList) {
      obj.onList = {
        start: {},
        stop: {},
        pause: {},
        resume: {},
        abort: {},
        timeUpdate: {},
        error: {},
      }
    }
    const callbackId = uid();
    let callback = (res) => {
      let result = '';
      if (res) {
        result = JSON.stringify(res);
      }
      var resStr = JSON.stringify({
        id,
        res: JSON.stringify({
          eventType,
          result,
        }),
      })
      moduleHelper.send('_OnGameRecorderCallback', resStr)
    }
    if (obj.onList[eventType]) {
      obj.onList[eventType][callbackId] = callback
      obj.on(eventType, callback)
      return callbackId;
    }
    return '';
  },
  WX_GameRecorderStart(id, option) {
    var obj = GameRecorderList[id]
    if (obj) {
      obj.start(JSON.parse(option))
    }
  },
  WX_GameRecorderAbort(id){
    var obj = GameRecorderList[id];
    if(obj){
        obj.abort();
    }
  },
  WX_GameRecorderPause(id){
      var obj = GameRecorderList[id];
      if(obj){
          obj.pause();
      }
  },
  WX_GameRecorderResume(id){
      var obj = GameRecorderList[id];
      if(obj){
          obj.resume();
      }
  },
  WX_GameRecorderStop(id){
      var obj = GameRecorderList[id];
      if(obj){
          obj.stop();
      }
  },
  WX_OperateGameRecorderVideo(option){
    if(typeof wx.operateGameRecorderVideo !== 'undefined') {
      const data = JSON.parse(option);
      data.fail = (res) => {
        console.error(res);
      }
      wx.operateGameRecorderVideo(data);
    }
  },
}