Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Loop sound in each background

New Here ,
Sep 04, 2013 Sep 04, 2013

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 !

TOPICS
ActionScript
1.2K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Sep 05, 2013 Sep 05, 2013

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();
    
...
Translate
Community Expert ,
Sep 04, 2013 Sep 04, 2013

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

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Sep 04, 2013 Sep 04, 2013

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 04, 2013 Sep 04, 2013

replace:

channel = music.play();

with:

channel = music.play(0,100);

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Sep 04, 2013 Sep 04, 2013

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 04, 2013 Sep 04, 2013

what code are you using to play your sound?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Sep 05, 2013 Sep 05, 2013

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();


Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 05, 2013 Sep 05, 2013

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);


Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Sep 05, 2013 Sep 05, 2013

PERFECT ! THANK YOU VERY MUCH !!!!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 05, 2013 Sep 05, 2013
LATEST

you're welcome.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines