jeudi 23 mars 2017

ActionScript 3.0 Switching Video by pressing a key

I'm writing a simple program that switch from a video to another when I press two different key. If I press the key F it will be displayed the video1 if I press the key 2 it will be displayed the video 2. While the video1 or the video2 are displayed if I press the key F or 2 I switch between the two video. I have a problem. If I press quickly 2 and F the fps start to drop and after some quick press of 2 and F the .swf crash. I think that it's a problem of my code cause it'isnt optimized. The base idea is that everytime that the user press F or 2 key while a video is displayed I close the stream of that video and I start to stream the other video and so on. This is the code:

package {
import flash.display.MovieClip;
import flash.net.NetConnection;
import flash.net.NetStream;
import flash.events.AsyncErrorEvent;
import flash.media.Video;
import flash.events.MouseEvent;
import flash.ui.Keyboard;
import flash.events.KeyboardEvent;
import flash.events.Event;
import flash.utils.getTimer;


public class MainDisplayDifferentVideoOnCLick extends MovieClip {

    public function MainDisplayDifferentVideoOnCLick() {
        keyPressed();
        fpsNumber();

    }
    public function keyPressed(): void {
        stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
        function onKeyDown(myKey: KeyboardEvent): void {
            if (myKey.keyCode == 70) {
                playVideo1();
            }
            if (myKey.keyCode == 50) {
                playVideo2();
            }
        }
    }

    function playVideo1(): void {
        var networkConnection: NetConnection = new NetConnection();
        networkConnection.connect(null);
        var networkStream: NetStream = new NetStream(networkConnection);
        networkStream.addEventListener(AsyncErrorEvent.ASYNC_ERROR, errorHandler);
        networkStream.play("Damiani.mp4");
        function errorHandler(error1: AsyncErrorEvent): void {
            // ingoro l' evento
        }
        var myVideo1: Video = new Video();
        myVideo1.attachNetStream(networkStream);
        myVideo1.width = 1920;
        myVideo1.height = 1080;
        addChild(myVideo1);
        stage.addEventListener(KeyboardEvent.KEY_DOWN, on2Down);
        function on2Down(ev: KeyboardEvent): void {
            if (ev.keyCode == 50) {
                networkStream.close();
            }
        }

    }
    function playVideo2(): void {
        var networkConnection: NetConnection = new NetConnection();
        networkConnection.connect(null);
        var networkStream: NetStream = new NetStream(networkConnection);
        networkStream.addEventListener(AsyncErrorEvent.ASYNC_ERROR, errorHandler);
        networkStream.play("Sfelab.mp4");
        function errorHandler(error1: AsyncErrorEvent): void {
            // ingoro l' evento
        }
        var myVideo2: Video = new Video();
        myVideo2.attachNetStream(networkStream);
        myVideo2.width = 1920;
        myVideo2.height = 1080;
        addChild(myVideo2);

        stage.addEventListener(KeyboardEvent.KEY_DOWN, onFDown);
        function onFDown(ev: KeyboardEvent): void {
            if (ev.keyCode == 70) {
                networkStream.close();
            }
        }
    }
    public function fpsNumber(): void {
        var frames: int = 0;
        var prevTimer: Number = 0;
        var curTimer: Number = 0;

        this.addEventListener(Event.ENTER_FRAME, performFrameTest);

        function performFrameTest(e: Event): void {
            frames += 1;
            curTimer = getTimer();
            if (curTimer - prevTimer >= 1000) {
                trace("FPS: ");
                trace(Math.round(frames * 1000 / (curTimer - prevTimer)));
                prevTimer = curTimer;
                frames = 0;
            }
        }
    }
}

}

Do you have any advice? Thank you!

Aucun commentaire:

Enregistrer un commentaire