inner-audio.js 4.0 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
import {
  onMethod,
  invokeMethod
} from '../../platform'

const eventNames = [
  'canplay',
  'play',
  'pause',
  'stop',
  'ended',
12
  'timeUpdate',
13 14 15 16 17 18
  'error',
  'waiting',
  'seeking',
  'seeked'
]

19
const props = [
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
  {
    name: 'src',
    cache: true
  },
  {
    name: 'startTime',
    default: 0,
    cache: true
  },
  {
    name: 'autoplay',
    default: false,
    cache: true
  },
  {
    name: 'loop',
    default: false,
    cache: true
  },
  {
    name: 'obeyMuteSwitch',
    default: true,
    readonly: true,
    cache: true
  },
  {
    name: 'duration',
    readonly: true
  },
  {
    name: 'currentTime',
    readonly: true
  },
  {
    name: 'paused',
    readonly: true
  },
  {
    name: 'buffered',
    readonly: true
  },
  {
    name: 'volume'
63 64 65 66 67 68
  },
  {
    name: 'sessionCategory'
  },
  {
    name: 'playbackRate',
Q
qiang 已提交
69
    cache: true
70 71 72 73 74 75 76 77 78
  }
]

class InnerAudioContext {
  constructor (id) {
    this.id = id
    this._callbacks = {}
    this._options = {}
    eventNames.forEach(name => {
79
      this._callbacks[name.toLowerCase()] = []
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
    })
    props.forEach(item => {
      const name = item.name
      const data = {
        get () {
          const result = item.cache ? this._options : invokeMethod('getAudioState', {
            audioId: this.id
          })
          const value = name in result ? result[name] : item.default
          return typeof value === 'number' && name !== 'volume' ? value / 1e3 : value
        }
      }
      if (!item.readonly) {
        data.set = function (value) {
          this._options[name] = value
          invokeMethod('setAudioState', Object.assign({}, this._options, {
            audioId: this.id
          }))
        }
      }
      Object.defineProperty(this, name, data)
    })
102
  }
fxy060608's avatar
fxy060608 已提交
103

104 105
  play () {
    this._operate('play')
106
  }
fxy060608's avatar
fxy060608 已提交
107

108 109
  pause () {
    this._operate('pause')
110
  }
fxy060608's avatar
fxy060608 已提交
111

112 113
  stop () {
    this._operate('stop')
114
  }
fxy060608's avatar
fxy060608 已提交
115

116
  seek (position) {
117 118
    this._operate('seek', {
      currentTime: position * 1e3
119
    })
120
  }
fxy060608's avatar
fxy060608 已提交
121

122
  destroy () {
Q
qiang 已提交
123
    clearInterval(this.__timing)
124 125 126 127
    invokeMethod('destroyAudioInstance', {
      audioId: this.id
    })
    delete innerAudioContexts[this.id]
128
  }
fxy060608's avatar
fxy060608 已提交
129

130 131 132 133 134 135 136 137 138 139
  _operate (type, options) {
    invokeMethod('operateAudio', Object.assign({}, options, {
      audioId: this.id,
      operationType: type
    }))
  }
}

eventNames.forEach(item => {
  const name = item[0].toUpperCase() + item.substr(1)
140
  item = item.toLowerCase()
141 142 143 144 145 146 147 148 149 150 151 152
  InnerAudioContext.prototype[`on${name}`] = function (callback) {
    this._callbacks[item].push(callback)
  }
  InnerAudioContext.prototype[`off${name}`] = function (callback) {
    const callbacks = this._callbacks[item]
    const index = callbacks.indexOf(callback)
    if (index >= 0) {
      callbacks.splice(index, 1)
    }
  }
})

153 154 155 156 157 158 159 160 161 162 163
function emit (audio, state, errMsg, errCode) {
  audio._callbacks[state].forEach(callback => {
    if (typeof callback === 'function') {
      callback(state === 'error' ? {
        errMsg,
        errCode
      } : {})
    }
  })
}

164 165 166 167 168 169 170
onMethod('onAudioStateChange', ({
  state,
  audioId,
  errMsg,
  errCode
}) => {
  const audio = innerAudioContexts[audioId]
171 172 173 174 175 176 177 178 179 180 181 182
  if (audio) {
    emit(audio, state, errMsg, errCode)
    if (state === 'play') {
      const oldCurrentTime = audio.currentTime
      audio.__timing = setInterval(() => {
        const currentTime = audio.currentTime
        if (currentTime !== oldCurrentTime) {
          emit(audio, 'timeupdate')
        }
      }, 200)
    } else if (state === 'pause' || state === 'stop' || state === 'error') {
      clearInterval(audio.__timing)
183
    }
184
  }
185 186 187 188 189 190 191 192 193 194 195
})

const innerAudioContexts = Object.create(null)

export function createInnerAudioContext () {
  const {
    audioId
  } = invokeMethod('createAudioInstance')
  const innerAudioContext = new InnerAudioContext(audioId)
  innerAudioContexts[audioId] = innerAudioContext
  return innerAudioContext
196
}