Layabox How to add background music and sound effects

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.