基础信息说明
功能概述:监听与处理小游戏运行时的全局错误和音频播放被系统中断的事件。
方法说明
全局错误
| 方法名 | 签名 | 同步/异步 | 说明 |
| onError | qg.onError(Function callback): void | 同步注册,回调异步触发 | 监听全局运行时错误 |
| offError | qg.offError(Function callback): void | 同步解绑 | 取消 onError 绑定的回调 |
音频错误
| 方法名 | 签名 | 同步/异步 | 说明 |
| onAudioInterruptionBegin | qg.onAudioInterruptionBegin(Function callback): void | 同步注册,回调异步触发 | 音频被系统占用而中断开始 |
| offAudioInterruptionBegin | qg.offAudioInterruptionBegin(Function callback): void | 同步解绑 | 取消 onAudioInterruptionBegin 绑定的回调 |
| onAudioInterruptionEnd | qg.onAudioInterruptionEnd(Function callback): void | 同步注册,回调异步触发 | 音频中断结束(可重新播放) |
| offAudioInterruptionEnd | qg.offAudioInterruptionEnd(Function callback): void | 同步解绑 | 取消 onAudioInterruptionEnd 绑定的回调 |
参数说明
qg.onError(Function callback)
| 属性名 | 类型 | 必填 | 说明 |
| callback | Function(String|Object error) | 否 | 全局错误事件的监听函数 |
回调参数常见结构
| 字段 | 类型 | 说明 |
| message | String | 错误信息 |
| stack | String | 错误堆栈信息 |
qg.offError(Function callback)
| 属性名 | 类型 | 默认值(如有) | 必填 | 说明 |
| callback | Function | - | 否 | 需传入与 onError 注册时相同的函数引用;空参不会清除全部 |
qg.onAudioInterruptionBegin(Function callback)
| 属性名 | 类型 | 默认值(如有) | 必填 | 说明 |
| callback | Function | - | 否 | 音频因为受到系统占用而被中断开始事件的监听函数 |
触发场景:闹钟、电话、FaceTime 通话、微信语音聊天、微信视频聊天等系统占用音频时。
qg.offAudioInterruptionBegin(Function callback)
| 属性名 | 类型 | 默认值(如有) | 必填 | 说明 |
| callback | Function | - | 否 | 需传入与 onAudioInterruptionBegin 注册时相同的函数引用;空参不会清除全部 |
qg.onAudioInterruptionEnd(Function callback)
| 属性名 | 类型 | 默认值(如有) | 必填 | 说明 |
| callback | Function | - | 否 | 音频中断结束时触发;收到该事件后才可重新播放音频 |
qg.offAudioInterruptionEnd(Function callback)
| 属性名 | 类型 | 默认值(如有) | 必填 | 说明 |
| callback | Function | - | 否 | 需传入与 onAudioInterruptionEnd 注册时相同的函数引用;空参不会清除全部 |
回调结果说明
| 回调/事件 | 触发时机 | 回调参数 | 说明 |
| onError 事件 | 小游戏运行时抛出错误 | error:错误信息或对象(常见含 message、stack) | - |
| onAudioInterruptionBegin 事件 | 系统占用导致音频被中断 | info:底层透传中断信息 | 收到后游戏内音频被暂停 |
| onAudioInterruptionEnd 事件 | 音频中断结束 | info:底层透传结束信息 | 收到后音频可再次播放 |
示例代码
const errorListener = function(res) {
console.log('occur exception:', res);
};
qg.onError(errorListener);
qg.offError(errorListener); // 需传入与监听时同一个的函数对象
const audioItpEndListener = function() {
console.log('audio interruption end!');
};
qg.onAudioInterruptionEnd(audioItpEndListener);
qg.offAudioInterruptionEnd(audioItpEndListener); // 需传入与监听时同一个的函数对象
const audioItpBeginListener = function() {
console.log('audio interruption begin!');
};
qg.onAudioInterruptionBegin(audioItpBeginListener);
qg.offAudioInterruptionBegin(audioItpBeginListener); // 需传入与监听时同一个的函数对象
注意事项