-
Notifications
You must be signed in to change notification settings - Fork 4.7k
/
audio.ts
371 lines (329 loc) · 8.86 KB
/
audio.ts
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
import { Audio } from 'expo-av'
import { Permissions } from 'react-native-unimodules'
import { askAsyncPermissions, isUrl } from '../utils'
/**
* InnerAudioContext 实例,可通过 wx.createInnerAudioContext 接口获取实例。
*/
class InnerAudioContext {
private _src: string // TODO asset path
private _startTime: number
private _autoplay: boolean = false
private _loop: boolean = false
private _obeyMuteSwitch: boolean = true // TODO
private _volume: number = 1
/** @member 当前音频的长度(单位 s)。只有在当前有合法的 src 时返回(只读)*/
public duration: number
/** @member 当前音频的播放位置(单位 s)。只有在当前有合法的 src 时返回,时间保留小数点后 6 位(只读 */
public currentTime: number
/** @member 当前是是否暂停或停止状态(只读)*/
public paused: boolean
/** @member 音频缓冲的时间点,仅保证当前播放时间点到此时间点内容已缓冲(只读)*/
public buffered: number //
// private
private soundObject: Audio.Sound
private offCanplayCallback
private offEndedCallback
private offErrorCallback
private offPauseCallback
private offPlayCallback
private offSeekedCallback
private offSeekingCallback
private offStopCallback
private offTimeUpdateCallback
private offWaitingCallback
private onCanplayCallback
private onEndedCallback
private onErrorCallback
private onPauseCallback
private onPlayCallback
private onSeekedCallback
private onSeekingCallback
private onStopCallback
private onTimeUpdateCallback
private onWaitingCallback
constructor () {
this.soundObject = new Audio.Sound()
this.soundObject.setOnPlaybackStatusUpdate(this._onPlaybackStatusUpdate)
}
_onPlaybackStatusUpdate = playbackStatus => {
this.duration = playbackStatus.durationMillis / 1000
this.currentTime = playbackStatus.positionMillis / 1000
this.buffered = playbackStatus.playableDurationMillis / 1000
this.paused = !playbackStatus.isPlaying
// 监听音频播放进度更新事件
this.onTimeUpdateCallback && this.onTimeUpdateCallback(playbackStatus)
if (!playbackStatus.isLoaded) {
// Update your UI for the unloaded state
console.log('isLoaded')
if (playbackStatus.error) {
console.log(`Encountered a fatal error during playback: ${playbackStatus.error}`)
}
} else {
// Update your UI for the loaded state
if (playbackStatus.isPlaying) {
// Update your UI for the playing state
console.log('isPlaying')
} else {
// paused state
console.log('paused')
}
if (playbackStatus.isBuffering) {
this.onWaitingCallback && this.onWaitingCallback()
}
if (playbackStatus.didJustFinish && !playbackStatus.isLooping) {
this.onEndedCallback && this.onEndedCallback()
}
}
}
set src (value) {
this._src = value
if (this._autoplay) {
this._firstPlay()
}
}
set autoplay (value) {
this._autoplay = value
}
set startTime (value) {
this._startTime = value
}
set volume (value) {
this._volume = value
}
set loop (value: boolean) {
this._loop = value
}
set obeyMuteSwitch (value: boolean) {
this._obeyMuteSwitch = value
}
private async _firstPlay () {
if (!this._src) return {errMsg: `src is undefined`}
const source = isUrl(this._src) ? {uri: this._src} : this._src
await this.soundObject.loadAsync(source as any, {}, true)
this.onCanplayCallback && this.onCanplayCallback()
await this.soundObject.playAsync()
if (this._startTime) {
await this.soundObject.playFromPositionAsync(this._startTime * 1000)
}
this.onPlayCallback && this.onPlayCallback()
}
/**
* 播放
*/
async play () {
const status = await askAsyncPermissions(Permissions.AUDIO_RECORDING)
if (status !== 'granted') {
const res = {errMsg: `Permissions denied!`}
return Promise.reject(res)
}
const soundStatus = await this.soundObject.getStatusAsync()
try {
if (soundStatus.isLoaded === false && (soundStatus as any).isPlaying === undefined) {
// First load
await this._firstPlay()
} else {
await this.soundObject.playAsync()
}
// TODO
// await this.soundObject.setIsMutedAsync(this._obeyMuteSwitch)
await this.soundObject.setVolumeAsync(this._volume)
await this.soundObject.setIsLoopingAsync(this._loop)
this.onPlayCallback && this.onPlayCallback()
} catch (error) {
this.onError && this.onError(error)
}
}
/**
* 暂停。暂停后的音频再播放会从暂停处开始播放
*/
async pause () {
try {
await this.soundObject.pauseAsync()
this.onPauseCallback && this.onPauseCallback()
} catch (error) {
this.onError && this.onError(error)
}
}
/**
* 停止。停止后的音频再播放会从头开始播放
*/
async stop () {
try {
await this.soundObject.stopAsync()
this.onStopCallback && this.onStopCallback()
} catch (error) {
this.onError && this.onError(error)
}
}
/**
* 跳转到指定位置
* @param position - 跳转的时间,单位 s。精确到小数点后 3 位,即支持 ms 级别精确度
*/
async seek (position: number) {
const millis = position * 1000
try {
this.onSeekingCallback && this.onSeekingCallback()
await this.soundObject.setPositionAsync(millis)
this.onSeekedCallback && this.onSeekedCallback()
} catch (error) {
this.onError && this.onError(error)
}
}
/**
* @todo
* 销毁当前实例
*/
destroy () {
console.log('destroy')
this.stop()
// this.soundObject = undefined
}
/**
* 监听音频进入可以播放状态的事件。但不保证后面可以流畅播放
* @param callback
*/
onCanplay (callback) {
this.onCanplayCallback = callback
}
/**
* 取消监听音频进入可以播放状态的事件
* @param callback
*/
offCanplay (callback) {
this.offCanplayCallback = callback
}
/**
* 监听音频播放事件
* @param callback
*/
onPlay (callback) {
this.onPlayCallback = callback
}
/**
* 取消监听音频播放事件
* @param callback
*/
offPlay (callback) {
this.offPlayCallback = callback
}
/**
* 监听音频暂停事件
* @param callback
*/
onPause (callback) {
this.onPauseCallback = callback
}
/**
* 取消监听音频暂停事件
* @param callback
*/
offPause (callback) {
this.offPauseCallback = callback
}
/**
* 监听音频停止事件
* @param callback
*/
onStop (callback) {
this.onStopCallback = callback
}
/**
* 取消监听音频停止事件
* @param callback
*/
offStop (callback) {
this.offStopCallback = callback
}
/**
* 监听音频自然播放至结束的事件
* @param callback
*/
onEnded (callback) {
this.onEndedCallback = callback
}
/**
* 取消监听音频自然播放至结束的事件
* @param callback
*/
offEnded (callback) {
this.offEndedCallback = callback
}
/**
* 监听音频播放进度更新事件
* @param callback
*/
onTimeUpdate (callback) {
this.onTimeUpdateCallback = callback
}
/**
* 取消监听音频播放进度更新事件
* @param callback
*/
offTimeUpdate (callback) {
this.offTimeUpdateCallback = callback
}
/**
* 监听音频播放错误事件
* @param callback
*/
onError (callback) {
this.onErrorCallback = callback
}
/**
* 取消监听音频播放错误事件
* @param callback
*/
offError (callback) {
this.offErrorCallback = callback
}
/**
* 监听音频加载中事件。当音频因为数据不足,需要停下来加载时会触发
* @param callback
*/
onWaiting (callback) {
this.onWaitingCallback = callback
}
/**
* 取消监听音频加载中事件
* @param callback
*/
offWaiting (callback) {
this.offWaitingCallback = callback
}
/**
* 监听音频进行跳转操作的事件
* @param callback
*/
onSeeking (callback) {
this.onSeekingCallback = callback
}
/**
* 取消监听音频进行跳转操作的事件
* @param callback
*/
offSeeking (callback) {
this.offSeekingCallback = callback
}
/**
* 监听音频完成跳转操作的事件
* @param callback
*/
onSeeked (callback) {
this.onSeekedCallback = callback
}
/**
* 取消监听音频完成跳转操作的事件
* @param callback
*/
offSeeked (callback) {
this.offSeekedCallback = callback
}
}
/**
* 创建 audio 上下文 AudioContext 对象。
* @param {string} id - audio 组件的 id
* @param {object} t - 在自定义组件下,当前组件实例的this,以操作组件内 audio 组件
*/
export function createInnerAudioContext (id: string, t: object) {
return new InnerAudioContext()
}