Looping sound in AS3 - actionscript-3

Trying to loop a background soundtrack while my flash program is in use.
So far my code is this:
//turn off sound
btnOff.addEventListener(MouseEvent.CLICK, fl_stopsound);
function fl_stopsound(event:MouseEvent):void
{
SoundMixer.stopAll();
}
//turns sound on
btnOn.addEventListener(MouseEvent.CLICK, fl_ClickToPlayStopSound_1);
var fl_SC_1:SoundChannel;
//keeps track of whether the sound should be played or stopped
var fl_ToPlay_1:Boolean = true;
function fl_ClickToPlayStopSound_1(evt:MouseEvent):void
{
var mySound:Sound = new background();
mySound.play();
}
where btnOff turns off the sound and btnOn turns on the sound. My soundtrack is 1:50min long. Is it possible to loop the track within the program with these buttons?
Cheers

Use mySound.play(0, int.MAX_VALUE);

Related

gotoAndPlay after sound ends as3

I would like my timeline to advance to a certain labeled frame after a sound completes. Here is the code that I have so far:
var n1Channel:SoundChannel = new narratorAlphabetSounds();
n1Channel.addEventListener(Event.SOUND_COMPLETE, audioComplete);
function audioComplete(e:Event):void
{
gotoAndPlay("step2");
}
The sound fails to load and errors occure when trying to test. Any suggestions? Thanks!
You got the implicit coercion error because your narratorAlphabetSounds object is a Sound object and not a SoundChannel one .. then to play your sound and detect when it has finished playing, you should use a Sound and a SoundChannel objects, like this :
var sound:Sound = new narratorAlphabetSounds();
var sound_channel:SoundChannel = sound.play();
sound_channel.addEventListener(Event.SOUND_COMPLETE, audioComplete);
function audioComplete(e:Event):void
{
trace('audio complete');
}
Hope that can help.

How to loop a sound after I use a soundchannel.stop() in the same sound? AS3 - FlashDevelop

I'm having a hard time to solve a sound loop in a screen that i have created, i would love if you could help me. I have created two screens: a startScreen and a gameScreen.If is the startScreen it plays a "bg sound", if is in the gameScreen the "bg sound" from the startScreen stop(i used the soundchannel.stop()). So far is okay but the soundchannel.stop(); doesn't make the sound be able to play again everytime is in the startScreen. Is there a possible to stop a sound without the soundchannel.stop(); and start again the sound from the begning everytime is in startScreen? heres a part of the code i have ben using:
private var scSound:Sound = new SCSOUND();
private var scSoundChannel:SoundChannel = scSound.play(0, 999);
private function initStartScreen():void
{
startScreen.visible = true;
gameScreen.visible = false;
scSoundChannel;
}
private function initGameScreen():void
{
gameScreen.visible = true;
startScreen.visible = false;
scSoundChannel.stop();
}
I tried to add a addEventListener for the sound but the removeEventListener doesn't wanna remove the sound.

Playlist loop in AS3 Flash

I'm very bad with Flash and I just can't do this.
I have php generated playlist.
I get it with LoaderInfo(), then with split() the data is added to array.
var paramObj:Object = LoaderInfo(this.root.loaderInfo).parameters;
var str:String = paramObj.playlist;
var array:Array = str.split(",");
Here is the problem. I know how to play 1 song from the playlist, once or in the loop with code like this:
sound.addEventListener(Event.COMPLETE, onLoadComplete, false, 0,
true);
function onLoadComplete(evt:Event):void {
channel = sound.play();
var pauseSong:Number = channel.position;
channel.addEventListener(Event.SOUND_COMPLETE, soundCompleteHandler);
}
function soundCompleteHandler(e:Event):void {
channel = sound.play();
channel.addEventListener(Event.SOUND_COMPLETE, soundCompleteHandler);
}
, but I cant make code to play in loop all songs in playlist -first, second, etc, first...
How to do that?
You make an array of your loading sounds, put a separate listener onto the first (0th) sound of your list that will initiate playback, all others should have another listener that will just notify your program that the sound is loaded. Once the first sound started playing, you add a listener to channel that will select next song and play it if it's available. If it's not available, your code should wait in a frame-based loop (Event.ENTER_FRAME listener with one single check) until it will finally load, and resume playlist playback.

