Skip to main content
Known Participant
February 3, 2021
Question

Playing an external sound clip on a loop.

  • February 3, 2021
  • 1 reply
  • 382 views

Hi, sorry if this is an obvious thing, but I can't seem to find the answer.

I've made a simple HTML5 game and want to use a sound clip on a loop to give an ambient background noise. The method I've been using to play sounds is:

 

var ambience = new Audio('jungleSounds.aac');

ambience.play();

 

It works fine for one-off sound effects triggered but button clicks etc, but how would I loop a sound like this so that it plays continuously?

 

Thanks.

    This topic has been closed for replies.

    1 reply

    JoãoCésar17023019
    Community Expert
    Community Expert
    February 3, 2021

    Hi.

     

    It's better to use SoundJS, which is part of the CreateJS suit of libraries, to play and handle sounds. These libraries are integrated by default in HTML5 Canvas documents.

     

    If, for example, you want to play a sound in the Library with a linkage of Noise and set it to loop indefinitely, pass a PlayPropsConfig object as an argument to the static play method and set the loop property to -1. The code could be something like this:

     

    var noise = createjs.Sound.play("Noise", { loop: -1 }); // "Noise" is the symbol's linkage name in the Library

     

     

     

    I hope this helps.

     

    Regards,

    JC

    dc257Author
    Known Participant
    February 3, 2021

    That is great, thank you. Would this method also work with an external sound file from a folder rather than the library?

     

    My .aac files will not import into the library - I'm not sure if this is because that file format is unsupported by Animate, or if it is a result of the way they have been exported from Audition.

    JoãoCésar17023019
    Community Expert
    Community Expert
    February 3, 2021

    To handle external sounds, you're gonna need extra steps like in the example below:

    https://github.com/joao-cesar/adobe/tree/master/animate%20cc/html5_canvas/fade_sound_on_toggle

     

    But, if possible, try converting your sounds to MP3. It will be much easier to work with, IMO.

     

    Regards,

    JC