inneraudiocontext 概览
基础库 或更高版本; 且 支付宝客户端 10.1.87 或更高版本; 若版本较低,建议采取 兼容处理 。
小程序开发者工具: 不支持。请以真机的调试结果为准。
主体: 企业支付宝小程序 、 个人支付宝小程序
全局唯一的前景(内部)音频管理器。
可通过 my.createinneraudiocontext 获取。
属性
名称 | 类型 | 只读 | 功能说明 |
---|---|---|---|
duration | number | 是 | 当前音频的长度,单位为秒(s)。 |
paused | boolean | 是 | 当前是否是暂停或停止状态,true 表示暂停或停止,false 表示正在播放。 |
currenttime | number | 是 | 当前音频的播放位置,单位为秒(s),该值为浮点数。 |
buffered | number | 是 | 音频缓冲的时间点,仅保证当前播放时间点到此时间点内容已缓冲。 |
src | string | 否 | 音频的数据源,默认为空字符串,当设置了新的 src 时,会自动开始播放。
|
starttime | number | 否 | 音频开始播放的位置,默认从 0 开始播放,单位为秒(s)。 |
playbackrate | number | 否 | 播放速度。范围 0.5 - 2.0,默认为 1。 |
autoplay | boolean | 否 | 是否自动开始播放。默认为 false。 |
loop | boolean | 否 | 是否循环播放,默认为 false。 |
obeymuteswitch | boolean | 否 | 是否遵循系统静音开关,当此参数为 false 时,即使用户打开了静音开关,也能继续发出声音,默认值为 true。此参数仅 ios 支持。 |
volume | number | 否 | 音量。范围 0~1。例如: |
方法
名称 | 类型 | 功能说明 |
---|---|---|
inneraudiocontext.play | function | 播放前景音频。 |
inneraudiocontext.pause | function | 暂停播放前景音频。 |
inneraudiocontext.stop | function | 停止播放前景音频。 |
inneraudiocontext.seek | function | 跳转到指定位置,单位为秒(s)。精确到小数点后 3 位,即支持毫秒(ms)级别精确度。 |
inneraudiocontext.destroy | function | 销毁当前实例。 |
inneraudiocontext.onwaiting | function | 监听前景音频进入等待状态(开始缓冲)事件。 |
inneraudiocontext.offwaiting | function | 取消监听音频进入等待状态(开始缓冲)事件。 |
inneraudiocontext.oncanplay | function | 监听前景音频进入可以播放状态事件。但不保证后面可以流畅播放。 |
inneraudiocontext.offcanplay | function | 取消监听前景音频进入可以播放状态。 |
inneraudiocontext.onplay | function | 监听前景音频播放事件。 |
inneraudiocontext.offplay | function | 取消监听前景音频播放事件。 |
inneraudiocontext.onpause | function | 监听前景音频暂停事件。 |
inneraudiocontext.offpause | function | 取消监听前景音频暂停事件。 |
inneraudiocontext.onstop | function | 监听前景音频停止事件。 |
inneraudiocontext.offstop | function | 取消监听前景音频停止事件。 |
inneraudiocontext.onseeking | function | 监听前景音频开始播放进度跳转事件。 |
inneraudiocontext.offseeking | function | 取消监听前景音频开始播放进度跳转事件。 |
inneraudiocontext.onseeked | function | 监听前景音频完成播放进度跳转操作。 |
inneraudiocontext.offseeked | function | 取消监听前景音频完成播放进度跳转操作。 |
inneraudiocontext.onended | function | 监听前景音频自然播放结束事件。 |
inneraudiocontext.offended | function | 取消监听前景音频自然播放结束事件。 |
inneraudiocontext.onerror | function | 监听前景音频播放错误事件。 |
inneraudiocontext.offerror | function | 取消监听前景音频播放错误事件。 |
inneraudiocontext.ontimeupdate | function | 监听前景音频播放进度更新事件。 |
inneraudiocontext.offtimeupdate | function | 取消监听前景音频播放进度更新事件。 |
<view>
<view>
<button
type="primary"
ontap="playinneraudio"
>
开始播放前景音频
button>
view>
<view>
<button
type="primary"
ontap="pauseinneraudio"
>
暂停播放前景音频
button>
view>
<view>
<button
type="primary"
ontap="stopinneraudio"
>
停止播放前景音频
button>
view>
<view>
<button
type="primary"
ontap="seekinneraudio"
>
前景音频播放进度跳转
button>
view>
view>
page ({
onload () {
//创建前景音频上下文对象。
this.inneraudiocontext = my.createinneraudiocontext ();
//来源于优酷的音频码,用于直接播放。支持音频格式:aac,mp3。如果开发者不传入音频码,控制台不会报错,但无音频播放。
this.inneraudiocontext.src = 'xndy2nte2mje4na==';
//是否自动开始播放,默认为 false。
this.inneraudiocontext.autoplay = false;
//是否循环播放,默认为 false。
this.inneraudiocontext.loop = false;
//是否遵循系统静音开关,当此参数为 false 时,即使用户打开了静音开关,也能继续发出声音,默认值为 true (注意:此参数仅 ios 支持)。
this.inneraudiocontext.obeymuteswitch = false;
this.inneraudiocontext.onplay (() => {
console.log ('inneraudiocontext onplay 开始播放');
my.alert ({content: 'inneraudiocontext 前景音频播放事件 onplay'});
});
this.inneraudiocontext.onpause (() => {
console.log ('inneraudiocontext onpause 暂停播放');
my.alert ({content: 'inneraudiocontext 前景音频暂停事件 onpause'});
});
this.inneraudiocontext.onstop (() => {
console.log ('inneraudiocontext onstop 停止播放');
my.alert ({content: 'inneraudiocontext 前景音频停止事件 onstop'});
});
this.inneraudiocontext.onseeking (() => {
console.log ('inneraudiocontext onseeking 跳转中播放事件');
my.alert ({
content: 'inneraudiocontext 前景音频跳转中播放事件 onseeking',
});
});
this.inneraudiocontext.onseeked (() => {
console.log ('inneraudiocontext onseeked 完成跳转播放事件');
my.alert ({
content: 'inneraudiocontext 前景音频完成跳转播放事件 onseeked',
});
});
this.inneraudiocontext.onerror (err => {
console.log ('inneraudiocontext onerror 前景音频播放错误事件', err);
my.alert ({content: 'inneraudiocontext 前景音频播放错误事件 onerror'});
});
},
playinneraudio () {
console.log ('inneraudiocontext.play 播放前景景音频');
this.inneraudiocontext.play ();
},
stopinneraudio () {
console.log ('inneraudiocontext.stop 停止播放前景音频');
this.inneraudiocontext.stop ();
},
pauseinneraudio () {
console.log ('inneraudiocontext.pause 暂停播放前景音频');
this.inneraudiocontext.pause ();
},
seekinneraudio () {
console.log ('inneraudiocontext.seek 跳转到指定位置播放音频');
// 举例跳到 20 s
this.inneraudiocontext.seek (20);
},
});
page ({
onload () {
// 页面加载时,注册了 onplay、onstop、ontimeupdate、onerror 监听事件
this.inneraudiocontext = my.createinneraudiocontext ();
this.inneraudiocontext.onplay (() => {
console.log ('inneraudiocontext onplay 开始播放');
});
this.inneraudiocontext.onstop (() => {
console.log ('inneraudiocontext onstop 停止播放');
});
this.inneraudiocontext.ontimeupdate (() => {
console.log ('inneraudiocontext ontimeupdate 前景音频播放进度更新事件');
});
this.inneraudiocontext.onerror (err => {
console.log ('inneraudiocontext onerror 前景音频播放错误事件', err);
});
},
onunload () {
// 页面卸载时,卸载已监听的音频事件
this.inneraudiocontext.offplay ();
this.inneraudiocontext.offstop ();
this.inneraudiocontext.onerror ();
this.inneraudiocontext.offtimeupdate ();
},
});
-
tip支付宝提供了简单的快速示例,可以点此下载 。
-
bug支付宝客户端版本 10.5.50 之前,退出创建 inneraudiocontext 的页面时,如果存在任一音频事件监听没有取消,在页面退出这些事件监听函数后不会再被触发,并且后续在新的页面创建的 inneraudiocontext 实例上所有事件监听也都会无效。规避办法:在页面卸载时将已注册的音频监听事件全部都 off 掉,参考 示例代码。