Skip to main content
h1111
Participant
November 30, 2017
Question

Press keyboard-key, then play randomly 1 of 7 videos.

  • November 30, 2017
  • 2 replies
  • 1041 views

I want a simply function where I press the Enter-Key and then randomly 1 out of 7 imported videos starts playing.

Every video should be invisible first and should go invisible again after playing.

I tried to create it based on information I found, but I have the feeling it is very wrong and there are even simpler ways to create it

What is the best and simpliest Actionscript code to get this effect?

The name of the videos are video1, video2, video3, video4, video5, video6, video7.

Here is just for reference my code (but I guess there is not that much to take out of it):

video1.visible = false;

video2.visible = false;

video3.visible = false;

video4.visible = false;

var SelectMC:Number = 0;

addEventListener(KeyboardEvent.ENTER);

function fl_ClickToHide(event:KeyboardEvent):void

{

    function getMC() :void

    {

        var SelRam:Number = Math.random ();

        SelectMC = Math.round (SelRam*4+1);

        trace(SelectMC);

    }

    getMC();   

}

stage.addEventListener (Event.ENTER_FRAME,EntFrame);

function EntFrame(e:Event):void

{

    }

    if (SelectMC == 1)

    {

    video2.visible = false;

    video3.visible = false;

    video4.visible = false;

    video1.visible = true;

    }

    if (SelectMC == 2)

    {

    video1.visible = false;

    video3.visible = false;

    video4.visible = false;

    video2.visible = true;

    }

    if (SelectMC == 3)

    {

    video1.visible = false;

    video2.visible = false;

    video4.visible = false;

    video3.visible = true;

    }

    if (SelectMC == 4)

    {

    video1.visible = false;

    video2.visible = false;

    video3.visible = false;

    video4.visible = true;

    }

}

This topic has been closed for replies.

2 replies

JoãoCésar17023019
Community Expert
Community Expert
December 6, 2017

If you don't mind, I'd like to suggest an alternative using frames to store the different videos.

All you have to do is to:

- Create a container Movie Clip called videos.

- Inside of this container, put each imported FLVPlayback component on each frame, starting from 2 and leaving the first one empty.

- Then add this code to the main timeline:

import flash.events.KeyboardEvent;

import flash.ui.Keyboard;

import fl.video.VideoEvent;

var videosFrames:Array = getRandomFrames(videos.totalFrames - 1);

var count:uint = 0;

var canChange:Boolean = true;

function keyDownHandler(e:KeyboardEvent):void

{

    if (e.keyCode == Keyboard.ENTER)

        if (canChange)

            changeVideo();

}

function changeVideo():void

{

    var index:uint = 2 + count % (videos.totalFrames - 1);

   

    canChange = false;   

   

    if (index == 2)

    {

        videosFrames.sort(randomSort);

        count = 0;

    }   

   

    videos.gotoAndStop(videosFrames[count]);

    videos["video" + (videos.currentFrame - 1)].addEventListener(fl.video.VideoEvent.COMPLETE, completeHandler);

   

    count++;

}

function completeHandler(e:fl.video.VideoEvent):void

{

    e.currentTarget.removeEventListener(fl.video.VideoEvent.COMPLETE, completeHandler);

    videos.gotoAndStop(1);

    canChange = true;   

}

function getRandomFrames(total:uint):Array

{

    var array:Array = [];

   

    for (var i:uint = 0; i < total; i++)

        array = i + 2;

   

    return array;

}

function randomSort(a:*, b:*):Number

{

    if (Math.random() < 0.5)

        return -1;

    else

        return 1;

}

stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);

FLA/SWF/videos download: here.

I hope it helps.

Regards,

JC

kglad
Community Expert
Community Expert
December 1, 2017

by video, do you mean a movieclip?

of if you really mean a video, how are your displaying it?  in an flvplayback component, added to a timeline, in

h1111
h1111Author
Participant
December 1, 2017

Hello kglad,

yes, they are imported external videos with playback component. Each on a different layer.

(By the way: Instead of the enter-key, the space-bar or a left mouse click would also be working if this would be easier).

kglad
Community Expert
Community Expert
December 1, 2017

don't import the videos to the timeline.

add one flvplayback component to your stage>assign an instance name (eg, flv_pb).

then use:

var videoA:Array=['videos/flv1.flv','videos/vid2.mp4',etc];

and whenever you want to  play a video call videoF:

function videoF(possible event, eg, keyboardevent):void{

flv_pb.source=videoA[Math.floor(videoA.length*Math.random())];  // if you want to prevent repeats use a shuffle function

}