Looping MP4 video with AS3

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Looping MP4 video with AS3



I am trying to get a simple loaded MP4 video to loop in the background.



The component name is "videoloop"
and the file name is "OTG_MainLoop_1080p.mp4"



Here is the code.


videoloop.source="Content/OTG_MainLoop_1080p.mp4"
videoloop.autoRewind=true
videoloop.addEventListener(VideoEvent.AUTO_REWOUND, videoPlayAgain,
false, 0, true)
function videoPlayAgain( event:VideoEvent):void

event.target.play()



This is the error I get in the console, if it helps any.



TypeError: Error #1034: Type Coercion failed: cannot convert fl.video::VideoEvent@460cf159 to flash.events.VideoEvent.
at flash.events::EventDispatcher/dispatchEventFunction()
at flash.events::EventDispatcher/dispatchEvent()
at fl.video::FLVPlayback/http://www.adobe.com/2007/flash/flvplayback/internal::handleVideoEvent()
at flash.events::EventDispatcher/dispatchEventFunction()
at flash.events::EventDispatcher/dispatchEvent()
at fl.video::VideoPlayer/http://www.adobe.com/2007/flash/flvplayback/internal::setState()
at fl.video::VideoPlayer/http://www.adobe.com/2007/flash/flvplayback/internal::setStateFromCachedState()
at fl.video::VideoPlayer/http://www.adobe.com/2007/flash/flvplayback/internal::httpDoSeek()
at flash.utils::Timer/_timerDispatch()
at flash.utils::Timer/tick()



Any help would be greatly appreciated.



Thanks




1 Answer
1



Your error is because AS3 compiler is confusing your FLV Component's own internal videoEvent #1 against the other built-in Video Event #2 which is meant to be used for Video and NetStream based objects.


Video


NetStream



These two types are not compatible so you cannot force (coerce) a type #1 to act like a type #2. ...At least not while using AS3 language.


type #1


type #2



solution:



It works if you listen for the event is "complete" signal:


event is "complete"


videoloop.source="Content/OTG_MainLoop_1080p.mp4";
//videoloop.autoRewind=true; //is not needed.

videoloop.addEventListener("complete", videoPlayAgain, false, 0, true);

function videoPlayAgain (event:Event):void

event.target.seek(0);
event.target.play();



FLV Component is sometimes glitchy with some re-plays (example: after some loops the video freezes, then plays normal after a few seconds). I think it depends on encoder settings when outputting the video file.



You might prefer using the NetStream and Video API to handle video playback.


NetStream


Video


//# setup Video object and related Net Stream/Connection objects
var vidloop:Video = new Video(); addChild(vidloop);
vidloop.width = stage.stageWidth; vidloop.height = stage.stageHeight;

var vidNC:NetConnection = new NetConnection(); vidNC.connect(null);
var vidNS:NetStream = new NetStream(vidNC); vidloop.attachNetStream(vidNS);

//# metadata handler to correctly display video
var metaListener :Object = new Object(); metaListener = onMetaData: process_Metadata ;
vidNS.client = metaListener;

//# play video file
vidNS.addEventListener(NetStatusEvent.NET_STATUS, videoStatusHandler); //checks for ending
vidNS.play("Content/OTG_MainLoop_1080p.mp4");


function videoStatusHandler (event:NetStatusEvent):void

if (event.info.code == "NetStream.Buffer.Empty") //buffer is empty when video ends...

//# ...so rewind and play again.
vidNS.seek(0); vidNS.resume();




function process_Metadata (in_Data :Object):void

//check metadata like width, height, duration etc
//trace("duration is : " + in_Data.duration );





Thankyou so very much, First one worked, but I am going to take both bits of code with me. Thanks for explaining what I was doing wrong also, I really appreciate it so much :)
– Mark R
Aug 10 at 12:41





Glad to hear it's solved now. While NetStream might seem chunky it is more powerful (inc: webcam, live streams, etc). Only tedious issue is to create own user interface but that's easy.
– VC.One
Aug 10 at 13:44






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard