has.createInnerAudioContext():InnerAudioContext
创建内部audio上下文InnerAudioContext对象。
起始版本: 1.0.14
返回值:
返回InnerAudioContext对象。
示例:
const ctx = has.createInnerAudioContext();
InnerAudioContext实例。可通过has.createInnerAudioContext接口获取实例。注意,音频播放过程中,可能被系统中断,可通过has.onAudioInterruptionBegin、has.onAudioInterruptionEnd事件来处理这种情况。
起始版本: 1.0.14
属性:
| 属性名 | 类型 | 默认值 | 必填 | 只读 | 描述 |
|---|---|---|---|---|---|
| src | string | - | 是 | 否 | 音频资源的地址,用于直接播放。 |
| startTime | number | 0 | 否 | 否 | 开始播放的位置,单位:s。 |
| autoplay | boolean | false | 否 | 否 | 是否自动开始播放。(只写) |
| loop | boolean | false | 否 | 否 | 是否循环播放。 |
| volume | number | 1 | 否 | 否 | 音量。范围: 0~1。 |
| playbackRate | number | 1 | 否 | 否 | 播放速度。支持的播放速度有:0.125、0.25、0.5、0.75、1、1.25、1.5、1.75、2。 |
| duration | number | - | 否 | 是 | 当前音频的总时长,单位:s。只有在当前有合法的src时返回。 |
| currentTime | number | - | 否 | 否 | 当前音频的播放位置,单位:s。只有在当前有合法的src时返回。可读可写,改变currentTime值等同于调用seek。 |
| paused | boolean | - | 否 | 是 | 当前是否暂停或停止状态。 |
| buffered | number | - | 否 | 是 | 音频缓冲的时间点,仅保证当前播放时间点到此时间点内容已缓冲。 |
InnerAudioContext.destroy(): void
销毁当前实例。
起始版本: 1.0.14
示例:
const ctx = has.createInnerAudioContext(); ctx.destroy();
InnerAudioContext.play(): void
播放音频。
起始版本: 1.0.14
示例:
const ctx = has.createInnerAudioContext(); ctx.src = 'xxx'; // 开发时实际的音频资源地址 ctx.play();
InnerAudioContext.pause(): void
暂停。暂停后的音频再播放会从暂停处开始播放。
起始版本: 1.0.14
示例:
const ctx = has.createInnerAudioContext(); ctx.src = 'xxx'; // 开发时实际的音频资源地址 ctx.play(); ctx.pause(); ctx.play(); // 会从暂停处开始播放
InnerAudioContext.seek(number position): void
跳转到指定位置。
起始版本: 1.0.14
参数:
| 参数 | 类型 | 必填 | 描述 |
|---|---|---|---|
| position | number | 是 | 跳转的时间,单位:s。精确到小数点后3位,即支持ms级别精确度。 |
示例:
const ctx = has.createInnerAudioContext(); ctx.src = 'xxx'; // 开发时实际的音频资源地址 ctx.play(); ctx.seek(10);
InnerAudioContext.stop(): void
停止。停止后的音频再播放会从头开始播放。
起始版本: 1.0.14
示例:
const ctx = has.createInnerAudioContext(); ctx.src = 'xxx'; // 开发时实际的音频资源地址 ctx.play(); ctx.stop(); ctx.play(); // 会从头开始播放
InnerAudioContext.onCanplay(function callback): void
监听音频进入可以播放状态的事件。但不保证后面可以流畅播放。
起始版本: 1.0.14
参数:
| 参数 | 类型 | 必填 | 描述 |
|---|---|---|---|
| callback | function | 是 | 音频进入可以播放状态的事件的监听函数。 |
示例:
const ctx = has.createInnerAudioContext();
ctx.onCanplay(() => {
// 监听函数
});
ctx.src = 'xxx'; // 开发时实际的音频资源地址
ctx.play(); InnerAudioContext.offCanplay(function callback): void
移除音频进入可以播放状态的事件的监听函数。
起始版本: 1.0.14
参数:
| 参数 | 类型 | 必填 | 描述 |
|---|---|---|---|
| callback | function | 否 | InnerAudioContext.onCanplay传入的监听函数,不传此参数则移除所有监听函数。 |
示例:
const ctx = has.createInnerAudioContext();
const callback = ()=>{
// 监听函数
};
ctx.onCanplay(callback);
ctx.offCanplay(callback);
ctx.src = 'xxx'; // 开发时实际的音频资源地址
ctx.play(); InnerAudioContext.onEnded(function callback): void
监听音频自然播放至结束的事件。
起始版本: 1.0.14
参数:
| 参数 | 类型 | 必填 | 描述 |
|---|---|---|---|
| callback | function | 是 | 音频自然播放至结束的事件的监听函数。 |
示例:
const ctx = has.createInnerAudioContext();
ctx.onEnded(() => {
// 监听函数
});
ctx.src = 'xxx'; // 开发时实际的音频资源地址
ctx.play(); InnerAudioContext.offEnded(function callback): void
移除音频自然播放至结束的事件的监听函数。
起始版本: 1.0.14
参数:
| 参数 | 类型 | 必填 | 描述 |
|---|---|---|---|
| callback | function | 否 | InnerAudioContext.onEnded传入的监听函数。不传此参数则移除所有监听函数。 |
示例:
const ctx = has.createInnerAudioContext();
const callback = ()=>{
// 监听函数
};
ctx.onEnded(callback);
ctx.offEnded(callback);
ctx.src = 'xxx'; // 开发时实际的音频资源地址
ctx.play(); InnerAudioContext.onError(function callback(res: result)): void
监听音频播放错误事件。
起始版本: 1.0.14
参数:
| 参数 | 类型 | 必填 | 描述 |
|---|---|---|---|
| callback | function | 是 | 音频播放错误事件的监听函数。 |
result:
| 参数 | 类型 | 描述 |
|---|---|---|
| errMsg | string | 错误信息。 |
| errCode | number | 错误码。 |
示例:
const ctx = has.createInnerAudioContext();
ctx.onError((res) => {
// 监听函数
console.error(res.errMsg);
console.error(res.errCode);
});
ctx.src = 'xxx'; // 开发时实际的音频资源地址
ctx.play(); InnerAudioContext.offError(function callback): void
移除音频播放错误事件的监听函数。
起始版本: 1.0.14
参数:
| 参数 | 类型 | 必填 | 描述 |
|---|---|---|---|
| callback | function | 否 | InnerAudioContext.onError传入的监听函数。不传此参数则移除所有监听函数。 |
示例:
const ctx = has.createInnerAudioContext();
const callback = (res)=>{
// 监听函数
console.error(res.errMsg);
console.error(res.errCode);
};
ctx.onError(callback);
ctx.offError(callback);
ctx.src = 'xxx'; // 开发时实际的音频资源地址
ctx.play(); InnerAudioContext.onPause(function callback): void
监听音频暂停事件。
起始版本: 1.0.14
参数:
| 参数 | 类型 | 必填 | 描述 |
|---|---|---|---|
| callback | function | 是 | 音频暂停事件的监听函数。 |
示例:
const ctx = has.createInnerAudioContext();
ctx.onPause(() => {
// 监听函数
});
ctx.src = 'xxx'; // 开发时实际的音频资源地址
ctx.play();
ctx.pause(); InnerAudioContext.offPause(function callback): void
移除音频暂停事件的监听函数。
起始版本: 1.0.14
参数:
| 参数 | 类型 | 必填 | 描述 |
|---|---|---|---|
| callback | function | 否 | InnerAudioContext.onPause传入的监听函数。不传此参数则移除所有监听函数。 |
示例:
const ctx = has.createInnerAudioContext();
const callback = ()=>{
// 监听函数
};
ctx.onPause(callback);
ctx.offPause(callback);
ctx.src = 'xxx'; // 开发时实际的音频资源地址
ctx.play();
ctx.pause(); InnerAudioContext.onPlay(function callback): void
监听音频播放事件。
起始版本: 1.0.14
参数:
| 参数 | 类型 | 必填 | 描述 |
|---|---|---|---|
| callback | function | 是 | 音频播放事件的监听函数。 |
示例:
const ctx = has.createInnerAudioContext();
ctx.onPlay(() => {
// 监听函数
});
ctx.src = 'xxx'; // 开发时实际的音频资源地址
ctx.play(); InnerAudioContext.offPlay(function callback): void
移除音频播放事件的监听函数。
起始版本: 1.0.14
参数:
| 参数 | 类型 | 必填 | 描述 |
|---|---|---|---|
| callback | function | 否 | InnerAudioContext.onPlay传入的监听函数。不传此参数则移除所有监听函数。 |
示例:
const ctx = has.createInnerAudioContext();
const callback = ()=>{
// 监听函数
};
ctx.onPlay(callback);
ctx.offPlay(callback);
ctx.src = 'xxx'; // 开发时实际的音频资源地址
ctx.play(); InnerAudioContext.onSeeked(function callback): void
监听音频完成跳转操作的事件。
起始版本: 1.0.14
参数:
| 参数 | 类型 | 必填 | 描述 |
|---|---|---|---|
| callback | function | 是 | 音频完成跳转操作的事件的监听函数。 |
示例:
const ctx = has.createInnerAudioContext();
ctx.onSeeked(() => {
// 监听函数
});
ctx.src = 'xxx'; // 开发时实际的音频资源地址
ctx.play();
ctx.seek(10); InnerAudioContext.offSeeked(function callback): void
移除音频完成跳转操作的事件的监听函数。
起始版本: 1.0.14
参数:
| 参数 | 类型 | 必填 | 描述 |
|---|---|---|---|
| callback | function | 否 | InnerAudioContext.onSeeked传入的监听函数。不传此参数则移除所有监听函数。 |
示例:
const ctx = has.createInnerAudioContext();
const callback = ()=>{
// 监听函数
};
ctx.onSeeked(callback);
ctx.offSeeked(callback);
ctx.src = 'xxx'; // 开发时实际的音频资源地址
ctx.play();
ctx.seek(10); InnerAudioContext.onSeeking(function callback): void
监听音频进行跳转操作的事件。
起始版本: 1.0.14
参数:
| 参数 | 类型 | 必填 | 描述 |
|---|---|---|---|
| callback | function | 是 | 音频进行跳转操作的事件的监听函数。 |
示例:
const ctx = has.createInnerAudioContext();
ctx.onSeeking(() => {
// 监听函数
});
ctx.src = 'xxx'; // 开发时实际的音频资源地址
ctx.play();
ctx.seek(10); InnerAudioContext.offSeeking(function callback): void
移除音频进行跳转操作的事件的监听函数。
起始版本: 1.0.14
参数:
| 参数 | 类型 | 必填 | 描述 |
|---|---|---|---|
| callback | function | 否 | InnerAudioContext.onSeeking传入的监听函数。不传此参数则移除所有监听函数。 |
示例:
const ctx = has.createInnerAudioContext();
const callback = ()=>{
// 监听函数
};
ctx.onSeeking(callback);
ctx.offSeeking(callback);
ctx.src = 'xxx'; // 开发时实际的音频资源地址
ctx.play();
ctx.seek(10); InnerAudioContext.onStop(function callback): void
监听音频停止事件。
起始版本: 1.0.14
参数:
| 参数 | 类型 | 必填 | 描述 |
|---|---|---|---|
| callback | function | 是 | 音频停止事件的监听函数。 |
示例:
const ctx = has.createInnerAudioContext();
ctx.onStop(() => {
// 监听函数
});
ctx.src = 'xxx'; // 开发时实际的音频资源地址
ctx.play();
ctx.stop(); InnerAudioContext.offStop(function callback): void
移除音频停止事件的监听函数。
起始版本: 1.0.14
参数:
| 参数 | 类型 | 必填 | 描述 |
|---|---|---|---|
| callback | function | 否 | InnerAudioContext.onStop传入的监听函数。不传此参数则移除所有监听函数。 |
示例:
const ctx = has.createInnerAudioContext();
const callback = ()=>{
// 监听函数
};
ctx.onStop(callback);
ctx.offStop(callback);
ctx.src = 'xxx'; // 开发时实际的音频资源地址
ctx.play();
ctx.stop(); InnerAudioContext.onTimeUpdate(function callback): void
监听音频播放进度更新事件。
起始版本: 1.0.14
参数:
| 参数 | 类型 | 必填 | 描述 |
|---|---|---|---|
| callback | function | 是 | 音频播放进度更新事件的监听函数。 |
示例:
const ctx = has.createInnerAudioContext();
ctx.onTimeUpdate(() => {
// 监听函数
});
ctx.src = 'xxx'; // 开发时实际的音频资源地址
ctx.play(); InnerAudioContext.offTimeUpdate(function callback): void
移除音频播放进度更新事件的监听函数。
起始版本: 1.0.14
参数:
| 参数 | 类型 | 必填 | 描述 |
|---|---|---|---|
| callback | function | 否 | InnerAudioContext.onTimeUpdate传入的监听函数。不传此参数则移除所有监听函数。 |
示例:
const ctx = has.createInnerAudioContext();
const callback = ()=>{
// 监听函数
};
ctx.onTimeUpdate(callback);
ctx.offTimeUpdate(callback);
ctx.src = 'xxx'; // 开发时实际的音频资源地址
ctx.play(); InnerAudioContext.onWaiting(function callback): void
监听音频加载中事件。当音频因为数据不足,需要停下来加载时会触发。
起始版本: 1.0.14
参数:
| 参数 | 类型 | 必填 | 描述 |
|---|---|---|---|
| callback | function | 是 | 音频加载中事件的监听函数。 |
示例:
const ctx = has.createInnerAudioContext();
ctx.onWaiting(() => {
// 监听函数
});
ctx.src = 'xxx'; // 开发时实际的音频资源地址
ctx.play(); InnerAudioContext.offWaiting(function callback): void
移除音频加载中事件的监听函数。
起始版本: 1.0.14
参数:
| 参数 | 类型 | 必填 | 描述 |
|---|---|---|---|
| callback | function | 否 | InnerAudioContext.onWaiting传入的监听函数。不传此参数则移除所有监听函数。 |
示例:
const ctx = has.createInnerAudioContext();
const callback = () => {
// 监听函数
};
ctx.onWaiting(callback);
ctx.offWaiting(callback);
ctx.src = 'xxx'; // 开发时实际的音频资源地址
ctx.play();