multiple mp4 videos code for looping (as3) - actionscript-3

import fl.video.FLVPlayback;
import fl.video.VideoEvent;
mcV2.autoRewind = true;
mcV2.autoPlay = true;
function Loop(event:fl.video.VideoEvent):void {
mcV2.play();
}
mcV2.addEventListener(fl.video.VideoEvent.AUTO_REWOUND, Loop);
Its a good solution for looping mp4 video files, thx.
How can I use 2 (or more) videos with this code for loop?
etc: mcV1, mcV2, ../..

this will solve your problem:
var myArr = ["mcV1","mcV2","mcV3"]
for(var i=0;i<myArr.length;i++){
var targetBox:FLVPlayback = this.getChildByName("mcV"+(i+1)) as FLVPlayback;
targetBox.addEventListener(fl.video.VideoEvent.AUTO_REWOUND, Loop);
targetBox.autoRewind = true;
targetBox.autoPlay = true;
}
function Loop(event:fl.video.VideoEvent):void {
event.target.play();
}
I added the sample fla as well FLA

Related

How do i play a song from library when the preloader is a "x"% of total loaded?

I am currently undertaking a final exam based on the presentation of an offline portfolio and I need to make a preloader, where when the process of loading is 25% of total bytes, to play a sound that is imported into the library. I've tried several ways and can’t do it.
I'll leave you the code for my preloader.
//(also this code is a mouse loader in text form)
this.loaderInfo.addEventListener(ProgressEvent.PROGRESS,checkingProgress);
function checkingProgress(event:ProgressEvent):void{
var procentLoaded:Number = event.bytesLoaded/event.bytesTotal*100;
ptxt.text=int(procentLoaded)+"%";
if(procentLoaded == 100){
this.gotoAndPlay(1,"Video-Int");
}
}
stage.addChild(ptxt);
ptxt.mouseEnabled = false;
ptxt.addEventListener(Event.ENTER_FRAME, fl_CustomMouseCursor);
function fl_CustomMouseCursor(event:Event) {
ptxt.x = stage.mouseX;
ptxt.y = stage.mouseY;
}
Mouse.hide();
this.addEventListener(Event.ENTER_FRAME, loading);
function loading(e:Event):void {
var total:Number = this.stage.loaderInfo.bytesTotal;
var loaded:Number = this.stage.loaderInfo.bytesLoaded;
prel.scaleX = loaded/total;
if (total == loaded){
play();
this.removeEventListener(Event.ENTER_FRAME, loading);
}
}
You need to make the sound file in your library available for actionscript.
Give it a Class name ex. MySound
...
var mysound:MySound = new MySound();
var soundPLaying:Boolean = false;
function checkingProgress(event:ProgressEvent):void{
var procentLoaded:Number = event.bytesLoaded/event.bytesTotal*100;
ptxt.text= procentLoaded +"%";
if(procentLoaded > 25 && !soundPlaying){ //check if loaded mor then 25% and sound isn't playing
mysound.play();
soundPlaying = true;
}
if(procentLoaded == 100){
this.gotoAndPlay(1,"Video-Int");
}
}
...
if you need to do some extra stuff like pause, change volume etc you will need a SoundChannel Object

AS3 maintime delay script while external swf loads and plays

I'm having an issue with playing an external SWFs inside the main SWF. Everything loads great, except not on cue. I'm using a simple delay actionscript to pause the main timeline until the external SWFs load, but it doesn't always sync up when testing in browsers.
Is there an AS3 code that I can use to pause the main timeline until the external SWF is finish loading and playing?
I need to do this multiple times through the movie, btw...
Below is the delay and the loadmovie array I'm using.
<--------------//Timer//--------------->
var timer4:Timer = new Timer(1500, 1);
timer4.addEventListener(
TimerEvent.TIMER,
function(evt:TimerEvent):void {
play();
}
);
timer4.start();
<--------------//loadMovie//--------------->
function startLoad()
{
var mLoader:Loader = new Loader();
var mRequest:URLRequest = new URLRequest('flip/note_flip.swf');
mLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onCompleteHandler);
mLoader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, onProgressHandler);
mLoader.load(mRequest);
}
function onCompleteHandler(loadEvent:Event)
{
addChild(loadEvent.currentTarget.content);
}
function onProgressHandler(mProgress:ProgressEvent)
{
var percent:Number = mProgress.bytesLoaded/mProgress.bytesTotal;
trace(percent);
}
startLoad();
The load() method of the loader object is asynchronous. It means that the process will continue to execute next lines of code and loading is done independently. That's why you can see it's not on cue.
The simple way to fix your problem is to stop() all the loaded movies upon Event.COMPLETE and play() them once all the movies are loaded.
Use an array to store the loaded movies and address them once all movies are loaded.
And yeah, the timer hack is pointless, you won't know how long it will take the file to load, it can be different every time.
EDIT:
Let's assume that you've got N movie .swf files that makes a complete scene. You would like to load all of them, and once they're all loaded - play them.
Let's build a simple step-by-step logics:
1) First of all, we need a list of the URL pointing to where the external SWFs are.
2) Then we want to start loading the SWFs one by one (and not altogether) to prevent Flash Player bug of not loading some of the content AND keeping the order of movie clips aka z-index, depth, layer order etc.
3) On each loaded movie we count how many are still left to be loaded.
4) Once all movies are loaded, we play() all these movies.
Here's the code for you (written in a frame in case if you're not using classes and not familiar with OOP):
import flash.display.Loader;
import flash.net.URLRequest;
import flash.events.Event;
import flash.display.MovieClip;
var linkArray:Array = ["1.swf", "2.swf", "3.swf"];
var loadedMovieArray:Array = [];
var totalMovies:int = linkArray.length;
var loadedMovies:int = 0;
var urlRequest:URLRequest = new URLRequest();
var l:Loader = new Loader();
l.contentLoaderInfo.addEventListener(Event.COMPLETE, onMovieLoaded);
loadMovies();
function loadMovies():void
{
var movieIndex:int = loadedMovies;
var movieURL:String = linkArray[movieIndex];
urlRequest.url = movieURL;
l.load(urlRequest);
}
function onMovieLoaded(e:Event):void
{
var loadedMC:MovieClip = l.content as MovieClip;
loadedMC.stop();
loadedMovieArray.push( loadedMC );
loadedMovies++;
if (loadedMovies == totalMovies)
onAllMoviesLoaded();
else
loadMovies();
}
function onAllMoviesLoaded():void
{
for (var i:int = 0; i < loadedMovieArray.length; i++) {
var mc:MovieClip = loadedMovieArray[i];
addChild(mc);
mc.play();
}
}

