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

Loop sound in each background

New Here ,
Sep 04, 2013 Sep 04, 2013

Copy link to clipboard

Copied

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

Views

1.0K

Translate

Translate

Report

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

Votes

Translate

Translate
Community Expert ,
Sep 04, 2013 Sep 04, 2013

Copy link to clipboard

Copied

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

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

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

replace:

channel = music.play();

with:

channel = music.play(0,100);

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

what code are you using to play your sound?

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

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


Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

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


Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

PERFECT ! THANK YOU VERY MUCH !!!!

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

LATEST

you're welcome.

Votes

Translate

Translate

Report

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