# createInnerAudioContext

创建innerAudioContext实例,通过它能够操作音频播放。

注意

如果是本地资源,在创建innerAudioContext实例时就能拿到duration, 如果是网络资源,在添加onPlay、onCanplay、onTimeUpdate回调后,能拿到duration

# 支持说明

应用能力 Android iOS PC 预览效果
小程序 3.9.0 3.8.0 X
扫码预览
用BoosHi扫码或PC端点击
网页应用 X X X /

# 输入

名称 数据类型 必填 默认值 描述
autoplay boolean true 是否自动播放音频
src string 要播放音频的资源地址,支持网络url路径和本地路径播放。支持的格式有:MP3、WAV、⁣M4A
startTime number 0 开始播放时间,单位秒
loop boolean false 是否循环播放
obeyMuteSwitch boolean true 是否遵循系统静音开关,传false及时打开系统静音也能继续发出声音
该参数只在iOS上生效
volume number 1 播放音量,0-1

# 输出

返回值:innerAudioContext,该对象的方法 & 属性列表参见下表:

方法 介绍
play 播放
pause 暂停播放
stop 停止播放
seek 跳转到指定位置播放,数据格式为number,单位为s
destroy 销毁当前 innerAudioContext 实例
onCanplay 监听音频 Canplay 状态,但不保证后面可以流畅播放
offCanplay 取消监听 Canplay 事件
onPlay 监听音频 Play 事件
offPlay 取消监听 Play 事件
onPause 监听音频 Pause 事件
offPause 取消监听 Pause 事件
onStop 监听音频 Stop 事件
offStop 取消监听 Stop 事件
onEnded 监听音频 Ended 事件
offEnded 取消监听 Ended 事件
onTimeUpdate 监听音频 TimeUpdate 事件
offTimeUpdate 取消监听 TimeUpdate 事件
onError 监听音频 Error 事件
offError 取消监听 Error 事件
onWaiting 监听音频 Waiting 事件,当音频因为数据不足,需要停下来加载时会触发
offWaiting 取消监听 Waiting 事件
onSeeking 音频进行 seek 操作事件
offSeeking 取消监听 Seeking 事件
onSeeked 音频完成 seek 操作事件
offSeeked 取消监听 Seeked 事件
属性 介绍
autoplay 是否自动播放
src 播放音频的资源地址
startTime 初始化播放开始时间,单位秒
loop 是否循环播放
obeyMuteSwitch 是否遵循系统静音开关,当此参数为 false 时,即使用户打开了静音开关,也能继续发出声音
volume 播放音量
duration 当前音频长度,单位秒
currentTime 当前播放时长,单位秒
buffered 已缓冲时长,单位秒
destroyed 当前实例是否被销毁

# 示例代码

const innerAudioContext = bz.createInnerAudioContext({
    autoplay: true,
    src: 'https://histatic.zhipin.com/front/files/cLuL6tHMpJkHdqmtxSnqVt.mp3',
    startTime: 0,
    loop: false,
    obeyMuteSwitch: true,
    volume: 0.5
})
innerAudioContext.onPlay(() => {
    console.log('开始播放');
});
innerAudioContext.onError((error) => {
    console.log(error)
});
innerAudioContext.onTimeUpdate((res) => {
    this.setData({
        progress: innerAudioContext.currentTime / innerAudioContext.duration
    });
})
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
最后更新于 : 3/21/2023, 4:06:55 PM