FORUMS: list search recent posts

Flash CS5 AS2.0 pause netstream video

COW Forums : Adobe Flash

<< PREVIOUS   •   FAQ   •   VIEW ALL   •   PRINT   •   NEXT >>
Sean Jahnig
Flash CS5 AS2.0 pause netstream video
on Dec 2, 2015 at 12:30:57 pm

Hi guys,

I'm using basic AS2 for something and was wondering where i'm going wrong? Please help.

Here's the code:
var nc:NetConnection = new NetConnection();
nc.connect(null);
myNetStream = new NetStream(nc);
videoPlayer1.attachVideo(myNetStream);
myNetStream.play("_assets/16_airplane.flv");
function checkTime() {
var ns_seconds:Number = myNetStream.time();
trace(ns_seconds)
}
//setInterval takes name of function to call, and how many milliseconds between calls
//replace nsVideo with the name of your netstream
setInterval(checkTime,10);
function checkTime():Void{
//Math.floor rounds the time in seconds down to the nearest whole number
if(Math.floor(myNetStream.time) == 5){
btn_sometimes._alpha = 20;
myNetStream.pause();
}
}


The video loads, and plays, and pauses at 5 seconds as meant to. It also shows the button which is, until the point hidden. But the button doesn't un-pause.

The funny thing is, the button does pause and un-pause if you use it anytime before the loop has implemented the stop.

Any ideas?


Return to posts index

Sam Mattern
Re: Flash CS5 AS2.0 pause netstream video
on Dec 3, 2015 at 8:25:08 pm
Last Edited By Sam Mattern on Dec 3, 2015 at 8:29:03 pm

One thing I noticed above is that you have two checkTime functions and I'm not sure why.

I would put a trace on the button that refers to the netstream:
trace(myNetStream);
Hit the button before the video pauses automatically (which you said is working) and note the message. It should say something like object[NetStream]. Then after the video pauses, hit the button and see what message you get. If it's the same message you got before, then it's a problem with the pause command. If you don't get a message, you probably have a keyframe that is clearing out your button action before the video automatically pauses (your button action no longer exists). If the trace shows an error, you know that the button cannot see the netstream (scope) which is why it can't pause it.

-Sam


Return to posts index

Sean Jahnig
Re: Flash CS5 AS2.0 pause netstream video
on Dec 3, 2015 at 8:50:16 pm

Thanks Sam,

That first checkTime displays the playbar time of the flv as it's playing. I was using it to make sure that i was pausing where i was telling it to. If you completely remove that code it doesn't affect the functionality second checkTime.

I'll try what you've suggested and let you know what i find.

I'm not a code expect so i wouldn't know where to begin troubleshooting this, but i feel like the function statement locks the flv at 5 seconds once it's paused because it's a loop that's checking continuously. So even a button that's telling it to unpause, the code is overiding it. I didn't spend too much testing my theory but i did a netStream.seek(6); so that it forced the flv just out of the range where the function is true and it seemed to work.

Any idea how we could set the function to stop working once it's paused?


Return to posts index


Sam Mattern
Re: Flash CS5 AS2.0 pause netstream video
on Dec 3, 2015 at 9:15:51 pm

Update your code to store the setInterval as a variable. This allows you to clear it after you hit 5 seconds and pause your video. Then to start monitoring your netstream playback again after pressing your button (maybe to stop it at 10 seconds) you will need to call another setInterval statement.

var timeInterval = setInterval(checkTime,10);

function checkTime():Void{
if(Math.floor(myNetStream.time) == 5){
btn_sometimes._alpha = 20;
myNetStream.pause();
clearInterval(timeInterval);
}
}


-Sam


Return to posts index

Sean Jahnig
Re: Flash CS5 AS2.0 pause netstream video
on Dec 3, 2015 at 9:27:33 pm

YES!

It worked... well done. Now i can sleep tonight. Incase you want to see the file working, here's the link http://www.vibrant-sorcery.com/flash_test/test.swf

The button in the bottom middle only appears at 5 seconds when the video pauses. When you click it resumes and the button hides until 10 seconds when it pauses again. Thanks Sam!

This is the code i ended up using:

// load and play the video
var nc:NetConnection = new NetConnection();
nc.connect(null);
myNetStream = new NetStream(nc);
videoPlayer1.attachVideo(myNetStream);
myNetStream.play("_assets/16_airplane.flv");

// set variables for the setInterval so that it can be cleared
var timeInterval1 = setInterval(checkTime1,10);
var timeInterval2 = setInterval(checkTime2,10);

// first stop
function checkTime1():Void{
if(Math.floor(myNetStream.time) == 5){
btn_sometimes._alpha = 20;
myNetStream.pause();
clearInterval(timeInterval1);
}
}

// second stop
function checkTime2():Void{
if(Math.floor(myNetStream.time) == 10){
btn_sometimes._alpha = 20;
myNetStream.pause();
clearInterval(timeInterval2);
}
}


Return to posts index

Sam Mattern
Re: Flash CS5 AS2.0 pause netstream video
on Dec 3, 2015 at 9:49:35 pm

Awesome! Now, if you wanted to get fancy, instead of hiding your button you should disable it by default, enable it when you pause your video, then disable it again when you start playing again. That way you won't get the hand when you find it accidentally while it's hidden.

-Sam


Return to posts index


Sean Jahnig
Re: Flash CS5 AS2.0 pause netstream video
on Dec 3, 2015 at 9:57:43 pm

Haha,

Indeed that would be nice but that is a task for another day. For now, i'm going to enjoy this little victory.

Now i can create a 2 minute animation in After Effect with 20 key slides that blend into each other. Then i can play it through Flash with pauses so that it appears to work like a timed presentation, but with the power of having all the video effects. Doing the movement of elements in flash is not only very time consuming it's also very limited.

