'a'
mh-two-thousand-and-two
2024-04-12 44d2c92345cd156a59fc327b3060292a282d2893
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
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
import {
  getRealPath
} from '../util'
 
import {
  publish
} from '../../bridge'
 
let audio
 
let timeUpdateTimer = null
const TIME_UPDATE = 250
 
const publishBackgroundAudioStateChange = (state, res = {}) => publish('onBackgroundAudioStateChange', Object.assign({
  state
}, res))
 
const events = ['play', 'pause', 'ended', 'stop', 'canplay']
 
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
        startTimeUpdateTimer()
      } else if (event === 'stop') {
        audio.isStopped = true
      }
 
      if (event === 'pause' || event === 'ended' || event === 'stop') {
        stopTimeUpdateTimer()
      }
 
      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', () => {
    stopTimeUpdateTimer()
    publishBackgroundAudioStateChange('waiting', {
      dataUrl: audio.src
    })
  })
  audio.addEventListener('error', err => {
    stopTimeUpdateTimer()
    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 startTimeUpdateTimer () {
  stopTimeUpdateTimer()
  publishBackgroundAudioStateChange('timeUpdate', {})
  timeUpdateTimer = setInterval(() => {
    publishBackgroundAudioStateChange('timeUpdate', {})
  }, TIME_UPDATE)
}
 
function stopTimeUpdateTimer () {
  if (timeUpdateTimer !== null) {
    clearInterval(timeUpdateTimer)
  }
}
 
function setMusicState (args, name) {
  initMusic()
  const props = ['src', 'startTime', 'coverImgUrl', 'webUrl', 'singer', 'epname', 'title']
 
  if (name && name === 'playbackRate') {
    const val = args[name]
    audio.playbackRate && audio.playbackRate(parseFloat(val))
    return
  }
 
  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(),
      status: audio.isPaused() ? 0 : 1,
      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, name) {
  setMusicState(args, name)
  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) {
    const newData = {
      duration: audio.getDuration() || 0,
      currentTime: audio.isStopped ? 0 : audio.getPosition(),
      paused: audio.isPaused(),
      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
}