background-audio.js 4.4 KB
Newer Older
fxy060608's avatar
fxy060608 已提交
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
import {
  getRealPath
} from '../util'

import {
  publish
} from '../../bridge'

let audio

const publishBackgroundAudioStateChange = (state, res = {}) => publish('onBackgroundAudioStateChange', Object.assign({
  state
}, res))

const events = ['play', 'pause', 'ended', 'stop']

function initMusic () {
  if (audio) {
    return
  }
  audio = plus.audio.createPlayer({
    autoplay: true,
    backgroundControl: true
  })
  audio.src = audio.title = audio.epname = audio.singer = audio.coverImgUrl = audio.webUrl = ''
  audio.startTime = 0
  events.forEach(event => {
    audio.addEventListener(event, () => {
      // 添加 isStopped 属性是为了解决 安卓设备停止播放后获取播放进度不正确的问题
      if (event === 'play') {
        audio.isStopped = false
      } else if (event === 'stop') {
        audio.isStopped = true
      }
      const eventName = `onMusic${event[0].toUpperCase() + event.substr(1)}`
      publish(eventName, {
        dataUrl: audio.src,
        errMsg: `${eventName}:ok`
      })
      publishBackgroundAudioStateChange(event, {
        dataUrl: audio.src
      })
    })
  })
  audio.addEventListener('waiting', () => {
    publishBackgroundAudioStateChange('waiting', {
      dataUrl: audio.src
    })
  })
  audio.addEventListener('error', err => {
    publish('onMusicError', {
      dataUrl: audio.src,
      errMsg: 'Error:' + err.message
    })
    publishBackgroundAudioStateChange('error', {
      dataUrl: audio.src,
      errMsg: err.message,
      errCode: err.code
    })
  })
  audio.addEventListener('prev', () => publish('onBackgroundAudioPrev'))
  audio.addEventListener('next', () => publish('onBackgroundAudioNext'))
}

function setMusicState (args) {
  initMusic()
  const props = ['src', 'startTime', 'coverImgUrl', 'webUrl', 'singer', 'epname', 'title']
  const style = {}
  Object.keys(args).forEach(key => {
    if (props.indexOf(key) >= 0) {
      let val = args[key]
      if (key === props[0] && val) {
        val = getRealPath(val)
      }
      audio[key] = style[key] = val
    }
  })
  audio.setStyles(style)
}

function getAudio () {
  return audio
}

export function getMusicPlayerState () {
  const audio = getAudio()
  if (audio) {
    return {
      dataUrl: audio.src,
      duration: audio.getDuration() || 0,
      currentPosition: audio.getPosition(),
92
      status: audio.isPaused() ? 0 : 1,
fxy060608's avatar
fxy060608 已提交
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 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
      downloadPercent: Math.round(100 * audio.getBuffered() / audio.getDuration()),
      errMsg: `getMusicPlayerState:ok`
    }
  }
  return {
    status: 2,
    errMsg: `getMusicPlayerState:ok`
  }
}
export function operateMusicPlayer ({
  operationType,
  dataUrl,
  position,
  api = 'operateMusicPlayer',
  title,
  coverImgUrl
}) {
  const audio = getAudio()
  var operationTypes = ['resume', 'pause', 'stop']
  if (operationTypes.indexOf(operationType) > 0) {
    audio && audio[operationType]()
  } else if (operationType === 'play') {
    setMusicState({
      src: dataUrl,
      startTime: position,
      title,
      coverImgUrl
    })
    audio.play()
  } else if (operationType === 'seek') {
    audio && audio.seekTo(position)
  }
  return {
    errMsg: `${api}:ok`
  }
}
export function setBackgroundAudioState (args) {
  setMusicState(args)
  return {
    errMsg: `setBackgroundAudioState:ok`
  }
}
export function operateBackgroundAudio ({
  operationType,
  src,
  startTime,
  currentTime
}) {
  return operateMusicPlayer({
    operationType,
    dataUrl: src,
    position: startTime || currentTime || 0,
    api: 'operateBackgroundAudio'
  })
}
export function getBackgroundAudioState () {
  let data = {
    duration: 0,
    currentTime: 0,
    paused: false,
    src: '',
    buffered: 0,
    title: '',
    epname: '',
    singer: '',
    coverImgUrl: '',
    webUrl: '',
    startTime: 0,
    errMsg: `getBackgroundAudioState:ok`
  }
  const audio = getAudio()
  if (audio) {
    let newData = {
      duration: audio.getDuration() || 0,
      currentTime: audio.isStopped ? 0 : audio.getPosition(),
168
      paused: audio.isPaused(),
fxy060608's avatar
fxy060608 已提交
169 170 171 172 173 174 175 176 177 178 179 180 181
      src: audio.src,
      buffered: audio.getBuffered(),
      title: audio.title,
      epname: audio.epname,
      singer: audio.singer,
      coverImgUrl: audio.coverImgUrl,
      webUrl: audio.webUrl,
      startTime: audio.startTime
    }
    data = Object.assign(data, newData)
  }
  return data
}