ActionScript 3.0 按键切换视频
ActionScript 3.0 Switching Video by pressing a key
我正在编写一个简单的程序,当我按下两个不同的键时,它会从一个视频切换到另一个视频。如果我按下键 F 它将显示视频 1 如果我按下键 2 它将显示视频 2。如果我按下键 F 或 2 显示视频 1 或视频 2,我会在两个视频之间切换。我有个问题。如果我快速按下 2 和 F,fps 开始下降,并且在快速按下 2 和 F 之后,.swf 崩溃。我认为这是我的代码的问题,因为它没有优化。基本想法是,每次用户在显示视频时按 F 或 2 键,我都会关闭该视频流,然后开始流式传输另一个视频,依此类推。这是代码:
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;
}
}
}
}
}
你有什么建议吗?谢谢!
如果你想让两件事在你的程序中做同样的事情。不要只是复制粘贴适用于单一事物的代码。例如,您可以对任意数量的视频使用单个视频实例。
不清楚您是真的想关闭视频,然后在选择回来后从头开始,还是只是在播放之间快速切换。下面的代码做第二件事,但我希望它清楚如何修改它。
当然,如果您希望拥有返回流的功能,则无需在选择另一个流后立即停止流。
package adnss.projects.tchqs
{
import flash.display.MovieClip;
import flash.events.KeyboardEvent;
import flash.media.Video;
import flash.ui.Keyboard;
public class Main extends MovieClip {
/**List of all video streams that suppose to played.**/
private var vids:Vector.<VideoPlayback> = Vector.<VideoPlayback>(
[new VideoPlayback("AlexD.mp4", Keyboard.F),
new VideoPlayback("Nike AIR.mp4", Keyboard.NUMBER_2)]);
/**Current video playback. Should be one of "vids" items.**/
private var currVid:VideoPlayback;
/**Common Video view used to display stream on stage.**/
private var _video:Video = new Video();
public function Main() {
prepareVideoView(1680, 1050);
stage.addEventListener(KeyboardEvent.KEY_DOWN, onKDown);
}
/**Hangles switching between videos accordingly to key that was pressed.**/
private function onKDown(e:KeyboardEvent): void {
for each (var vp:VideoPlayback in vids) {
if (vp.keyCode != e.keyCode) continue; //not our key
if (vp == currVid) return; //the video of hited key is already playing - not need to change.
if (currVid) currVid.stream.pause(); //If you stat fist video by default, you can remove if(...)
if (!vp.stream) vp.openStream(); //open stream if not oppened already.
_video.attachNetStream(vp.stream); //change stream of our video view.
vp.stream.resume(); //I hope it's clear what "vp" is...
currVid = vp; return;
}
}
/**
* Setup _video:Video view and adds it to stage.
* @param w width of video view.
* @param h height of video view.
*/
private function prepareVideoView(w:Number, h:Number) {
_video.width = w;
_video.height = h;
stage.addChild(_video);
}
}
}
import flash.events.AsyncErrorEvent;
import flash.net.NetConnection;
import flash.net.NetStream;
/**
* This class define particular video playback - bind video address to shortcut key,
* and opens unique stream for it if necessary.
*/
class VideoPlayback {
private var _adrs:String;
private var _kCode:uint;
private var _s:NetStream;
public function VideoPlayback(adress:String, keyCode:uint) {
_adrs = adress;
_kCode = keyCode;
}
/**Open NetStream for this playback accordingly to video adress passed in constructor.
* Until this method is called "stream" property is not available.**/
public function openStream() {
var c: NetConnection = new NetConnection();
c.connect(null);
_s = new NetStream(c);
_s.addEventListener(AsyncErrorEvent.ASYNC_ERROR, errorHandler);
_s.play(_adrs);
function errorHandler(error1: AsyncErrorEvent): void {
// ingoro l' evento
}
}
public function get adress():String{return _adrs;}
public function get keyCode():uint{return _kCode;}
public function get stream():NetStream{return _s;}
}
请注意 VideoPlayback
class 在包外。它可以在一个文件中正常工作,但您可能希望将其移动到单独的文件中,以便让事情更有条理。
我正在编写一个简单的程序,当我按下两个不同的键时,它会从一个视频切换到另一个视频。如果我按下键 F 它将显示视频 1 如果我按下键 2 它将显示视频 2。如果我按下键 F 或 2 显示视频 1 或视频 2,我会在两个视频之间切换。我有个问题。如果我快速按下 2 和 F,fps 开始下降,并且在快速按下 2 和 F 之后,.swf 崩溃。我认为这是我的代码的问题,因为它没有优化。基本想法是,每次用户在显示视频时按 F 或 2 键,我都会关闭该视频流,然后开始流式传输另一个视频,依此类推。这是代码:
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;
}
}
}
}
}
你有什么建议吗?谢谢!
如果你想让两件事在你的程序中做同样的事情。不要只是复制粘贴适用于单一事物的代码。例如,您可以对任意数量的视频使用单个视频实例。
不清楚您是真的想关闭视频,然后在选择回来后从头开始,还是只是在播放之间快速切换。下面的代码做第二件事,但我希望它清楚如何修改它。 当然,如果您希望拥有返回流的功能,则无需在选择另一个流后立即停止流。
package adnss.projects.tchqs
{
import flash.display.MovieClip;
import flash.events.KeyboardEvent;
import flash.media.Video;
import flash.ui.Keyboard;
public class Main extends MovieClip {
/**List of all video streams that suppose to played.**/
private var vids:Vector.<VideoPlayback> = Vector.<VideoPlayback>(
[new VideoPlayback("AlexD.mp4", Keyboard.F),
new VideoPlayback("Nike AIR.mp4", Keyboard.NUMBER_2)]);
/**Current video playback. Should be one of "vids" items.**/
private var currVid:VideoPlayback;
/**Common Video view used to display stream on stage.**/
private var _video:Video = new Video();
public function Main() {
prepareVideoView(1680, 1050);
stage.addEventListener(KeyboardEvent.KEY_DOWN, onKDown);
}
/**Hangles switching between videos accordingly to key that was pressed.**/
private function onKDown(e:KeyboardEvent): void {
for each (var vp:VideoPlayback in vids) {
if (vp.keyCode != e.keyCode) continue; //not our key
if (vp == currVid) return; //the video of hited key is already playing - not need to change.
if (currVid) currVid.stream.pause(); //If you stat fist video by default, you can remove if(...)
if (!vp.stream) vp.openStream(); //open stream if not oppened already.
_video.attachNetStream(vp.stream); //change stream of our video view.
vp.stream.resume(); //I hope it's clear what "vp" is...
currVid = vp; return;
}
}
/**
* Setup _video:Video view and adds it to stage.
* @param w width of video view.
* @param h height of video view.
*/
private function prepareVideoView(w:Number, h:Number) {
_video.width = w;
_video.height = h;
stage.addChild(_video);
}
}
}
import flash.events.AsyncErrorEvent;
import flash.net.NetConnection;
import flash.net.NetStream;
/**
* This class define particular video playback - bind video address to shortcut key,
* and opens unique stream for it if necessary.
*/
class VideoPlayback {
private var _adrs:String;
private var _kCode:uint;
private var _s:NetStream;
public function VideoPlayback(adress:String, keyCode:uint) {
_adrs = adress;
_kCode = keyCode;
}
/**Open NetStream for this playback accordingly to video adress passed in constructor.
* Until this method is called "stream" property is not available.**/
public function openStream() {
var c: NetConnection = new NetConnection();
c.connect(null);
_s = new NetStream(c);
_s.addEventListener(AsyncErrorEvent.ASYNC_ERROR, errorHandler);
_s.play(_adrs);
function errorHandler(error1: AsyncErrorEvent): void {
// ingoro l' evento
}
}
public function get adress():String{return _adrs;}
public function get keyCode():uint{return _kCode;}
public function get stream():NetStream{return _s;}
}
请注意 VideoPlayback
class 在包外。它可以在一个文件中正常工作,但您可能希望将其移动到单独的文件中,以便让事情更有条理。