Thanks again Sam!


Return to posts index

Sean Jahnig
Re: Flash CS5 AS2.0 pause netstream video
on Nov 22, 2016 at 5:07:59 pm

Hey Sam,

Remember me? Hope all is well. Was hoping you could take this one step further and help me to get more control over this code.

The below is working, but i'm limited by having to stop the playing video at with 1 second sharp, or 2 seconds sharp. It would be great if it could stop it at 1.5 seconds (for example) or even better. If i could stop it by frame... for example first stop is at frame 22 and then 2nd stop is at frame 45 etc.

Hope you can help!

// load and play the video
var nc:NetConnection = new NetConnection();
nc.connect(null);
myNetStream = new NetStream(nc);
videoPlayer1.attachVideo(myNetStream);
myNetStream.play("_assets/2.flv");

// set variables for the setInterval so that it can be cleared
var timeInterval1 = setInterval(checkTime1,10);
var timeInterval2 = setInterval(checkTime2,10);
var timeInterval3 = setInterval(checkTime3,10);

// first stop
function checkTime1():Void{
if(Math.floor(myNetStream.time) == 1){
btn_sometimes._alpha = 20;
myNetStream.pause();
clearInterval(timeInterval1);
}
}

// second stop
function checkTime2():Void{
if(Math.floor(myNetStream.time) == 2){
btn_sometimes._alpha = 20;
myNetStream.pause();
clearInterval(timeInterval2);
}
}
// third stop
function checkTime3():Void{
if(Math.floor(myNetStream.time) == 4){
btn_sometimes._alpha = 20;
myNetStream.pause();
clearInterval(timeInterval3);
}
}


Return to posts index

Sam Mattern
Re: Flash CS5 AS2.0 pause netstream video
on Nov 22, 2016 at 5:28:30 pm

Well, you have two options. The reason that the video can only be stopped per second is because we're rounding down the playback head. So no matter if it's 2.01 or 2.99 seconds, the code is seeing it as 2 because we are running it through Math.floor(). If instead you want to check for a spot specific to the hundredths, you would need to update the code to:
var timeInterval1 = setInterval(checkTime1,5); //may need to go from 10 to 5 to check more frequently
// first stop
function checkTime1():Void{
/*new rounding scheme will round to closest hundredth. You can modify this to thousandths by changing "*100/100" to "*1000/1000"*/
if((int((myNetStream.time)*100)/100) == 1.15){
btn_sometimes._alpha = 20;
myNetStream.pause();
clearInterval(timeInterval1);
}
}

The other option is to import your video directly to the timeline and scratch all of this code. It converts the movie into still frames that fill up your timeline. You can then simply put "stop();" on the frames you want it to stop at. I've used this method for simple videos, but it can get ugly very quickly. You'll also probably need control the audio separately.

-Sam


Return to posts index


Sean Jahnig
Re: Flash CS5 AS2.0 pause netstream video
on Nov 22, 2016 at 7:51:23 pm

Hey Sam,

Amazing - thanks for your quick reply! Appreciate it.

I actually found that seeking method to be a little sketchy - i guess it's just the way Flash handles netstream videos. Random previews of the flash file will result in certain points being missed and other times not. I'm guessing due to the period as you suggested but as i make the number smaller i feel like it's putting strain on the presentation.. it becomes jerky.

As you suggested i've placed the flv on the timeline (in a movieclip) which has given me much better control of the video. This is not really ideal because the ultimate goal is to keep the many videos that the presentation references in an external folder - that allows me to update the videos if required without touching the code. Nevertheless, it does work so i'm grateful.

Do you know if the method of video on the timeline (in a movieclip) is known for being specifically buggy? The reason i ask is because it looks great now but when doing the actual presentation i'd hate the flash file to crash.

Thanks a lot


Return to posts index

Sam Mattern
Re: Flash CS5 AS2.0 pause netstream video
on Nov 22, 2016 at 9:08:22 pm

Not sure if there are issues with it crashing. Rather, I've had some issues with the playback not being true to time (getting a little ahead, then a little behind) during playback. Also, once you have the video on the timeline, you cannot change the frame rate of your project. If you do, you'll have to reimport the video.

If you did want to keep the files external, you could do a hybrid approach. Create a blank project and load your video to a timeline. Add in any hard stops on frames if you like. Then export that project as a swf. Then in your main project, you can load that swf in as a native movieclip. It will give you full control over the timeline and playback, while keeping your main project small and nimble. Here is how that looks:

var mcExt:MovieClip; //variable to store your video in
var ldr:Loader = new Loader();
ldr.contentLoaderInfo.addEventListener(Event.COMPLETE, swfLoaded);//once file is loaded, run function swfLoaded
ldr.load(new URLRequest("ext.swf")); //enter the path and name of the file you are loading

function swfLoaded(e:Event):void {<blockquote>mcExt = MovieClip(ldr.contentLoaderInfo.content);//associates mxExt variable with the loaded video
ldr.contentLoaderInfo.removeEventListener(Event.COMPLETE, swfLoaded);
mcExt.x = 50;//where on the stage you want video to be
mcExt.y = 50;//where on the stage you want video to be
addChild(mcExt);//add it to the stage</blockquote>}
//Then you can manipulate it like:
mxExt.stop();
mxExt.gotoAndPlay(50);

Anyway, it's an option if you want a reusable platform that you can keep small and nimble.

-Sam


Return to posts index

Sean Jahnig
Re: Flash CS5 AS2.0 pause netstream video
on Nov 23, 2016 at 12:23:52 pm

Excellent, thanks again Sam!


Return to posts index

<< PREVIOUS   •   VIEW ALL   •   PRINT   •   NEXT >>
© 2017 CreativeCOW.net All Rights Reserved
[TOP]