CocosCreator版本:3.3.0
我在做Playable把打包资源压成一个html,在base64转audio时遇到了问题。
我为用assetManager.downloader.register来注册下载mp3功能,想把base64转成audio格式给引擎,但发现引擎下面还使用AudioPlayer.loadNative来使用http请求下载,请问大家有没有好的思路来处理这个问题呢。
assetManager.downloader.register('.mp3', function (url, options, callback) {
// 下载对应资源
......
});
static loadNative (url: string): Promise<AudioBuffer> {
return new Promise((resolve, reject) => {
const xhr = new XMLHttpRequest();
const errInfo = `load audio failed: ${url}, status: `;
xhr.open('GET', url, true);
xhr.responseType = 'arraybuffer';
xhr.onload = () => {
if (xhr.status === 200 || xhr.status === 0) {
audioContextAgent!.decodeAudioData(xhr.response).then((buffer) => {
resolve(buffer);
}).catch((e) => {});
} else {
reject(new Error(`${errInfo}${xhr.status}(no response)`));
}
};
xhr.onerror = () => { reject(new Error(`${errInfo}${xhr.status}(error)`)); };
xhr.ontimeout = () => { reject(new Error(`${errInfo}${xhr.status}(time out)`)); };
xhr.onabort = () => { reject(new Error(`${errInfo}${xhr.status}(abort)`)); };
xhr.send(null);
});
}