Using present time of video to manage all video in Action Script 3.0 - actionscript-3

I would like to use present time of video to manage all video. For example, I have some cue points, I choose it and video is now playing from this cue point, and after 10 sec video goes to other part and everything depends on present time of video. I do not use it on web so my video isn't loading (I mean that property of VideoProgressEvent like bytesLoaded will not help me). Is it possible to do it in action script 3.0 ? Another question is if I can add some transitions between cue points.
import fl.video.*;
// Video component instance name
var flvControl:FLVPlayback = display;
var flvSource:String = "myMovie.flv";
// Set video
flvControl.source = flvSource;
var myTextFormat:TextFormat = new TextFormat();
myTextFormat.size = 20
btn1.setStyle("textFormat", myTextFormat);
btn2.setStyle("textFormat", myTextFormat);
btn3.setStyle("textFormat", myTextFormat);
btn4.setStyle("textFormat", myTextFormat);
display.autoPlay = false;
// Add seek to time code
function seekToTimeHandler1(event:MouseEvent):void
{
var sec:Number = 15;
flvControl.seek(sec);
}
btn1.addEventListener(MouseEvent.CLICK, seekToTimeHandler1);
// Add seek to time code
function seekToTimeHandler2(event:MouseEvent):void
{
var sec:Number = 61;
flvControl.seek(sec);
}
btn2.addEventListener(MouseEvent.CLICK, seekToTimeHandler2);
// Add seek to time code
function seekToTimeHandler3(event:MouseEvent):void
{
var sec:Number = 63;
flvControl.seek(sec);
}
btn3.addEventListener(MouseEvent.CLICK, seekToTimeHandler3);
// Add seek to time code
function seekToTimeHandler4(event:MouseEvent):void
{
var sec:Number = 80;
flvControl.seek(sec);
}
btn4.addEventListener(MouseEvent.CLICK, seekToTimeHandler4);

Related

How do I get images to update dynamically with a slider in actionscript 3?

Here is my dilemma. We use image stacks a lot here with alpha channels. I want to call up images in series using a slider. I am getting a concatenated string a pull up an image from my folder. But I can not get the images to load dynamically when I slide the slider bar.
I am using the frame number to change the file name to pull up the next frame. I can get the image to show up if I put the loader outside of the function, but when I put the loader in the function I get error after error saying the file cannot be located.
Here is the code as it currently stands :
var imgLoader = new Loader();
var str1 = String (".jpg");
var sliderValue:uint = mySlider.sliderKnob.x / 3;
addEventListener(Event.ENTER_FRAME, onEnterFrame);
function onEnterFrame(event:Event):void {
sliderValue = mySlider.sliderKnob.x / 3;
status_txt.text = "Slider position is: "+sliderValue;
fileText.text = "Sketch_0"+sliderValue+".jpg";
imgLoader.load( new URLRequest ( "Sketch_0"+sliderValue+".jpg"));
addChild(imgLoader);
setChildIndex(imgLoader, 0);
}
First thing up, you need to load one image without that slider stuff and see if it loads. Verify the paths if not. Relative paths, as you put them, will work relatively to the folder where SWF is (or where the HTML page, containing the SWF, is).
Second, relieve the heavy burden you are trying to put on the Flash Player.
addEventListener(Event.ENTER_FRAME, onFrame);
var nextName:String;
function onFrame(e:Event):void
{
var anIndex:int = mySlider.sliderKnob.x / 3;
var aName:String = "Sketch_0" + anIndex + ".jpg";
status_txt.text = "Slider position is: " + anIndex;
fileText.text = aName;
// Flash Player does not load images instantly,
// so lets request new image only if user stopped moving the slider.
if (aName == nextName)
{
loadNext(nextName);
}
else
{
nextName = aName;
}
}
var currentName:String;
var currentImage:Loader;
function loadNext(value:String):void
{
// If the requested file is the same as current, just ignore the request.
if (value == currentName) return;
currentName = value;
// Dispose of the previous image in a proper way.
if (currentImage)
{
removeChild(currentImage);
currentImage.unloadAndStop(true);
currentImage = null;
}
// Create a new one.
var aRequest:URLRequest = new URLRequest(currentName);
currentImage = new Loader();
currentImage.load(aRequest);
addChild(currentImage);
}

Play two videos simultaneously with sync in AS3