streaming audio and video independently as3

I have this 2 functions. And as you can see the first one is streaming a video (witch is looping ) and the second one is streaming an audio ( witch si looping too ). So I need when the video is playing the audio to starts running to and to be looped independently because the length of my audio is bigger than the length of the video. Any ideas how can i manage to do that. Use code please.
function NCListener(e:NetStatusEvent){
if (e.info.code == "NetStream.Play.Stop") {
ns.play("http://media.easyads.bg/ads/display_ads_richmedia/video/avon/maria_ilieva/video_2.flv");
shaker(null);
}
};
var sound:Sound = new Sound();
var soundChannel:SoundChannel= new SoundChannel();
sound.addEventListener(Event.COMPLETE, onSoundLoadComplete );
function onSoundLoadComplete(e:Event):void{
sound.removeEventListener(Event.COMPLETE, onSoundLoadComplete);
soundChannel = sound.play(0, 9999);
soundChannel.addEventListener(Event.SOUND_COMPLETE, onSoundChannelSoundComplete);
}
function onSoundChannelSoundComplete(e:Event):void{
e.currentTarget.removeEventListener(Event.SOUND_COMPLETE, onSoundChannelSoundComplete);
soundChannel = sound.play(0, 9999);
}
First of all, you don't need the
soundChannel.addEventListener(Event.SOUND_COMPLETE, onSoundChannelSoundComplete);
It triggers when the sound has finished running, but since it's looping, there's no need to play it again.
First of all, you need to move the line
soundChannel = sound.play(0, 9999);
And place it right after your ns.play. So the sound will start as soon as your video start.
Then, to prevent it looping again when the video start, just check if soundChannel exists :
if (soundChannel == null)
soundChannel = sound.play(0, 9999);
Please notice that's there is no way to keep your sound in sync with your video afterwards, it depends completely on flashplayer will.

Recyclable Sound Object in Actionscript 3?

Using ONE Sound() object in Actionscript3, how can I play a one MP3 and then, when the user chooses a different one, play a second sound, using the SAME Sound() object?
EDIT: See my answer for how I did it.
You cannot use same Sound object to play multiple files.
Once load() is called on a Sound object, you can't later load a different sound file into that Sound object. To load a different sound file, create a new Sound object.
Ok, I actually did it using the following code. My bug was somewhere else in the FLA file, but this works. I made an uninitialized global variable and created the Sound() object LOCALLY inside of a function. While I'm technically using multiple sound objects, my references are all pointing to ONE object. Additionally, I can call these methods over one another for easier coding. This works for me:
/* -------------
Sound player
functions
------------ */
var snd:Sound; //the sound object
var sndC:SoundChannel; //the soudchannel used as "controller"
var sndT:SoundTransform; //soundTransform used for volume
var vol:Number = 1; //the volume of the song
var pan:Number = 0; //panning of the sound
var pos:Number = 0; //position of the song
var currentSound:String; //currently playing song?
function playSound(s:String){ //this function resets the sound and plays it
stopSound(sndC); //stop the sound from playing
snd = new Sound(); //reset the sound
snd.load(new URLRequest(s)); //load the desired sound
sndC = new SoundChannel(); //(re-)apply the sound channel
applyVolume(vol,pan,sndT,sndC); //apply the volume
sndC = snd.play(pos); //play it
sndC.addEventListener(Event.SOUND_COMPLETE, startSound); //remind it to restart playing when it's done
} //end function
function applyVolume(n:Number, p:Number, st:SoundTransform, sc:SoundChannel){ //takes an argument for the volume, pan, soundTYransform and soundChannel
sndT = new SoundTransform(n,p); //applies the soundTransfrom settings
sndC.soundTransform = sndT; //and attaches it to the soundChannel
} //end function
function stopSound(sndC:SoundChannel){ //this function stops a sound from playing
if(sndC != null){ //if the sound was used before (ie: playing)
if(currentLabel == "video-frame"){ //if we are in the video frame
pos = sndC.position; //store the position of the song to play from at a later time
}else{ //otherwise
pos = 0; //set the position at 0
} //end if
sndC.stop(); //stop it
} //end if
} //end function
function startSound(snd:Sound){ //restarts a sound when it's playing
if(snd != null){ //if the sound exists
sndC = snd.play(pos); //play it
} //end if
} //end function