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.
Related
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);
I have a program that I use as a recording platform, and for each sound I play I make a new Sound.
public function playAudioFile():void {
trace("audio file:", currentAudioFile);
sound = new Sound();
sound.addEventListener(IOErrorEvent.IO_ERROR, errorHandler);
sound.addEventListener(Event.COMPLETE, soundLoaded);
sound.load(new URLRequest(soundLocation + currentAudioFile));
}
public function soundLoaded(event:Event):void {
sound.removeEventListener(IOErrorEvent.IO_ERROR, errorHandler);
sound.removeEventListener(Event.COMPLETE, soundLoaded);
var soundChannel:SoundChannel = new SoundChannel();
soundChannel = sound.play();
soundChannel.addEventListener(Event.SOUND_COMPLETE, handleSoundComplete);
trace('soundChannel?', soundChannel);
}
public function handleSoundComplete(event:Event):void {
var soundChannel:SoundChannel = event.target as SoundChannel;
soundChannel.stop();
soundChannel.removeEventListener(Event.SOUND_COMPLETE,handleSoundComplete);
soundChannel = null;
}
After 32 times, I stop getting a SoundChannel object when I call sound.play() (in soundLoaded). However, I don't need to have 32 SoundChannel objects because I play these sounds only serially and not at the same time. How can I get rid of the SoundChannel after I 'used' it to play a file?
You could be explicit about the soundChannel you use :
var soundChannel:SoundChannel = new SoundChannel();
soundChannel = sound.play();
soundChannel.addEventListener(Event.SOUND_COMPLETE,handleSoundComplete);
Then when you are done playing the sound, you can then set the channel to null so it will be marked for garbage collection :
function handleSoundComplete(e:Event):void
{
var soundChannel:SoundChannel = e.target as SoundChannel;
soundChannel.removeEventListener(Event.SOUND_COMPLETE,handleSoundComplete);
soundChannel = null;
}
Keep in mind that you need to remove those event listeners that you show in your code above, when the sound is done loading.
Also keep in mind that when you set something to null, that just sets it as fair game for garbage collection, it doesn't force garbage collection.
Another note is that this code I have posted is just an example, and you might want to think about having several sound channels instances that you keep active, and reuse as needed. Management is required in that case, but then you will not be constantly creating/killing sound channels.
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.
I have this function witch is looping everything in it after the streamed video finishes Is there a way to loop only the streamed video and shaker(null) without the audio ? Because the audio is loping to but it length is biger. So basically I need the video and audio to be independently looped.
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");
soundChannel = sound.play(0, 9999);
shaker(null);
}
};
var sound:Sound = new Sound();
var soundChannel:SoundChannel= new SoundChannel();
sound.load(new URLRequest("sound.mp3"));
sound.addEventListener(Event.COMPLETE, onSoundLoadComplete );
function onSoundLoadComplete(e:Event):void{
sound.removeEventListener(Event.COMPLETE, onSoundLoadComplete);
soundChannel.addEventListener(Event.SOUND_COMPLETE, onSoundChannelSoundComplete);
}
function onSoundChannelSoundComplete(e:Event):void{
e.currentTarget.removeEventListener(Event.SOUND_COMPLETE, onSoundChannelSoundComplete);
soundChannel = sound.play(0, 9999);
}
For independent looping, you'll need to listen to events on both the NetStream (onPlayStatus) and the SoundChannel (soundComplete). In the event handler, you'd restart just the media that completed.
It looks like your sample code is just listening to the NetStream.
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