Video play error in Actionscript3 - actionscript-3

i am working on an actionscript3 project. when i am loading the video file in a class without the e:Event parameter;
private function setupVideo_2():void
{
vid = new Video(640,480);
addChild(vid);
nc= new NetConnection();
nc.connect(null);
ns= new NetStream(nc);
vid.attachNetStream(ns);
var listener:Object = new Object();
listener.onMetaData= function (evt:Object):void{};
ns.client = listener;
ns.play("introduction to 3G and 4G.flv");
}
it plays fine.
The moment i put it in the condition stating play only when the marker pattern is sited; it loadz but there is a visible lag followed by stopping of the video and repeating of the first line around 3 times and then the air application stops working and eventually crashes.
here is the code for it.
private function loop (e:Event):void
{
bmd.draw(vid);
try
{
if (detector.detectMarkerLite(raster, 80)&& detector.getConfidence() > 0.5)
{
vid = new Video(640,480);
addChild(vid);
nc= new NetConnection();
nc.connect(null);
ns= new NetStream(nc);
vid.attachNetStream(ns);
var listener:Object = new Object();
listener.onMetaData= function (evt:Object):void{};
ns.client = listener;
ns.play("4 Clever Ethernet Cable Hacks.flv");
}
}
catch(e:Error){}
}
Please tell me why is the video lagging when played inside the if condition.

Related

How to display mp4 then pause and replay in Flash CC

I’m resurrecting a simple banner where we used an FLV movie and the following code to have the movie pause using setInterval and then play again. It’s a simple butterfly movie that flaps it’s wings and then goes back to a static state.
Looks like FLVs are now discouraged in the more recent Flash versions so I would like to know how to make a movie play, then pause using setInterval, then replay.
Here is the working AS3 when using an FLV on the Timeline in a frame based animation but does not work with an imported .mp4:
stop();
function timesUp()
{
gotoAndPlay(1,"Scene 1");
clearInterval(itv);
}
var itv=setInterval(timesUp, 15000);
Do the new suggested methods use a single frame movie where you load and play the mp4, then use setInterval as a timer to replay?
I can’t find any tutorials that have this method as an example.
I did read a tutorial on the Adobe site but the following did not load and play the movie (morpho.mp4)
var nc:NetConnection = new NetConnection();
nc.connect(null);
var vid:Video = new Video();
addChild(vid);
var ns:NetStream = new NetStream(nc);
ns.addEventListener(NetStatusEvent.NET_STATUS,netStatusHandler);
ns.addEventListener(AsyncErrorEvent.ASYNC_ERROR, asyncErrorHandler);
function netStatusHandler(event:NetStatusEvent):void
{
// handle netStatus events, described later
}
function asyncErrorHandler(event:AsyncErrorEvent):void
{
// ignore error
}
vid.attachNetStream(ns);
ns.play("morpho.mp4");
Try this :
import flash.utils.Timer
import flash.events.TimerEvent
var video_playing:Boolean = false,
timer:Timer,
nc:NetConnection,
ns:NetStream,
video:Video,
video_name:String = 'video.mp4'
nc = new NetConnection()
nc.connect(null)
ns = new NetStream(nc)
ns.addEventListener(NetStatusEvent.NET_STATUS,netStatusHandler);
ns.addEventListener(AsyncErrorEvent.ASYNC_ERROR, asyncErrorHandler);
function netStatusHandler(event:NetStatusEvent):void {
switch (event.info.code){
case 'NetStream.Play.Start' :
if(timer.currentCount == 0){
timer.start()
}
break
}
}
function asyncErrorHandler(event:AsyncErrorEvent):void {
}
video = new Video(135, 135) // set the size of video to 135x135px
video.x = 165 // set the left of video to 300 - 135 = 165px
video.y = 0 // set the top of video to 0
video.attachNetStream(ns)
addChild(video)
timer = new Timer(1000, 3) // 3 seconds, just for example
timer.addEventListener(TimerEvent.TIMER_COMPLETE, timer_on_Complete)
function timer_on_Complete(e:TimerEvent){
if(video_playing){
video_playing = false
ns.close()
} else {
start_video()
}
timer.reset()
timer.start()
}
start_video()
function start_video(){
ns.play(video_name)
video_playing = true
}