Flash video in movieclip problems

I have several video files playing on the stage. I've converted them into movieclips so that I can scale and drag them by clicking. The problem is that I cannot loop them.
Then I tried to make them as SWF playback object's but after that my code wasn't working with them.
Next step was to make them Embedded video objects so that they loop automatically and the code is working. After that there appeared a problem that the objects are duplicating at some point.
Here's the original code as the videos are movieclips.
var allDraggables:Array = new Array();
var mouseHold = false;
stage.addEventListener(MouseEvent.MOUSE_UP, mUp);
function mUp(MouseEvent)
{
mouseHold = false;
}
function draggableObject(mc)
{
var mouseOnThisObject = false;
allDraggables.push(mc);
mc.addEventListener(Event.ENTER_FRAME, drag);
mc.addEventListener(MouseEvent.MOUSE_DOWN, mDown);
function mDown(MouseEvent)
{
mouseHold = true;
mouseOnThisObject = true;
}
function drag(MouseEvent)
{
if (mouseHold == true && mouseOnThisObject == true)
{
mc.addEventListener(Event.ENTER_FRAME, dragger);
}
if (mouseHold == false)
{
mc.removeEventListener(Event.ENTER_FRAME, dragger);
mouseOnThisObject = false;
}
}
mc.doubleClickEnabled = true;
mc.addEventListener(MouseEvent.DOUBLE_CLICK, scaleMe);
function scaleMe(e:MouseEvent)
{
if (e.target.scaleX < 2)
{
e.target.scaleX= e.target.scaleY = 2;
}
else (e.target.scaleX= e.target.scaleY = 1);
}
function dragger(Event)
{
mc.x+=(mouseX-mc.x)/3;
mc.y+=(mouseY-mc.y)/3;
for (var i:int=0; i<allDraggables.length; i++){
if(mc.hitTestObject(allDraggables[i]) && getChildIndex(allDraggables[i]) > getChildIndex(mc)){
swapChildren(allDraggables[i], mc)
}
}
}
}
draggableObject(green);
draggableObject(red);
draggableObject(video1);
draggableObject(video2);
draggableObject(video3);
Well it's hard to tell what you've tried exactly, since you haven't provided any code (yet)..
However, from the top of my head, I think this will work:
if(videoMC1.currentFrame == 250) { //put the number of the last frame of the movieclip in place of 250
loopMC();
}
function loopMC() {
videoMC1.stop();
videoMC1.gotoAndPlay(1);
}
What you do here is simple; you check the current frame that is passed/playing and when it reaches the desired number (in your case most likely the last frame) it calls a function that resets and plays the video.
I found some old code I once used in a project, maybe you could try this. In my flash app it worked, although I didn't put the video into a movieclip.
import flash.media.Video;
import flash.net.NetConnection;
import flash.net.NetStream;
import flash.events.NetStatusEvent;
var nc:NetConnection = new NetConnection();
nc.connect(null);
ns = new NetStream(nc);
ns.bufferTime = 10;
var vid:Video = new Video(1024,640);
vid.attachNetStream(ns);
addChild(vid);
ns.addEventListener(NetStatusEvent.NET_STATUS, ns_onPlayStatus)
function ns_onPlayStatus(event:NetStatusEvent):void {//loop video
if(event.info.code == "NetStream.Play.Stop") {
ns.seek(0);
}
}

mp3 Looping Problems-Actionscript 3

