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 !
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();
Copy link to clipboard
Copied
your sound's play() methods accept a loop paramenter:
channel = music.play(0,100); // 100 loops
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 ?
Copy link to clipboard
Copied
replace:
channel = music.play();
with:
channel = music.play(0,100);
Copy link to clipboard
Copied
Nope, It's not working...I'm not sure that "var nextSong" is using the playSound function...
Copy link to clipboard
Copied
what code are you using to play your sound?
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();
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(); }
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);
Copy link to clipboard
Copied
PERFECT ! THANK YOU VERY MUCH !!!!
Copy link to clipboard
Copied
you're welcome.