egret 【白鹭引擎】中 iphone 苹果手机微信没有声音
原创
©著作权归作者所有:来自51CTO博客作者ch_kexin的原创作品,请联系作者获取转载授权,否则将追究法律责任
在android 的手机上没有问题 但是在苹果手机的微信中听不到声音 。(ios环境下微信浏览器的音频自动播放问题)
解决方法:
<script>
function playsound(sound, loop) {
if (sound == null) {
console.log('sound err')
return;
}
var times = loop ? 0 : 1;
if (typeof WeixinJSBridge != 'undefined') {
WeixinJSBridge.invoke('getNetworkType', {}, function (e) {
return sound.play(0, times).volume = 0;
});
} else {
return sound.play(0, times).volume = 0;
}
}
</script>
接下来,我们怎么在ts中调用呢?
class SoundManager {
private static _instance: SoundManager;
private onOff: boolean = true;//音效总开关
private music: MusicPlay;//背景音乐
public static get instance() {
if (!this._instance) {
this._instance = new SoundManager();
}
return this._instance;
}
/**
* 播放音乐
*/
public playMusic(pKey: string = "", volume = 0.5) {
}
//停止播放音乐
public stopMusic() {
}
//构造函数
public constructor() {
this.music = new MusicPlay();
}
//静音引导后播放可以控制的背景音乐
//这个方法最好在loading加载完的时候就执行,再所有要播放音乐之前执行才好用
public playNullSound() {
//先加载音乐资源,获取到音乐资源,才能调用index.html 里面的方法,
我这里是在loading的时候提前加载好,所以同步获取资源
let nullSound: egret.Sound=RES.getRes("nullSound_mp3");
window["playsound"](nullSound, false);
}
}
//声音资源加载完成,播放静音,静音引导后播放可以控制的背景音乐
SoundManager.instance.playNullSound();