UNPKG

35.6 kBTypeScriptView Raw
1import Taro from '../../index'
2
3declare module '../../index' {
4 namespace stopVoice {
5 interface Option {
6 /** 接口调用结束的回调函数(调用成功、失败都会执行) */
7 complete?: (res: TaroGeneral.CallbackResult) => void
8 /** 接口调用失败的回调函数 */
9 fail?: (res: TaroGeneral.CallbackResult) => void
10 /** 接口调用成功的回调函数 */
11 success?: (res: TaroGeneral.CallbackResult) => void
12 }
13 }
14
15 namespace setInnerAudioOption {
16 interface Option {
17 /** 接口调用结束的回调函数(调用成功、失败都会执行) */
18 complete?: (res: TaroGeneral.CallbackResult) => void
19 /** 接口调用失败的回调函数 */
20 fail?: (res: TaroGeneral.CallbackResult) => void
21 /** 是否与其他音频混播,设置为 true 之后,不会终止其他应用或微信内的音乐 */
22 mixWithOther?: boolean
23 /** (仅在 iOS 生效)是否遵循静音开关,设置为 false 之后,即使是在静音模式下,也能播放声音 */
24 obeyMuteSwitch?: boolean
25 /** 接口调用成功的回调函数 */
26 success?: (res: TaroGeneral.CallbackResult) => void
27 }
28 }
29
30 namespace playVoice {
31 interface Option {
32 /** 需要播放的语音文件的文件路径 */
33 filePath: string
34 /** 接口调用结束的回调函数(调用成功、失败都会执行) */
35 complete?: (res: TaroGeneral.CallbackResult) => void
36 /** 指定录音时长,到达指定的录音时长后会自动停止录音,单位:秒 */
37 duration?: number
38 /** 接口调用失败的回调函数 */
39 fail?: (res: TaroGeneral.CallbackResult) => void
40 /** 接口调用成功的回调函数 */
41 success?: (res: TaroGeneral.CallbackResult) => void
42 }
43 }
44
45 namespace pauseVoice {
46 interface Option {
47 /** 接口调用结束的回调函数(调用成功、失败都会执行) */
48 complete?: (res: TaroGeneral.CallbackResult) => void
49 /** 接口调用失败的回调函数 */
50 fail?: (res: TaroGeneral.CallbackResult) => void
51 /** 接口调用成功的回调函数 */
52 success?: (res: TaroGeneral.CallbackResult) => void
53 }
54 }
55
56 namespace getAvailableAudioSources {
57 interface Option {
58 /** 接口调用结束的回调函数(调用成功、失败都会执行) */
59 complete?: (res: TaroGeneral.CallbackResult) => void
60 /** 接口调用失败的回调函数 */
61 fail?: (res: TaroGeneral.CallbackResult) => void
62 /** 接口调用成功的回调函数 */
63 success?: (result: SuccessCallbackResult) => void
64 }
65 interface SuccessCallbackResult extends TaroGeneral.CallbackResult {
66 /** 支持的音频输入源列表,可在 [RecorderManager.start()](/docs/apis/media/recorder/RecorderManager#start)用。返回值定义参考 https://developer.android.com/reference/kotlin/android/media/MediaRecorder.AudioSource */
67 audioSources: Array<keyof audioSources>
68 /** 调用结果 */
69 errMsg: string
70 }
71 /** 支持的音频输入源 */
72 interface audioSources {
73 /** 自动设置,默认使用手机麦克风,插上耳麦后自动切换使用耳机麦克风,所有平台适用 */
74 'auto'
75 /** 手机麦克风,仅限 iOS */
76 'buildInMic'
77 /** 耳机麦克风,仅限 iOS */
78 'headsetMic'
79 /** 麦克风(没插耳麦时是手机麦克风,插耳麦时是耳机麦克风),仅限 Android */
80 'mic'
81 /** 同 mic,适用于录制音视频内容,仅限 Android */
82 'camcorder'
83 /** 同 mic,适用于实时沟通,仅限 Android */
84 'voice_communication'
85 /** 同 mic,适用于语音识别,仅限 Android */
86 'voice_recognition'
87 }
88 }
89
90 /** AudioBuffer 接口表示存在内存里的一段短小的音频资源,利用 [WebAudioContext.decodeAudioData](./WebAudioContext#decodeaudiodata) 方法从一个音频文件构建,或者利用 [AudioContext.createBuffer](https://developers.weixin.qq.com/miniprogram/dev/api/media/audio/(AudioContext.createBuffer).html) 从原始数据构建。把音频放入 AudioBuffer 后,可以传入到一个 AudioBufferSourceNode 进行播放。
91 * @supported weapp
92 * @see https://developers.weixin.qq.com/miniprogram/dev/api/media/audio/AudioBuffer.html
93 */
94 interface AudioBuffer {
95 /** 存储在缓存区的PCM数据的采样率(单位为sample/s) */
96 sampleRate: number
97
98 /** 返回存储在缓存区的PCM数据的采样帧率 */
99 length: number
100
101 /** 返回存储在缓存区的PCM数据的时长(单位为秒) */
102 duration: number
103
104 /** 储存在缓存区的PCM数据的通道数 */
105 numberOfChannels: number
106
107 /** 返回一个 Float32Array,包含了带有频道的PCM数据,由频道参数定义(有0代表第一个频道)
108 * @supported weapp
109 * @example
110 * ```tsx
111 * const audioCtx = Taro.createWebAudioContext()
112 * const myArrayBuffer = audioCtx.createBuffer(2, frameCount, audioCtx.sampleRate);
113 * const nowBuffering = myArrayBuffer.getChannelData(channel);
114 * ```
115 * @see https://developers.weixin.qq.com/miniprogram/dev/api/media/audio/AudioBuffer.getChannelData.html
116 */
117 getChannelData(channel: number): Float32Array
118
119 /** 从 AudioBuffer 的指定频道复制到数组终端。
120 * @supported weapp
121 * @example
122 * ```tsx
123 * const audioCtx = Taro.createWebAudioContext()
124 * const audioBuffer = audioCtx.createFromAudioFile({
125 * filePath:'/pages/res/bgm.mp3', // 静态资源
126 * mixToMono:true,
127 * sampleRate:44100
128 * });
129 * const channels = audioBuffer.numberOfChannels
130 * const anotherArray = new Float32Array(frameCount);
131 * const rate = audioBuffer.sampleRate
132 * const startOffSet = 0
133 * const endOffset = rate * 3;
134 * const newAudioBuffer = audioCtx.createBuffer(channels,endOffset - startOffset,rate)
135 * const offset = 0
136 *
137 * for (let channel = 0; channel < channels; channel++) {
138 * audioBuffer.copyFromChannel(anotherArray, channel, startOffset);
139 * newAudioBuffer.copyToChannel(anotherArray, channel, offset);
140 * }
141 * ```
142 * @see https://developers.weixin.qq.com/miniprogram/dev/api/media/audio/AudioBuffer.copyFromChannel.html
143 */
144 copyFromChannel(): void
145
146 /** 从指定数组复制样本到 audioBuffer 的特定通道
147 * @supported weapp
148 * @see https://developers.weixin.qq.com/miniprogram/dev/api/media/audio/AudioBuffer.copyToChannel.html
149 */
150 copyToChannel(
151 /** 需要复制的源数组 */
152 source: Float32Array,
153 /** 需要复制到的目的通道号 */
154 channelNumber: number,
155 /** 复制偏移数据量 */
156 startInChannel: number
157 ): void
158 }
159
160 /** `AudioContext` 实例,可通过 `Taro.createAudioContext` 获取。
161 *
162 * `AudioContext` 通过 `id` 跟一个 `audio` 组件绑定,操作对应的 audio 组件。
163 * @supported weapp
164 * @see https://developers.weixin.qq.com/miniprogram/dev/api/media/audio/AudioContext.html
165 */
166 interface AudioContext {
167 /** 暂停音频。
168 * @supported weapp
169 * @see https://developers.weixin.qq.com/miniprogram/dev/api/media/audio/AudioContext.pause.html
170 */
171 pause(): void
172 /** 播放音频。
173 * @supported weapp
174 * @see https://developers.weixin.qq.com/miniprogram/dev/api/media/audio/AudioContext.play.html
175 */
176 play(): void
177 /** 跳转到指定位置。
178 * @supported weapp
179 * @see https://developers.weixin.qq.com/miniprogram/dev/api/media/audio/AudioContext.seek.html
180 */
181 seek(
182 /** 跳转位置,单位 s */
183 position: number,
184 ): void
185 /** 设置音频地址
186 * @supported weapp
187 * @see https://developers.weixin.qq.com/miniprogram/dev/api/media/audio/AudioContext.setSrc.html
188 */
189 setSrc(
190 /** 音频地址 */
191 src: string,
192 ): void
193 }
194 /** InnerAudioContext 实例,可通过 [Taro.createInnerAudioContext](./createInnerAudioContext) 接口获取实例。
195 *
196 * **支持格式**
197 *
198 * | 格式 | iOS | Android |
199 * | ---- | ---- | ------- |
200 * | flac | x | √ |
201 * | m4a | √ | √ |
202 * | ogg | x | √ |
203 * | ape | x | √ |
204 * | amr | x | √ |
205 * | wma | x | √ |
206 * | wav | √ | √ |
207 * | mp3 | √ | √ |
208 * | mp4 | x | √ |
209 * | aac | √ | √ |
210 * | aiff | √ | x |
211 * | caf | √ | x |
212 * @example
213 * ```tsx
214 * const innerAudioContext = Taro.createInnerAudioContext()
215 * innerAudioContext.autoplay = true
216 * innerAudioContext.src = 'https://ws.stream.qqmusic.qq.com/M500001VfvsJ21xFqb.mp3?guid=ffffffff82def4af4b12b3cd9337d5e7&uin=346897220&vkey=6292F51E1E384E061FF02C31F716658E5C81F5594D561F2E88B854E81CAAB7806D5E4F103E55D33C16F3FAC506D1AB172DE8600B37E43FAD&fromtag=46'
217 * innerAudioContext.onPlay(() => {
218 * console.log('开始播放')
219 * })
220 * innerAudioContext.onError((res) => {
221 * console.log(res.errMsg)
222 * console.log(res.errCode)
223 * })
224 * ```
225 * @see https://developers.weixin.qq.com/miniprogram/dev/api/media/audio/InnerAudioContext.html
226 */
227 interface InnerAudioContext {
228 /** 音频资源的地址,用于直接播放。 */
229 src: string
230 /** 开始播放的位置(单位:s)
231 * @default 0
232 */
233 startTime: number
234 /** 是否自动开始播放
235 * @default false
236 */
237 autoplay: boolean
238 /** 是否循环播放
239 * @default false
240 */
241 loop: boolean
242 /** 是否遵循系统静音开关。当此参数为 `false` 时,即使用户打开了静音开关,也能继续发出声音。从 2.3.0 版本开始此参数不生效,使用 [Taro.setInnerAudioOption](/docs/apis/media/audio/setInnerAudioOption) 接口统一设置。
243 * @default true
244 */
245 obeyMuteSwitch: boolean
246 /** 音量。范围 0~1。
247 * @default 1
248 */
249 volume: number
250 /** 播放速度。范围 0.5-2.0。
251 * @default 1
252 */
253 playbackRate: number
254 /** 当前音频的长度(单位 s)。只有在当前有合法的 src 时返回
255 * @readonly
256 */
257 duration: number
258 /** 当前音频的播放位置(单位 s)。只有在当前有合法的 src 时返回,时间保留小数点后 6 位
259 * @readonly
260 */
261 currentTime: number
262 /** 当前是是否暂停或停止状态
263 * @readonly
264 */
265 paused: boolean
266 /** 音频缓冲的时间点,仅保证当前播放时间点到此时间点内容已缓冲
267 * @readonly
268 */
269 buffered: number
270 /** origin: 发送完整的 referrer; no-referrer: 不发送 */
271 referrerPolicy?: 'origin' | 'no-referrer' | string
272 /** 播放
273 * @supported weapp, h5, rn
274 */
275 play(): void
276 /** 暂停
277 * @supported weapp, h5, rn
278 */
279 pause(): void
280 /** 停止
281 * @supported weapp, h5, rn
282 */
283 stop(): void
284 /** 跳转到指定位置,单位 s
285 * @supported weapp, h5, rn
286 */
287 seek(position: number): void
288 /** 销毁当前实例
289 * @supported weapp, h5
290 */
291 destroy(): void
292 /** 音频进入可以播放状态,但不保证后面可以流畅播放
293 * @supported weapp, h5, rn
294 */
295 onCanplay(callback?: () => void): void
296 /** 音频播放事件
297 * @supported weapp, h5, rn
298 */
299 onPlay(callback?: () => void): void
300 /** 音频暂停事件
301 * @supported weapp, h5, rn
302 */
303 onPause(callback?: () => void): void
304 /** 音频停止事件
305 * @supported weapp, h5, rn
306 */
307 onStop(callback?: () => void): void
308 /** 音频自然播放结束事件
309 * @supported weapp, h5, rn
310 */
311 onEnded(callback?: () => void): void
312 /** 音频播放进度更新事件
313 * @supported weapp, h5, rn
314 */
315 onTimeUpdate(callback?: () => void): void
316 /** 音频播放错误事件
317 * @supported weapp, h5, rn
318 */
319 onError(callback?: (res: InnerAudioContext.onErrorDetail) => void): void
320 /** 音频加载中事件,当音频因为数据不足,需要停下来加载时会触发
321 * @supported weapp, h5, rn
322 */
323 onWaiting(callback?: () => void): void
324 /** 音频进行 seek 操作事件
325 * @supported weapp, h5, rn
326 */
327 onSeeking(callback?: () => void): void
328 /** 音频完成 seek 操作事件
329 * @supported weapp, h5, rn
330 */
331 onSeeked(callback?: () => void): void
332 /** 取消监听 onCanplay 事件
333 * @supported weapp, h5, rn
334 */
335 offCanplay(callback?: () => void): void
336 /** 取消监听 onPlay 事件
337 * @supported weapp, h5, rn
338 */
339 offPlay(callback?: () => void): void
340 /** 取消监听 onPause 事件
341 * @supported weapp, h5, rn
342 */
343 offPause(callback?: () => void): void
344 /** 取消监听 onStop 事件
345 * @supported weapp, h5, rn
346 */
347 offStop(callback?: () => void): void
348 /** 取消监听 onEnded 事件
349 * @supported weapp, h5, rn
350 */
351 offEnded(callback?: () => void): void
352 /** 取消监听 onTimeUpdate 事件
353 * @supported weapp, h5, rn
354 */
355 offTimeUpdate(callback?: () => void): void
356 /** 取消监听 onError 事件
357 * @supported weapp, h5, rn
358 */
359 offError(callback?: () => void): void
360 /** 取消监听 onWaiting 事件
361 * @supported weapp, h5, rn
362 */
363 offWaiting(callback?: () => void): void
364 /** 取消监听 onSeeking 事件
365 * @supported weapp, h5, rn
366 */
367 offSeeking(callback?: () => void): void
368 /** 取消监听 onSeeked 事件
369 * @supported weapp, h5, rn
370 */
371 offSeeked(callback?: () => void): void
372 }
373
374 namespace InnerAudioContext {
375 interface onErrorDetail extends TaroGeneral.CallbackResult {
376 /** 错误码 */
377 errCode: number
378 /** 错误信息 */
379 errMsg: string
380 }
381
382 interface onErrorDetailErrCode {
383 /** 系统错误 */
384 10001
385 /** 网络错误 */
386 10002
387 /** 文件错误 */
388 10003
389 /** 格式错误 */
390 10004
391 /** 未知错误 */
392 '-1'
393 }
394 }
395
396 /** MediaAudioPlayer 实例,可通过 [Taro.createMediaAudioPlayer](./createMediaAudioPlayer) 接口获取实例。
397 * @supported weapp
398 * @see https://developers.weixin.qq.com/miniprogram/dev/api/media/audio/MediaAudioPlayer.html
399 */
400 interface MediaAudioPlayer {
401 /** 音量。范围 0~1
402 * @default 1
403 */
404 volume: number
405
406 /** 启动播放器
407 * @supported weapp
408 * @see https://developers.weixin.qq.com/miniprogram/dev/api/media/audio/MediaAudioPlayer.start.html
409 */
410 start(): Promise<void>
411
412 /** 添加音频源
413 * @supported weapp
414 * @see https://developers.weixin.qq.com/miniprogram/dev/api/media/audio/MediaAudioPlayer.addAudioSource.html
415 */
416 addAudioSource(
417 /** 视频解码器实例。作为音频源添加到音频播放器中 */
418 source: VideoDecoder
419 ): Promise<void>
420
421 /** 移除音频源
422 * @supported weapp
423 * @see https://developers.weixin.qq.com/miniprogram/dev/api/media/audio/MediaAudioPlayer.removeAudioSource.html
424 */
425 removeAudioSource(
426 /** 视频解码器实例 */
427 source: VideoDecoder
428 ): Promise<void>
429
430 /** 停止播放器
431 * @supported weapp
432 * @see https://developers.weixin.qq.com/miniprogram/dev/api/media/audio/MediaAudioPlayer.stop.html
433 */
434 stop(): Promise<void>
435
436 /** 销毁播放器
437 * @supported weapp
438 * @see https://developers.weixin.qq.com/miniprogram/dev/api/media/audio/MediaAudioPlayer.destroy.html
439 */
440 destroy(): Promise<void>
441 }
442
443 /** WebAudioContext 实例,通过 [Taro.createWebAudioContext](./createWebAudioContext) 接口获取该实例。
444 * @supported weapp
445 * @example
446 * 监听状态
447 *
448 * ```tsx
449 * const audioCtx = Taro.createWebAudioContext()
450 * audioCtx.onstatechange = () => {
451 * console.log(ctx.state)
452 * }
453 * setTimeout(audioCtx.suspend, 1000)
454 * setTimeout(audioCtx.resume, 2000)
455 * ```
456 * @see https://developers.weixin.qq.com/miniprogram/dev/api/media/audio/WebAudioContext.html
457 */
458 interface WebAudioContext {
459 /** 当前 WebAudio 上下文的状态。
460 *
461 * 可能的值如下:suspended(暂停)、running(正在运行)、closed(已关闭)。
462 * 需要注意的是,不要在 audioContext close 后再访问 state 属性
463 */
464 state: string
465
466 /** 可写属性,开发者可以对该属性设置一个监听函数,当 WebAudio 状态改变的时候,会触发开发者设置的监听函数。 */
467 onstatechange: () => void
468
469 /** 获取当前上下文的时间戳。 */
470 currentTime: number
471
472 /** 当前上下文的最终目标节点,一般是音频渲染设备。 */
473 destination: WebAudioContextNode
474
475 /** 空间音频监听器。 */
476 listener: AudioListener
477
478 /** 采样率,通常在 8000-96000 之间,通常 44100hz 的采样率最为常见。 */
479 sampleRate: number
480
481 /** 关闭WebAudioContext
482 *
483 * **注意事项**
484 * 同步关闭对应的 WebAudio 上下文。close 后会立即释放当前上下文的资源,**不要在 close 后再次访问 state 属性**。
485 * @supported weapp
486 * @example
487 * ```tsx
488 * const audioCtx = Taro.createWebAudioContext()
489 * audioCtx.close().then(() => {
490 * console.log(audioCtx.state) // bad case:不应该在close后再访问state
491 * })
492 * ```
493 * @see https://developers.weixin.qq.com/miniprogram/dev/api/media/audio/WebAudioContext.close.html
494 */
495 close(): Promise<void>
496
497 /** 同步恢复已经被暂停的 WebAudioContext 上下文
498 * @supported weapp
499 * @see https://developers.weixin.qq.com/miniprogram/dev/api/media/audio/WebAudioContext.resume.html
500 */
501 resume(): Promise<void>
502
503 /** 同步暂停 WebAudioContext 上下文
504 * @supported weapp
505 * @see https://developers.weixin.qq.com/miniprogram/dev/api/media/audio/WebAudioContext.suspend.html
506 */
507 suspend(): Promise<void>
508
509 /** 创建一个 IIRFilterNode
510 * @supported weapp
511 * @example
512 * ```tsx
513 * let lowPassCoefs = [
514 * {
515 * frequency: 200,
516 * feedforward: [0.00020298, 0.0004059599, 0.00020298],
517 * feedback: [1.0126964558, -1.9991880801, 0.9873035442]
518 * },
519 * {
520 * frequency: 500,
521 * feedforward: [0.0012681742, 0.0025363483, 0.0012681742],
522 * feedback: [1.0317185917, -1.9949273033, 0.9682814083]
523 * },
524 * {
525 * frequency: 1000,
526 * feedforward: [0.0050662636, 0.0101325272, 0.0050662636],
527 * feedback: [1.0632762845, -1.9797349456, 0.9367237155]
528 * },
529 * {
530 * frequency: 5000,
531 * feedforward: [0.1215955842, 0.2431911684, 0.1215955842],
532 * feedback: [1.2912769759, -1.5136176632, 0.7087230241]
533 * }
534 * ]
535 *
536 * const feedForward = lowPassCoefs[filterNumber].feedforward
537 * const feedBack = lowPassCoefs[filterNumber].feedback
538 * const iirFilter = audioContext.createIIRFilter(feedForward, feedBack)
539 * ```
540 * @see https://developers.weixin.qq.com/miniprogram/dev/api/media/audio/WebAudioContext.createIIRFilter.html
541 */
542 createIIRFilter(
543 /** 一个浮点值数组,指定IIR滤波器传递函数的前馈(分子)系数。 */
544 feedforward: number[],
545 /** 一个浮点值数组,指定IIR滤波器传递函数的反馈(分母)系数。 */
546 feedback: number[]
547 ): IIRFilterNode
548
549 /** 创建一个 WaveShaperNode
550 * @supported weapp
551 * @see https://developers.weixin.qq.com/miniprogram/dev/api/media/audio/WebAudioContext.createWaveShaper.html
552 */
553 createWaveShaper(): WaveShaperNode
554
555 /** 创建一个 ConstantSourceNode
556 * @supported weapp
557 * @see https://developers.weixin.qq.com/miniprogram/dev/api/media/audio/WebAudioContext.createConstantSource.html
558 */
559 createConstantSource(): ConstantSourceNode
560
561 /** 创建一个 OscillatorNode
562 * @supported weapp
563 * @see https://developers.weixin.qq.com/miniprogram/dev/api/media/audio/WebAudioContext.createOscillator.html
564 */
565 createOscillator(): OscillatorNode
566
567 /** 创建一个 GainNode
568 * @supported weapp
569 * @see https://developers.weixin.qq.com/miniprogram/dev/api/media/audio/WebAudioContext.createGain.html
570 */
571 createGain(): GainNode
572
573 /** 创建一个 PeriodicWaveNode
574 *
575 * **注意**
576 * `real` 和 `imag` 数组必须拥有一样的长度,否则抛出错误
577 *
578 * ```tsx
579 * const real = new Float32Array(2)
580 * const imag = new Float32Array(2)
581 * real[0] = 0
582 * imag[0] = 0
583 * real[1] = 1
584 * imag[1] = 0
585 *
586 * const waveNode = audioContext.createPeriodicWave(real, imag, {disableNormalization: true})
587 * ```
588 * @supported weapp
589 * @see https://developers.weixin.qq.com/miniprogram/dev/api/media/audio/WebAudioContext.createPeriodicWave.html
590 */
591 createPeriodicWave(
592 /** 一组余弦项(传统上是A项) */
593 real: Float32Array,
594 /** 一组余弦项(传统上是A项) */
595 imag: Float32Array,
596 /** 一个字典对象,它指定是否应该禁用规范化(默认启用规范化) */
597 constraints: WebAudioContext.createPeriodicWave.Constraints
598 ): PeriodicWave
599
600 /** 创建一个BiquadFilterNode
601 * @supported weapp
602 * @see https://developers.weixin.qq.com/miniprogram/dev/api/media/audio/WebAudioContext.createBiquadFilter.html
603 */
604 createBiquadFilter(): BiquadFilterNode
605
606 /** 创建一个 BufferSourceNode 实例,通过 AudioBuffer 对象来播放音频数据。
607 * @supported weapp
608 * @see https://developers.weixin.qq.com/miniprogram/dev/api/media/audio/WebAudioContext.createBufferSource.html
609 */
610 createBufferSource(): AudioBufferSourceNode
611
612 /** 创建一个ChannelMergerNode
613 * @supported weapp
614 * @see https://developers.weixin.qq.com/miniprogram/dev/api/media/audio/WebAudioContext.createChannelMerger.html
615 */
616 createChannelMerger(
617 /** 输出流中需要保持的输入流的个数 */
618 numberOfInputs: number
619 ): ChannelMergerNode
620
621 /** 创建一个ChannelSplitterNode
622 * @supported weapp
623 * @see https://developers.weixin.qq.com/miniprogram/dev/api/media/audio/WebAudioContext.createChannelSplitter.html
624 */
625 createChannelSplitter(
626 /** 要分别输出的输入音频流中的通道数 */
627 numberOfOutputs: number
628 ): ChannelSplitterNode
629
630 /** 创建一个DelayNode
631 * @supported weapp
632 * @example
633 * ```tsx
634 * let audioCtx = Taro.createWebAudioContext()
635 * const delayNode = audioCtx.createDelay(5)
636 * ```
637 * @see https://developers.weixin.qq.com/miniprogram/dev/api/media/audio/WebAudioContext.createDelay.html
638 */
639 createDelay(
640 /** 最大延迟时间 */
641 maxDelayTime: number
642 ): DelayNode
643
644 /** 创建一个DynamicsCompressorNode
645 * @supported weapp
646 * @example
647 * ```tsx
648 * let audioCtx = Taro.createWebAudioContext()
649 * let compressor = audioCtx.createDynamicsCompressor()
650 *
651 * compressor.threshold.value = -50
652 * compressor.knee.value = 40
653 * compressor.ratio.value = 12
654 * compressor.attack.value = 0
655 * compressor.release.value = 0.25
656 * ```
657 * @see https://developers.weixin.qq.com/miniprogram/dev/api/media/audio/WebAudioContext.createDynamicsCompressor.html
658 */
659 createDynamicsCompressor(): DynamicsCompressorNode
660
661 /** 创建一个ScriptProcessorNode
662 * @supported weapp
663 * @example
664 * ```tsx
665 * let audioCtx = Taro.createWebAudioContext()
666 * const sampleSize = 4096
667 * audioContext.createScriptProcessor(sampleSize, 1, 1)
668 * ```
669 * @see https://developers.weixin.qq.com/miniprogram/dev/api/media/audio/WebAudioContext.createScriptProcessor.html
670 */
671 createScriptProcessor(
672 /** 缓冲区大小,以样本帧为单位 */
673 bufferSize: number,
674 /** 用于指定输入 node 的声道的数量 */
675 numberOfInputChannels: number,
676 /** 用于指定输出 node 的声道的数量 */
677 numberOfOutputChannels: number
678 ): ScriptProcessorNode
679
680 /** 创建一个PannerNode
681 * @supported weapp
682 * @see https://developers.weixin.qq.com/miniprogram/dev/api/media/audio/WebAudioContext.createPanner.html
683 */
684 createPanner(): PannerNode
685
686 /** 创建一个AudioBuffer,代表着一段驻留在内存中的短音频
687 * @supported weapp
688 * @example
689 * ```tsx
690 * const audioCtx = Taro.createWebAudioContext()
691 * const channels = 2, frameCount = audioCtx.sampleRate * 2.0
692 * const myArrayBuffer = audioCtx.createBuffer(channels, frameCount, audioCtx.sampleRate)
693 * ```
694 * @see https://developers.weixin.qq.com/miniprogram/dev/api/media/audio/WebAudioContext.createBuffer.html
695 */
696 createBuffer(
697 /** 定义了 buffer 中包含的声频通道数量的整数 */
698 numOfChannels: number,
699 /** 代表 buffer 中的样本帧数的整数 */
700 length: number,
701 /** 线性音频样本的采样率,即每一秒包含的关键帧的个数 */
702 sampleRate: number
703 ): AudioBuffer
704
705 /** 异步解码一段资源为AudioBuffer。
706 * @supported weapp
707 * @example
708 * ```tsx
709 * Taro.request({
710 * url: url, // 音频 url
711 * responseType: 'arraybuffer',
712 * success: res => {
713 * audioCtx.decodeAudioData(res.data, buffer => {
714 * console.log(buffer)
715 * }, err => {
716 * console.error('decodeAudioData fail', err)
717 * })
718 * }
719 * })
720 * ```
721 * @see https://developers.weixin.qq.com/miniprogram/dev/api/media/audio/WebAudioContext.decodeAudioData.html
722 */
723 decodeAudioData(): AudioBuffer
724 }
725
726 namespace WebAudioContext {
727 namespace createPeriodicWave {
728 /** 字典对象 */
729 interface Constraints {
730 /** 如果指定为 true 则禁用标准化
731 * @default false
732 */
733 disableNormalization?: boolean
734 }
735 }
736 }
737
738 /** 一类音频处理模块,不同的Node具备不同的功能,如GainNode(音量调整)等。一个 WebAudioContextNode 可以通过上下文来创建。
739 *
740 * > 目前已经支持以下Node: IIRFilterNode WaveShaperNode ConstantSourceNode ChannelMergerNode OscillatorNode GainNode BiquadFilterNode PeriodicWaveNode BufferSourceNode ChannelSplitterNode ChannelMergerNode DelayNode DynamicsCompressorNode ScriptProcessorNode PannerNode
741 * @supported weapp
742 * @see https://developers.weixin.qq.com/miniprogram/dev/api/media/audio/WebAudioContextNode.html
743 */
744 interface WebAudioContextNode {
745 /** 右手笛卡尔坐标系中X轴的位置。 */
746 positionX: number
747
748 /** 右手笛卡尔坐标系中Y轴的位置。 */
749 positionY: number
750
751 /** 右手笛卡尔坐标系中Z轴的位置。 */
752 positionZ: number
753
754 /** 表示监听器的前向系统在同一笛卡尔坐标系中的水平位置,作为位置(位置x,位置和位置和位置)值。 */
755 forwardX: number
756
757 /** 表示听众的前向方向在同一笛卡尔坐标系中作为位置(位置x,位置和位置和位置)值的垂直位置。 */
758 forwardY: number
759
760 /** 表示与position (positionX、positionY和positionZ)值在同一笛卡尔坐标系下的听者前进方向的纵向(前后)位置。 */
761 forwardZ: number
762
763 /** 表示在与position (positionX、positionY和positionZ)值相同的笛卡尔坐标系中侦听器向前方向的水平位置。 */
764 upX: number
765
766 /** 表示在与position (positionX、positionY和positionZ)值相同的笛卡尔坐标系中侦听器向上方向的水平位置。 */
767 upY: number
768
769 /** 表示在与position (positionX、positionY和positionZ)值相同的笛卡尔坐标系中侦听器向后方向的水平位置。 */
770 upZ: number
771
772 /** 设置监听器的方向
773 * @supported weapp
774 */
775 setOrientation(...args: any[]): void
776
777 /** 设置监听器的位置
778 * @supported weapp
779 */
780 setPosition(...args: any[]): void
781 }
782
783 interface TaroStatic {
784 /** 结束播放语音。
785 * **注意:1.6.0 版本开始,本接口不再维护。建议使用能力更强的 [Taro.createInnerAudioContext](./createInnerAudioContext) 接口**
786 * @supported weapp
787 * @example
788 * ```tsx
789 * Taro.startRecord({
790 * success: function (res) {
791 * const filePath = res.tempFilePath
792 * Taro.playVoice({ filePath })
793 *
794 * setTimeout(Taro.stopVoice, 5000)
795 * }
796 * })
797 * ```
798 * @example
799 * ```tsx
800 * Taro.startRecord(params).then(res => {
801 * const filePath = res.tempFilePath
802 * Taro.playVoice({ filePath })
803 *
804 * setTimeout(Taro.stopVoice, 5000)
805 * })
806 * ```
807 * @see https://developers.weixin.qq.com/miniprogram/dev/api/media/audio/wx.stopVoice.html
808 */
809 stopVoice(option?: stopVoice.Option): void
810
811 /** 设置 [InnerAudioContext](/docs/apis/media/audio/InnerAudioContext)项。设置之后对当前小程序全局生效。
812 * @supported weapp
813 * @see https://developers.weixin.qq.com/miniprogram/dev/api/media/audio/wx.setInnerAudioOption.html
814 */
815 setInnerAudioOption(option: setInnerAudioOption.Option): Promise<TaroGeneral.CallbackResult>
816
817 /** 开始播放语音。同时只允许一个语音文件正在播放,如果前一个语音文件还没播放完,将中断前一个语音播放。
818 * @supported weapp
819 * @example
820 * ```tsx
821 * Taro.startRecord({
822 * success: function (res) {
823 * const tempFilePath = res.tempFilePath
824 * Taro.playVoice({
825 * filePath: tempFilePath,
826 * complete: function () { }
827 * })
828 * }
829 * })
830 * ```
831 * @see https://developers.weixin.qq.com/miniprogram/dev/api/media/audio/wx.playVoice.html
832 */
833 playVoice(option: playVoice.Option): Promise<TaroGeneral.CallbackResult>
834
835 /** 暂停正在播放的语音。再次调用 [Taro.playVoice](/docs/apis/media/audio/stopVoice)。
836 * **注意:1.6.0 版本开始,本接口不再维护。建议使用能力更强的 [Taro.createInnerAudioContext](./createInnerAudioContext) 接口**
837 * @supported weapp
838 * @example
839 * ```tsx
840 * Taro.startRecord({
841 * success: function (res) {
842 * var tempFilePath = res.tempFilePath
843 * Taro.playVoice({
844 * filePath: tempFilePath
845 * })
846 * setTimeout(function() {
847 * //暂停播放
848 * Taro.pauseVoice()
849 * }, 5000)
850 * }
851 * })
852 * ```
853 * @see https://developers.weixin.qq.com/miniprogram/dev/api/media/audio/wx.pauseVoice.html
854 */
855 pauseVoice(option?: pauseVoice.Option): void
856
857 /** 获取当前支持的音频输入源
858 * @supported weapp
859 * @see https://developers.weixin.qq.com/miniprogram/dev/api/media/audio/wx.getAvailableAudioSources.html
860 */
861 getAvailableAudioSources(option?: getAvailableAudioSources.Option): Promise<getAvailableAudioSources.SuccessCallbackResult>
862
863 /** 创建 WebAudio 上下文。
864 * @supported weapp
865 * @example
866 * 一个简单的播放demo
867 *
868 * ```tsx
869 * const audioCtx = Taro.createWebAudioContext()
870 *
871 * const loadAudio = (url) => {
872 * return new Promise((resolve) => {
873 * Taro.request({
874 * url,
875 * responseType: 'arraybuffer',
876 * success: res => {
877 * console.log('res.data', res.data)
878 * audioCtx.decodeAudioData(res.data, buffer => {
879 * resolve(buffer)
880 * }, err => {
881 * console.error('decodeAudioData fail', err)
882 * reject()
883 * })
884 * },
885 * fail: res => {
886 * console.error('request fail', res)
887 * reject()
888 * }
889 * })
890 * })
891 * }
892 *
893 * const play = () => {
894 * loadAudio('xxx-test.mp3').then(buffer => {
895 * const source = audioCtx.createBufferSource()
896 * source.buffer = buffer
897 * source.connect(audioCtx.destination)
898 * source.start()
899 * }).catch(() => {
900 * console.log('fail')
901 * })
902 * }
903 *
904 * play()
905 * ```
906 * @see https://developers.weixin.qq.com/miniprogram/dev/api/media/audio/wx.createWebAudioContext.html
907 */
908 createWebAudioContext(): WebAudioContext
909
910 /** 创建媒体音频播放器对象 [MediaAudioPlayer](./MediaAudioPlayer) 对象,可用于播放视频解码器 [VideoDecoder](/docs/apis/media/video-decoder/VideoDecoder) 输出的音频
911 *
912 * **注意事项**
913 * - iOS 7.0.15 mediaAudioPlayer 播放网络视频资源会出现音频卡顿,本地视频没有这个问题,将下一个客户端版本修复。
914 * @supported weapp
915 * @example
916 * ```tsx
917 * // 创建视频解码器,具体参数见 createVideoDecoder 文档
918 * const videoDecoder = Taro.createVideoDecoder()
919 * // 创建媒体音频播放器
920 * const mediaAudioPlayer = Taro.createMediaAudioPlayer()
921 * // 启动视频解码器
922 * videoDecoder.start()
923 * // 启动播放器
924 * mediaAudioPlayer.start().then(() => {
925 * // 添加播放器音频来源
926 * mediaAudioPlayer.addAudioSource(videoDecoder).then(res => {
927 * videoDecoder.getFrameData() // 建议在 requestAnimationFrame 里获取每一帧视频数据
928 * console.log(res)
929 * })
930 *
931 * // 移除播放器音频来源
932 * mediaAudioPlayer.removeAudioSource(videoDecoder).then()
933 * // 停止播放器
934 * mediaAudioPlayer.stop().then()
935 * // 销毁播放器
936 * mediaAudioPlayer.destroy().then()
937 * // 设置播放器音量
938 * mediaAudioPlayer.volume = 0.5
939 * })
940 *```
941 * @see https://developers.weixin.qq.com/miniprogram/dev/api/media/audio/wx.createMediaAudioPlayer.html
942 */
943 createMediaAudioPlayer(): MediaAudioPlayer
944
945 /** 创建内部 audio 上下文 InnerAudioContext 对象。
946 * @supported weapp, h5, rn
947 * @example
948 * ```tsx
949 * const innerAudioContext = Taro.createInnerAudioContext()
950 * innerAudioContext.autoplay = true
951 * innerAudioContext.src = 'https://storage.360buyimg.com/jdrd-blog/27.mp3'
952 * innerAudioContext.onPlay(() => {
953 * console.log('开始播放')
954 * })
955 * innerAudioContext.onError((res) => {
956 * console.log(res.errMsg)
957 * console.log(res.errCode)
958 * })
959 * ```
960 * @see https://developers.weixin.qq.com/miniprogram/dev/api/media/audio/wx.createInnerAudioContext.html
961 */
962 createInnerAudioContext(): InnerAudioContext
963
964 /** 创建 audio 上下文 AudioContext 对象。
965 * **注意:1.6.0 版本开始,本接口不再维护。建议使用能力更强的 [Taro.createInnerAudioContext](./createInnerAudioContext) 接口**
966 * @supported weapp
967 * @example
968 * ```tsx
969 * const audioCtx = Taro.createAudioContext('myAudio')
970 * ```
971 * @see https://developers.weixin.qq.com/miniprogram/dev/api/media/audio/wx.createAudioContext.html
972 */
973 createAudioContext(
974 /** [audio](/docs/components/media/audio) 组件的 id */
975 id: string,
976 /** 在自定义组件下,当前组件实例的this,以操作组件内 [audio](/docs/components/media/audio) 组件 */
977 component?: TaroGeneral.IAnyObject,
978 ): AudioContext
979 }
980}