Skip to main content
Participant
September 4, 2013
Answered

Loop sound in each background

  • September 4, 2013
  • 2 replies
  • 1263 views

Someone helped me last time with my sounds. Now, when my player enters in a scene, a new sound starts (and the sound of the previous background stops).

But now I can't find a way to loop the sound in each background...I'm trying to use

public function playSound():void{

            channel = music.play();

            channel.soundTransform = transformer;

            channel.addEventListener(Event.SOUND_COMPLETE, loopSound, false, 0, true);

            }

            private function loopSound(e:Event):void{

                channel.removeEventListener(Event.SOUND_COMPLETE, loopSound);

                playSound();

            }

But it doesn't seem to work. In fact, I don't really know when should I call this function.

I've got a quite simple code :

  public var currentSoundChannel:SoundChannel;

    public function newBackground(thisBack:String):void

    {

        var room = back.currentBack;

        if (currentSoundChannel != null)

        {

            currentSoundChannel.stop()

        }

        var nextSong:Sound;

        switch (thisBack){

            case "maisonExt":

                nextSong = new exterieur ();

                break;

            case "garage":

                nextSong = new mouche ();

                break;

            case "monde":

                nextSong = new cartesnd();

                break;

        }

        currentSoundChannel = nextSong.play();

    }

So, where should I put my loopsound function ?

Thank you very much !

This topic has been closed for replies.
Correct answer kglad

Normaly I use a class for the background music named "musique.as". Here's the code :

public function Muzak(musicURL:String, musicVol:Number){

                                               this.musicURL = musicURL;

                                               if(musicURL != ""){

                                                               music = new Sound(new URLRequest(musicURL));

                                                               transformer  = new SoundTransform(musicVol, 0);

                                                               playSound();

                                               }

                               }

                               public function playSound():void{

                                               channel = music.play(0,100);

                                               channel.soundTransform = transformer;

                                               channel.addEventListener(Event.SOUND_COMPLETE, loopSound, false, 0, true);

                               }

                               private function loopSound(e:Event):void{

                                               channel.removeEventListener(Event.SOUND_COMPLETE, loopSound);

                                               playSound();

                               }

                               public function changeVolume(newVol:Number):void{

                                               if(musicURL != ""){

                                                               transformer.volume = newVol;

                                                               channel.soundTransform = transformer;

                                               }

                               }

                               public function stopSound():void{

                                               channel.stop();

                               }

But as I wanted to change the music in each background so I add in my Engine class :

public var currentSoundChannel:SoundChannel;        

public function newBackground(thisBack:String):void     {        

var room = back.currentBack;       

if (currentSoundChannel != null)       

{           

currentSoundChannel.stop()      

  }     

   var nextSong:Sound;

.....

nextSong = new "mysoundname"();

....

}         currentSoundChannel = nextSong.play();



use:

public function Muzak(musicURL:String, musicVol:Number){
                                               this.musicURL = musicURL;
                                               if(musicURL != ""){
                                                               music = new Sound(new URLRequest(musicURL));
                                                               transformer  = new SoundTransform(musicVol, 0);
                                                               playSound();
                                               }
                               }
 
                               public function playSound():void{
                                               channel = music.play(0,100);
                                               channel.soundTransform = transformer;
                                              // channel.addEventListener(Event.SOUND_COMPLETE, loopSound, false, 0, true);
                               }
 
                               private function loopSound(e:Event):void{
                                               channel.removeEventListener(Event.SOUND_COMPLETE, loopSound);
                                               playSound();
                               }
 
                               public function changeVolume(newVol:Number):void{
                                               if(musicURL != ""){
                                                               transformer.volume = newVol;
                                                               channel.soundTransform = transformer;
                                               }
                               }
 
                               public function stopSound():void{
                                               channel.stop();
                               }

or

public var currentSoundChannel:SoundChannel;         
public function newBackground(thisBack:String):void     {         
var room = back.currentBack;        
 if (currentSoundChannel != null)        
 {            
 currentSoundChannel.stop()       
  }      
   var nextSong:Sound;
.....
 nextSong = new "mysoundname"();
....
}         currentSoundChannel = nextSong.play(0,100);


2 replies

Beber4Author
Participant
September 4, 2013

And I should put "channel = music.play(0,100); // 100 loops" where exactly in order to have a loop sound in each background ?

kglad
Community Expert
Community Expert
September 4, 2013

replace:

channel = music.play();

with:

channel = music.play(0,100);

Beber4Author
Participant
September 4, 2013

Nope, It's not working...I'm not sure that "var nextSong" is using the playSound function...

kglad
Community Expert
Community Expert
September 4, 2013

your sound's play() methods accept a loop paramenter:

channel = music.play(0,100); // 100 loops