常驻节点上的AudioSource组件在切换场景后自动停止播放背景音乐,应该怎么办?我没有在该场景勾选释放内存什么的。求解答,跪谢!!!!
常驻节点在场景切换过程中会先添加到新场景,此时新场景还未激活因此会经历一次 disable 再 enable。
项目里新建一个空脚本,加入这些代码试试
const proto = cc.AudioSource.prototype;
proto.rawOnDisable = proto.onDisable;
proto.rawOnEnable = proto.onEnable;
proto.onDisable = null;
proto.onEnable = null;
proto.onLoad = function () {
if (!this.audio.src) {
this.audio.src = this._clip;
}
if (this.preload) {
this._clip._ensureLoaded();
}
this.rawOnEnable();
};
proto.onDestroy = function () {
this.rawOnDisable();
this.audio.destroy();
};
3赞
现有的组件生命周期,在切场景的时候会执行到 onDisable 阶段,导致 audioSource 组件停止播放了,这个问题在 3.x 上有处理过,可以参考下这个修复的思路 3.x 的修复方法
或者最简单的方法,就是使用 audioEngine 来播放背景音乐
audioEngine 播放 不受组件生命周期的影响
该主题在最后一个回复创建后14天后自动关闭。不再允许新的回复。