No problem on android phone but no sound can be heard in wechat on iphone. (Audio auto-play problem of WeChat browser in ios environment)
Solution:
<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>
Next, how do we call it in ts?
class SoundManager { private static _instance: SoundManager; private onOff: boolean = true;//Sound effect master switch private music: MusicPlay;//background music public static get instance() { if (!this._instance) { this._instance = new SoundManager(); } return this._instance; } /** * play music */ public playMusic(pKey: string = "", volume = 0.5) { } //stop playing music public stopMusic() { } //Constructor public constructor() { this.music = new MusicPlay(); } //Play background music that can be controlled after mute boot //This method is best executed when loading is finished, and it is easy to use before all music is played. public playNullSound() { //First load the music resources and get the music resources before calling the methods in index.html, I load it in advance when loading, so I get resources synchronously let nullSound: egret.Sound=RES.getRes("nullSound_mp3"); window["playsound"](nullSound, false); } } //The sound resource is loaded, the playback is muted, and the background music that can be controlled is played after the mute guide SoundManager.instance.playNullSound();