First, we want to put the sound and background music required for the project in the Res directory under the project.
Then to preload this music at the beginning of the project
//Pre-load main game page picture resource array varRes:array<any> =[{URL:"Res/atlas/ui.json", Type:Laya.Loader.ATLAS}, {URL:"Res/atlas/ui.png", Type:Laya.Loader.IMAGE}, {URL:"Res/atlas/bg.mp3", Type:Laya.Loader.SOUND},//pre-load background music{URL: "Res/atlas/hit.wav", Type:Laya.Loader.SOUND}//pre-load percussion sound ]; //set the 4th parameter of progress handler to true to get the loading progress based on the number of loaded filesLaya.loader.load (Res,NULL, Laya.Handler.create ( This, This. OnProgress,NULL,false));
You can call background music and sound when you need it.
Laya.media.SoundManager.playMusic ("Res/atlas/bg.mp3", 0); // 0 means loop playback
Laya.media.SoundManager.playSound ("Res/atlas/hit.wav", 1);
Note that here the background music and percussion sound is not the same method called the Oh, one is the Playmusic () method, one is the PlaySound () method, note the inside path Oh, is res inside the file
Note that you must use preload to load music and sound effects, which can cause music to delay playback if you call it directly.
Layabox How to add background music and sound effects