AS3 play multy video files

I have this code for playing flv video when swf starts. How can I make it play 2 or 3 flv videos in sequence? here is the code which loads flv and plays it so I need to play two more videos after the first one automatically.
var vid:Video = new Video(1080, 720);
addChild(vid);
var nc:NetConnection = new NetConnection();
nc.connect(null);
var ns:NetStream = new NetStream(nc);
vid.attachNetStream(ns);
var listener:Object = new Object();
listener.onMetaData = function(evt:Object):void {};
ns.client = listener;
ns.play("Postvideo1.flv");
You are very close. NetStream client will help you to solve this task. More details on onPlayStatus
Establishes a listener to respond when a NetStream object has completely played a stream.
var listener:Object = {};
listener.onMetaData = function (meta:Object):void {
//Video duration
trace(meta.duration);
};
listener.onPlayStatus = function (data:Object):void {
if (data.code == "NetStream.Play.Complete") {
trace("Video playback is completed!");
//Good place to initiate playback of another video
}
}
ns.client = listener;

streaming video player / attaching video(not camera) to NetStream in actionscript 3

I am new to actionscript, basically i am trying to stream video player but we can't use attachVideo in as3 so what can i use instead of attachVideo in following code? Im using flash builder/flex 4.6 . If you could suggest link/tutorial for streaming video player it would be great help. Thank you!
private function initOutStream():void
{ /* streamOut is NetStream to adobe cirrus ,
conn is making connection to adobe cirrus */
trace("initOutStream");
streamOut = new NetStream(conn,NetStream.DIRECT_CONNECTIONS);
streamOut.addEventListener(NetStatusEvent.NET_STATUS,streamStatus);
streamOut.publish("media");
// mp4 file from local machine
nc = new NetConnection();
nc.connect(null);
ns = new NetStream(nc);
ns.play("t.mp4");
ns.client = this;
var vid:Video = new Video;
vid.attachNetStream(ns);
// streaming vid to media server
streamOut.attachVideo(vid);
var streamOutClient:Object = new Object();
streamOutClient.onPeerConnect = function(farStream:NetStream):Boolean
{
return true;
}
}
error:
1061: Call to a possibly undefined method attachVideo through a reference with static type
This is a code i wrote lately for a business job. i hope it helps...
var vid:Video = new Video();
vid.width = 640; vid.height = 480;
addChild(vid);
var cnn:NetConnection = new NetConnection();
cnn.connect(null);
var ns:NetStream = new NetStream(cnn);
ns.play("t.mp4");
vid.attachNetStream(ns);
ns.addEventListener(AsyncErrorEvent.ASYNC_ERROR, error);
ns.addEventListener(NetStatusEvent.NET_STATUS, streamStatus);
function streamStatus(e:NetStatusEvent){
//trace(e.info.code);
if(e.info.code == "NetStream.Play.Stop"){
}
}
function error(e){
}

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

AS3 gotoAndPlay after video finishes playing

I have loaded a video wit this code
var video:Video = new Video(1440, 900);
addChild(video);
var nc:NetConnection = new NetConnection();
nc.connect(null);
var ns:NetStream = new NetStream(nc);
video.attachNetStream(ns);
var listener:Object = new Object();
listener.onMetaData = function(e:Object):void {};
ns.client = listener;
ns.play("introAPOK_blanco.f4v");
now I need to perform a "gotoAndPlay" to go to another frame or scene after the video finishes playing
Thanks!!
video.addEventListnere("complete", onPlayFinished);
function onPlayFinished(event:Event):void
{
//Video playback finished
}
UPDATE 1:
Check this tutorial http://www.adobe.com/devnet/flash/articles/flvplayback_programming.html
instead of "complete" use VideoEvent.COMPLETE