inner-audio.js 4.1 KB
Newer Older
1 2
import getRealPath from 'uni-platform/helpers/get-real-path'

fxy060608's avatar
fxy060608 已提交
3 4 5
/**
 * 可以批量设置的监听事件
 */
6 7 8 9
const innerAudioContextEventNames = ['onCanplay', 'onPlay', 'onPause', 'onStop', 'onEnded', 'onTimeUpdate', 'onError', 'onWaiting', 'onSeeking', 'onSeeked']

const innerAudioContextOffEventNames = ['offCanplay', 'offPlay', 'offPause', 'offStop', 'offEnded', 'offTimeUpdate', 'offError', 'offWaiting', 'offSeeking', 'offSeeke']

fxy060608's avatar
fxy060608 已提交
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
/**
 * 音频上下文对象
 */
class InnerAudioContext {
  /**
   * 原始音频对象
   */
  _audio
  /**
   * 是否暂停中
   */
  _stoping
  /**
   * 开始时间
   */
  startTime
  /**
   * 事件监听
   */
  _events
30 31 32 33
  /**
   * 音频地址
   */
  _src
fxy060608's avatar
fxy060608 已提交
34 35 36 37 38 39 40
  /**
   * 音频上下文初始化
   */
  constructor () {
    var audio = this._audio = new Audio()
    this._stoping = false
    // 和audio对象同名同效果的属性
41 42 43 44 45
    var propertys = ['src', 'autoplay', 'loop', 'duration', 'currentTime', 'paused', 'volume']
    propertys.forEach((property) => {
      Object.defineProperty(this, property, {
        set: property === 'src' ? (src) => {
          audio.src = getRealPath(src)
Q
qiang 已提交
46 47
          this._src = src
          return src
48
        } : (val) => {
Q
qiang 已提交
49 50
          audio[property] = val
          return val
fxy060608's avatar
fxy060608 已提交
51
        },
52 53 54 55
        get: property === 'src' ? () => {
          return this._src
        } : () => {
          return audio[property]
fxy060608's avatar
fxy060608 已提交
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
        }
      })
    })
    this.startTime = 0
    Object.defineProperty(this, 'obeyMuteSwitch', {
      set (val) {
        return false
      },
      get () {
        return false
      }
    })
    Object.defineProperty(this, 'buffered', {
      get () {
        var buffered = audio.buffered
        if (buffered.length) {
72
          return buffered.end(buffered.length - 1)
fxy060608's avatar
fxy060608 已提交
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 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
        } else {
          return 0
        }
      }
    })
    // 初始化事件监听列表
    this._events = {}
    innerAudioContextEventNames.forEach(eventName => {
      this._events[eventName] = []
    })
    audio.addEventListener('loadedmetadata', () => {
      var startTime = Number(this.startTime) || 0
      if (startTime > 0) {
        audio.currentTime = startTime
      }
    })
    // 和audio对象同名同效果的事件
    var eventNames = ['canplay', 'play', 'pause', 'ended', 'timeUpdate', 'error', 'waiting', 'seeking', 'seeked']
    var stopEventNames = ['pause', 'seeking', 'seeked', 'timeUpdate']
    eventNames.forEach(eventName => {
      audio.addEventListener(eventName.toLowerCase(), () => {
        // stop事件过滤
        if (this._stoping && stopEventNames.indexOf(eventName) >= 0) {
          return
        }
        this._events[`on${eventName.substr(0, 1).toUpperCase()}${eventName.substr(1)}`].forEach((callback) => {
          callback()
        })
      }, false)
    })
  }
  /**
   * 播放
   */
  play () {
    this._stoping = false
    this._audio.play()
  }
  /**
   * 暂停
   */
  pause () {
    this._audio.pause()
  }
  /**
   * 停止
   */
  stop () {
    this._stoping = true
    this._audio.pause()
    this._audio.currentTime = 0
    this._events.onStop.forEach((callback) => {
      callback()
    })
  }
  /**
   * 跳转到
   * @param {number} position
   */
  seek (position) {
    this._stoping = false
    position = Number(position)
    if (typeof position === 'number' && !isNaN(position)) {
      this._audio.currentTime = position
    }
  }
  /**
   * 销毁
   */
  destroy () {
    this.stop()
  }
}

// 批量设置音频上下文事件监听方法
innerAudioContextEventNames.forEach((eventName) => {
  InnerAudioContext.prototype[eventName] = function (callback) {
    if (typeof callback === 'function') {
151
      this._events[eventName].push(callback)
fxy060608's avatar
fxy060608 已提交
152 153 154 155
    }
  }
})

156 157 158 159 160 161 162 163 164 165 166
// 批量设置音频上下文事件取消监听方法
innerAudioContextOffEventNames.forEach((eventName) => {
  InnerAudioContext.prototype[eventName] = function (callback) {
    var handle = this._events[eventName.replace('off', 'on')]
    var index = handle.indexOf(callback)
    if (index >= 0) {
      handle.splice(index, 1)
    }
  }
})

fxy060608's avatar
fxy060608 已提交
167 168 169 170 171 172
/**
 * 创建音频上下文
 */
export function createInnerAudioContext () {
  return new InnerAudioContext()
}