I am busy with a project where two videos have to play on top of each other.
Like the one in the following site, http://kindnessday.sg/home#/kindness/home
Because of the large filesize I cannot embed the videos in the swf. It will become too large. Therefore the video's must be loaded thro' webstream link. But First video plays before the second video and causing the videos unsynced.
Is there a way to play both video at the same time?
var stream = 'http://mydomain/clip1.mp4'
var stream1 = 'https://mydomain/clip2.mp4'
var http_stream:URLStream = null
var http_stream1:URLStream = null
var video:Video = new Video() // the video is used only to ensure that stream is playing
var video1:Video = new Video() // the video is used only to ensure that stream is playing
addChild(video1)
addChild(video)
var nc:NetConnection = new NetConnection()
nc.connect(null)
var ns:NetStream = new NetStream(nc)
ns.client = new Object()
ns.client.onMetaData = function(e){}
ns.play(null)
var ns1:NetStream = new NetStream(nc)
ns1.client = new Object()
ns1.client.onMetaData = function(e){}
ns1.play(null)
video.attachNetStream(ns)
video1.attachNetStream(ns1)
http_stream = new URLStream();
http_stream.addEventListener(ProgressEvent.PROGRESS, on_Progress)
http_stream.load(new URLRequest(stream))
http_stream1 = new URLStream();
//http_stream1.addEventListener(ProgressEvent.PROGRESS, on_Progress1)
http_stream1.load(new URLRequest(stream1))
function on_Progress(e:ProgressEvent):void {
var b:ByteArray = new ByteArray()
http_stream.readBytes(b, 0, http_stream.bytesAvailable)
ns.appendBytes(b)
var b1:ByteArray = new ByteArray()
http_stream1.readBytes(b1, 0, http_stream1.bytesAvailable)
ns1.appendBytes(b1)
if (ns.appendBytes(b)==ns1.appendBytes(b1)){
ns.play(stream);
ns1.play(stream1);
}
}
why not not check how much of each video has loaded and when you have enough amount (you decide) then you can run some function to play both netstreams...
var min_amount : int = 500000; //for 500 kb minimum
var check_if_ready : Boolean = true;
Also on progressEvent function of loading (URLloader or URLstream???)..
if ( check_if_ready == true )
{
if( myA_NetStream.bytesLoaded >= min_amount && myB_NetStream.bytesLoaded >= min_amount )
{
myA_NetStream.play(); myB_NetStream.play();
//reset since not needed on further progressEvents during this load
check_if_ready = false;
}
}
}
This should only play when both videos have loaded x-amount of bytes. Therefore you can be sure none starts before another)
For better synchronization control you should get into handling bytes in Flash/AS3 and using the AppendBytes method because then you can control playback on a frame-by-frame basis for both NetStreams.

Text stops showing unless not playing

So i finally got my flash application to work and have it running on a website but after a while the dynamic text stops showing up in the fields. The file is set to loop every 5 seconds and it is supposed to update to show the staff on air and what is currently playing. Link to the website is http://mischieffm.com/
var xmlData:XML = new XML();
var header:URLRequestHeader = new URLRequestHeader("pragma", "no-cache");
var theURL_ur:URLRequest = new URLRequest ("/stream/shout.xml?rnd=" + Math.random());
var loader_ul:URLLoader = new URLLoader(theURL_ur);
loader_ul.addEventListener("complete", fileLoaded);
function fileLoaded(e:Event):void
{
xmlData = XML(loader_ul.data);
show_txt.text = xmlData.SERVERTITLE;
song_txt.text = xmlData.SONGTITLE;
}
This is all done in cs6 flash pro and actionscript 3
Try this:
function fileLoaded(e:Event):void
{
loader_ul.removeEventListener("complete", fileLoaded);
xmlData = new XML(loader_ul.data);
if (xmlData.SONGTITLE && xmlData.SERVERTITLE)
{
show_txt.text = xmlData.SERVERTITLE;
song_txt.text = xmlData.SONGTITLE;
}
else
{
show_txt.text = "Untitled artist";
song_txt.text = "Untitled song";
}
}
Note: In order to prevent memory leaks, always call removeEventListener("someEvent", someMethod); after your event gets dispatched. There are some exclusions, but generally you should do it all the time.

FLVPlayback component and Actionscript. Random Flv playback with Arrays.