This is so weird, this post wont let me start the sentence with hello hi or howdy maybe cache issue, anyways, hello, i'm very very new to AS3 so my knowledge is limited, hoping you pros can take it easy on the new guy :).
I'm building a flash site with an animation that loops and i've added an mp3 song as background music, it's also set to loop and auto play. the problem is, that the song is longer than the animation, and when the animation goes back to frame 1, the song overlaps the one that's still playing. I'd like for the song to fully finish before it starts playing again. yeaaah there's probably a heap of code missing, but i got closer to what i want, just need your help to polish it a little.
here is the code so far
import flash.events.MouseEvent;
import flash.media.Sound;
import flash.net.URLRequest;
//var codes
var AutoPlay:Boolean=true;
var isPlaying:Boolean=false;
var pausePosition:Number=0;
var myMusic:Sound=new Sound();
var soundFile:URLRequest=new URLRequest("Fluke.mp3");
myMusic.load(soundFile);
var channel:SoundChannel;
//if commands
if (AutoPlay==true) {
channel=myMusic.play(pausePosition);
isPlaying=true;
}
//pausebutton functions
pause_btn.addEventListener(MouseEvent.CLICK, pauseSound);
function pauseSound(e:MouseEvent):void
{
if (isPlaying == false) {
channel = myMusic.play(pausePosition);
isPlaying =true;
}else{
pausePosition=channel.position;
channel.stop();
isPlaying=false;
}
}
//playbutton functions
play_btn.addEventListener(MouseEvent.CLICK,playMus ic);
function playMusic(event:MouseEvent):void
{ if (isPlaying== false) {
channel=myMusic.play(pausePosition);
isPlaying=true;
}
}
THIS is the full working code thanks to Josha, with an .addEventListener that i added so that the song would loop after its finished playing.
import flash.events.MouseEvent;
import flash.media.Sound;
import flash.net.URLRequest;
//var codes
var AutoPlay:Boolean=true;
var isPlaying:Boolean;
var pausePosition:Number;
var myMusic:Sound;
var soundFile:URLRequest;
var channel:SoundChannel;
// only create music and register event listeners one time
if (myMusic == null)
{
soundFile = new URLRequest("Fluke.mp3");
myMusic = new Sound();
myMusic.load(soundFile);
pausePosition = 0;
if (AutoPlay)
{
channel = myMusic.play(pausePosition);
isPlaying = true;
}
else
{
isPlaying = false;
}
pause_btn.addEventListener(MouseEvent.CLICK, pauseSound);
play_btn.addEventListener(MouseEvent.CLICK, playMusic);
channel.addEventListener(Event.SOUND_COMPLETE, onPlaybackComplete);
}
channel.addEventListener(Event.SOUND_COMPLETE, onPlaybackComplete);
function onPlaybackComplete(event:Event) {
pausePosition = 0;
channel = myMusic.play(pausePosition);
isPlaying = true;
channel.addEventListener(Event.SOUND_COMPLETE, onPlaybackComplete);
}
//playbutton functions
function pauseSound(e:MouseEvent):void
{
if (isPlaying == false) {
channel = myMusic.play(pausePosition);
isPlaying =true;
channel.addEventListener(Event.SOUND_COMPLETE, onPlaybackComplete);
}else{
pausePosition=channel.position;
channel.stop();
isPlaying=false;
}
}
//playbutton functions
function playMusic(event:MouseEvent):void
{
if (isPlaying== false) {
channel=myMusic.play(pausePosition);
isPlaying=true;
channel.addEventListener(Event.SOUND_COMPLETE, onPlaybackComplete);
}
}
I can not place comments yet, so have to use answer.
Are you placing your code in separate AS files; or is this code placed at the first frame of your animation?
If the latter is the case, that indeed you will get overlapping sounds. Since the actionscript code is executed every time the playhead returns to frame 1; creating a new Sound object and starting to play that sound.
A simple solution is to create a key frame at the last frame and add the following action script:
gotoAndPlay(2);
This will loop your animation from frame 2, skipping over the action script.
Another solution that might work (you would have to test it):
import flash.events.MouseEvent;
import flash.media.Sound;
import flash.net.URLRequest;
//var codes
var AutoPlay:Boolean=true;
var isPlaying:Boolean;
var pausePosition:Number;
var myMusic:Sound;
var soundFile:URLRequest;
var channel:SoundChannel;
// only create music and register event listeners one time
if (myMusic == null)
{
soundFile = new URLRequest("Fluke.mp3");
myMusic = new Sound();
myMusic.load(soundFile);
pausePosition = 0;
if (AutoPlay)
{
channel = myMusic.play(pausePosition);
isPlaying = true;
}
else
{
isPlaying = false;
}
pause_btn.addEventListener(MouseEvent.CLICK, pauseSound);
play_btn.addEventListener(MouseEvent.CLICK, playMusic);
}
//playbutton functions
function pauseSound(e:MouseEvent):void
{
if (isPlaying == false) {
channel = myMusic.play(pausePosition);
isPlaying =true;
}else{
pausePosition=channel.position;
channel.stop();
isPlaying=false;
}
}
//playbutton functions
function playMusic(event:MouseEvent):void
{
if (isPlaying== false) {
channel=myMusic.play(pausePosition);
isPlaying=true;
}
}

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;
}