Loop audio and video separately AS3 - actionscript-3

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.

Related

FLASH AS3 - Loop video continuously

I found this code on the net, and it's almost perfect for my needs - but I need it to continuously loop the clip.
var so: Sound= new Sound();
var audioChannel: SoundChannel= new SoundChannel();
var audioTransform: SoundTransform = new SoundTransform();
var video:Video=new Video(320,240);
var nc:NetConnection=new NetConnection ;
nc.connect(null);
var ns:NetStream=new NetStream(nc);
var meta: Object = new Object();
meta.onMetaData = function(meta:Object){
trace(meta.duration);
};
ns.client=meta;
addChild(video);
video.attachNetStream(ns);
ns.play("vid.flv");
//clip is a transparent clip (alpha=0) for the entire scene
clip.addEventListener(MouseEvent.MOUSE_OVER,mute);
clip.addEventListener(MouseEvent.MOUSE_OUT,unmute);
function mute(e:MouseEvent) {
ns.soundTransform=audioTransform;
audioTransform.volume=0;
}
function unmute(e:MouseEvent) {
ns.soundTransform=audioTransform;
audioTransform.volume=1;
}
To loop a video stream in AS3:
Listen for the streams NET_STATUS event.
//ns is a netstream object
ns.addEventListener(NetStatusEvent.NET_STATUS, streamStatus);
In your streamStatus event handler, check if the status code is the video stop event:
function streamStatus(status:NetStatusEvent) {
if (status.info.code == "NetStream.Play.Stop") {
//the video has stopped
}
}
To loop the video, just seek to the beginning.
ns.seek(0);
So this is the final code:
ns.addEventListener(NetStatusEvent.NET_STATUS, streamStatus);
ns.play("vid.flv");
function streamStatus(status:NetStatusEvent) {
if (status.info.code == "NetStream.Play.Stop") {
ns.seek(0);
}
}
To see whatever status codes you can check for, see the documentation.

Lower volume of certain audio on frame with multiple audios. AS3

Okay, so, here's the deal, i have a sound steaming from an URL since the first frame (60 total), and i added another sound file (from the library) to a certain frame. As expected, both sounds overlapp, so i decided to make a mute function... the problem is, i cant get it to apply only to the streaming audio, the function mutes all audio, and i cant use the variable mySound because the mute function needs to be on other keyframe to work...
Here's the code of the streaming audio:
var mySound:Sound = new Sound();
var myChannel:SoundChannel = new SoundChannel();
var lastPosition:Number = 0;// pause button
mySound.load(new URLRequest("http://trollfacequiz.16mb.com/MusicFiles/Goat%20Songs.mp3"));
//mySound.play();
SoundMixer.stopAll();
myChannel = mySound.play();
btn_mute.btn_pause.addEventListener(MouseEvent.CLICK, onClickPause);
btn_mute.btn_play.addEventListener(MouseEvent.CLICK, onClickPlay);
function onClickPause(e:MouseEvent):void
{
btn_mute.btn_pause.visible = false;
lastPosition = myChannel.position;
myChannel.stop();
}
function onClickPlay(e:MouseEvent):void
{
btn_mute.btn_pause.visible = true;
myChannel = mySound.play(lastPosition);
}
And the mute function:
function setVolume(vol){
var volTransform:SoundTransform = new SoundTransform();
volTransform.volume = vol;
SoundMixer.soundTransform = volTransform;
}
setVolume(0);

Flash SoundChannel limit and memory management

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.

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.

Streaming audio and video AS3

I have couple of videos that I'm streaming. I start with the video_1.flv and after it finishes the video_2.flv is running and looping until the user takes some action to play other movie for example video_3.flv. So I need when video_2.flv is playing in the background to be played audio file sound.mp3 witch is going to be looped , witch i managed to do. But I need both of them to run independently. Because right now when the video is looped the audio is looping to. And I need the audio to be playd only when video_2.flv is played. Thank YOU.
var nc:NetConnection = new NetConnection();
nc.connect(null);
var ns:NetStream = new NetStream(nc);
loader.vid.Video_1.attachNetStream(ns);
var listener:Object = new Object();
listener.onMetaData = function(evt:Object):void {};
ns.client = listener;
ns.play("http://media.easyads.bg/ads/display_ads_richmedia/video/avon/maria_ilieva/video_1.flv");
ns.addEventListener(NetStatusEvent.NET_STATUS, NCListener);
var clipTimer:Timer = new Timer(4000);
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");
sound.load(req);
shaker(null);
}
};
var sound:Sound = new Sound();
var soundChannel:SoundChannel;
var req:URLRequest = new URLRequest("sound.mp3");
sound.addEventListener(Event.COMPLETE, onSoundLoadComplete);
function onSoundLoadComplete(e:Event):void{
sound.removeEventListener(Event.COMPLETE, onSoundLoadComplete);
soundChannel = sound.play();
soundChannel.addEventListener(Event.SOUND_COMPLETE, onSoundChannelSoundComplete);
}
function onSoundChannelSoundComplete(e:Event):void{
e.currentTarget.removeEventListener(Event.SOUND_COMPLETE, onSoundChannelSoundComplete);
soundChannel = sound.play();
}
loader.button_01.addEventListener(MouseEvent.CLICK, play_video_01);
loader.button_01.addEventListener(MouseEvent.ROLL_OVER, play_effect_01);
function play_video_01 (event:MouseEvent){
clipTimer.stop();
ns.play("http://media.easyads.bg/ads/display_ads_richmedia/video/avon/maria_ilieva/video_3.flv");
loader.button_01_mc.gotoAndPlay (41);
}
Maybe this tutorial is helpful to you...
I'm new in AS but I'd rather prefer to pack the whole s.... stuff into a (reusable) class...
Behavior of a Loaded SWF File versus Behavior of a MovieClip
Greets
Gee