I have come a little closer with my project, and still need some help please. I have 5 Flvs which i want to play randomly on this page www.music-puppets.com/
The .fla file I have created contains this code:
var files:Array = [ "Sz01Puppet.flv", "Sz02Puppet.flv", "Sz03Puppet.flv", "Sz04Puppet.flv", "Sz05Puppet.flv" ];
var shuffledFiles:Array = shuffleArray(files);
//quick test
var testTimer:Timer = new Timer(1000);
testTimer.addEventListener(TimerEvent.TIMER,updateFile);
testTimer.start();
function updateFile(event:TimerEvent):void{
if(shuffledFiles.length == 0) shuffledFiles = shuffleArray(files); //all files played, repeat process
trace('play file',shuffledFiles[0]);
shuffledFiles.shift();
}
function shuffleArray(source:Array,clone:Boolean = true):Array {
var output:Array = [];
var input:Array = clone ? [].concat(source) : source; //clone ? preserve orignal items by making a copy for shuffling, or not
while(input.length) output.push(input.splice(int(Math.random() * input.length-1),1) [0]);
return output;
}
This script works. In the Output every flv is listed randomly, and then repeated. Next up i want this AS Script to work with an FLV Component.
But how to I get that to work?
In my library I have the 5 flvs, and flvplayback component.
I dragged the FLVPlayback component to the stage, but I can only add one flv in the source. How do I get my working actionscript to work with the FLVPlayback component.
Here you can see how my screen looks like.
capture01.jpg
capture02.jpg
Would be great to get some feedback :)
First of all you need to give your FLVPlayback component an instance name using the properties panel. That will let you talk to it from the code.
Next you will need to add a listener to the FLVPlayback component to detect when each video finishes. Let's assume you've given it an instance name of myVideo:
myVideo.addEventListener(VideoEvent.COMPLETE,onVideoComplete);
You will need a handler function for this, and that will look similar to the function you currently use for each iteration of your timer:
function onVideoComplete(event:VideoEvent):void {
if(shuffledFiles.length == 0) shuffledFiles = shuffleArray(files); //all files played, repeat process
myVideo.source = shuffledFiles.shift(); //grab the first array item to play in the FLVPlayback component
}
Finally, make sure that your FLVPlayback component is set to play a new source automatically, then assign the first video to get things going:
myVideo.autoPlay = true;
myVideo.source = shuffledFiles.shift();
If anyone is interested. I have got the running code :)
import flash.events.Event;
import fl.video.*;
var files:Array;
var shuffledFiles:Array;
loaderInfo.addEventListener(Event.COMPLETE,ready);
function ready(event:Event):void{
loaderInfo.removeEventListener(Event.COMPLETE,ready);
//swf rescale setup
stage.align = StageAlign.TOP_LEFT;
stage.scaleMode = StageScaleMode.NO_SCALE;
stage.addEventListener(Event.RESIZE,stageResized);
//get FlashVars - a string converted into an Array by spliting it on the , character
//if the files FlashVar is setup correctly use the data, else use default values
if(loaderInfo.parameters.files != undefined) files = loaderInfo.parameters.files.indexOf(',') > 0 ? loaderInfo.parameters.files.split(",") : [loaderInfo.parameters.files];
else files = [ "Sz01Puppet.flv", "Sz02Puppet.flv", "Sz03Puppet.flv", "Sz04Puppet.flv", "Sz05Puppet.flv" ];
shuffledFiles = shuffleArray(files);
//play the 1st video
videoPlayer.source = shuffledFiles[0];
shuffledFiles.shift();
//see when the video finished playing
videoPlayer.addEventListener(VideoEvent.COMPLETE,videoFinished);
}
function videoFinished(event:VideoEvent):void{
if(shuffledFiles.length == 0) shuffledFiles = shuffleArray(files);//all files played, repeat process
videoPlayer.source = shuffledFiles[0];//play the first video in the random list
videoPlayer.play();
trace('playing',shuffledFiles[0]);
shuffledFiles.shift();//remove the first video from the random list (e.g. [2,0,1].shift() becomes [0,1])
}
function stageResized(event:Event):void{
videoPlayer.width = stage.stageWidth;
videoPlayer.height = stage.stageHeight;
}
function shuffleArray(source:Array,clone:Boolean = true):Array {
var output:Array = [];
var input:Array = clone ? [].concat(source) : source;//clone ? preserve orignal items by making a copy for shuffling, or not
while(input.length) output.push(input.splice(int(Math.random() * input.length-1),1)[0]);
return output;
}

Stream an FLVPlayback through the internet

I have a video in flash in the size of 19 MB (5 minutes) and I want the user to see what that has been loaded so far, or even get an indication of what has been loaded - so he won't be stuck in a blank screen until the video loads.
The quality of the video is important so I won't resize it - but how can I:
stream it so the user can see what that has been loaded so far
give him an indication of how long he will need to wait until it loads.
My code looks something like this:
import fl.video.*;
var video = new FLVPlayback();
video.fullScreenTakeOver = false;
video.source = "MansfredLoop.f4v";
stage.addChild(video);
Where do I start?
import fl.video.*;
var totalBytes:int;
var loadedBytes:int;
var remainingBytes:int;
var myTimer:Timer = new Timer(100);
var video = new FLVPlayback();
video.fullScreenTakeOver = false;
video.source = "MansfredLoop.f4v";
stage.addChild(video);
myTimer.addEventListener("timer", timerHandler);
myTimer.start();
totalBytes = video.bytesLoaded;
function timerHandler(event:TimerEvent):void {
loadedBytes = video.bytesLoaded;
remainingBytes = totalBytes - loadedBytes;
}