我这里有一些AS3,用来创建一个19按钮的交互亭。它称20种不同的video_files在FLVPLayer中播放。单击一个按钮时,它会绘制播放机并指定源。
发生在我们的电脑上,是我们疯狂的孩子测试,我们点击几个不同的按钮开始和停止视频,并在几个按钮按下SWF崩溃。
我有一些参数错误(根据Adobe的说法),我在调试器中存在一个noSource错误(指向removeChild(movie_container);和其他几个看似随机的bug/错误消息)。
有人介意看一下密码吗。谢谢。注意:因为它大约有500行长,所以我将粘贴到buttonTwo。
import flash.events.*;
import flash.display.*;
import flash.ui.Mouse;
import fl.video.*;
import flash.utils.Timer;
//Mouse.hide();
stop();
addEventListener(Event.ENTER_FRAME, timerHandler);
//===================== Primary Event Listeners ==========================//
buttonOne.addEventListener(MouseEvent.MOUSE_DOWN, playVideoOne);
buttonTwo.addEventListener(MouseEvent.MOUSE_DOWN, playVideoTwo);
// Show buttons so users can click - cheaper than adding/removing 20 e:listeners
function showTheButtons(): void {
buttonOne.visible = true;
buttonTwo.visible = true;
}
// Hide buttons so users cant crazy-click resulting in massive slowdown - cheaper than adding/removing 20 e:listeners
function hideTheButtons(): void {
buttonOne.visible = false;
buttonTwo.visible = false;
}
// ADD ALL EVENT Listeners after AttractLoop removed
//=====================
var attractTimer: Timer = new Timer(300000); //should be 7min OR 420000ms in production
attractTimer.addEventListener(TimerEvent.TIMER, timerHandler, false, 0, true);
attractTimer.start();
//=====================
this.aLoopMovie.visible = false;
aLoopMovie.addEventListener(MouseEvent.CLICK, stopRemoveVideo);
function stopRemoveVideo(event: Event): void {
showTheButtons();
aLoopMovie.visible = false;
aLoopMovie.gotoAndStop(1);
//=====================
attractTimer.start();
//=====================
}
function timerHandler(event: Event): void {
attractTimer.stop();
hideTheButtons();
//++
removeEventListener(Event.ENTER_FRAME, timerHandler);
//++
if (this.aLoopMovie.visible != true) {
this.aLoopMovie.visible = true;
this.aLoopMovie.play();
}
}
//////////// BUILD PLAYER ///////////////
var movie_container: MovieClip = new MovieClip();
function launchVideo(vBox, vFile): void {
hideTheButtons();
var flvPlayer: FLVPlayback = new FLVPlayback();
flvPlayer.source = vFile;
flvPlayer.skinAutoHide = true;
flvPlayer.skinBackgroundColor = 0x000000;
flvPlayer.width = 1920;
flvPlayer.height = 1080;
flvPlayer.autoRewind = true;
vBox.addChild(flvPlayer);
// Allow Playabck timer //
var playbackTimer: Timer = new Timer(5000); //should be 2sec OR 2000ms in production
playbackTimer.addEventListener(TimerEvent.TIMER, allowPlayback);
function allowPlayback(event: Event): void {
playbackTimer.stop();
movie_container.addEventListener(MouseEvent.CLICK, stopRemoveVideo);
function stopRemoveVideo(event: Event): void {
showTheButtons();
flvPlayer.stop();
//=====================
movie_container.removeEventListener(MouseEvent.CLICK, stopRemoveVideo);
//=====================
removeChild(movie_container);
attractTimer.start();
}
flvPlayer.addEventListener(fl.video.VideoEvent.COMPLETE, completeHandler);
function completeHandler(event: fl.video.VideoEvent): void {
flvPlayer.stop();
playbackTimer.stop();
showTheButtons();
flvPlayer.removeEventListener(fl.video.VideoEvent.COMPLETE, completeHandler);
//=====================
removeChild(movie_container);
//=====================
attractTimer.start();
}
}
playbackTimer.start();
//////////////////////////
}
//////////// END BUILD PLAYER ///////////////
//===================== Primary Functions
function playVideoOne(event: Event): void {
//=====================
attractTimer.stop();
hideTheButtons();
//=====================
// Place container on stage
addChild(movie_container);
movie_container.x = 0;
movie_container.y = 0;
//Video Source
var video_file = "MPVideos/MP-01.mp4";
launchVideo(movie_container, video_file);
}
function playVideoTwo(event: Event): void {
//=====================
attractTimer.stop();
hideTheButtons();
//=====================
// Place container on stage
addChild(movie_container);
movie_container.x = 0;
movie_container.y = 0;
//Video Source
var video_file = "MPVideos/MP-02.mp4";
launchVideo(movie_container, video_file);
}更新的
function playVideoOne(event: Event): void {
//=====================
attractTimer.stop();
hideTheButtons();
//=====================
var movie_container: MovieClip = new MovieClip();
// Place container on stage
addChild(movie_container);
movie_container.x = 0;
movie_container.y = 0;
//Video Source
var video_file = "MPVideos/MP-01.mp4";
var flvPlayer: FLVPlayback = new FLVPlayback();
function launchVideo(vBox, vFile): void {
flvPlayer.source = vFile;
flvPlayer.skinAutoHide = true;
flvPlayer.skinBackgroundColor = 0x000000;
flvPlayer.width = 1920;
flvPlayer.height = 1080;
flvPlayer.autoRewind = true;
vBox.addChild(flvPlayer);
}
flvPlayer.addEventListener(fl.video.VideoEvent.COMPLETE, completeHandler);
function completeHandler(event: fl.video.VideoEvent): void {
flvPlayer.stop();
flvPlayer.closeVideoPlayer(0);
showTheButtons();
flvPlayer.removeEventListener(fl.video.VideoEvent.COMPLETE, completeHandler);
//=====================
attractTimer.start();
//=====================
removeChild(movie_container);
//=====================
}
movie_container.addEventListener(MouseEvent.CLICK, stopRemoveVideo);
function stopRemoveVideo(event: Event): void {
flvPlayer.stop();
flvPlayer.closeVideoPlayer(0);
showTheButtons();
removeChild(movie_container);
//=====================
attractTimer.start();
//=====================
movie_container.removeEventListener(MouseEvent.CLICK, stopRemoveVideo);
//=====================
}
launchVideo(movie_container, video_file);
}将嵌套函数移到一个级别上。这些事件侦听器(Complete & CLICK)不在playVideoOne函数之外工作。
发布于 2015-06-08 06:56:32
您的主要错误是在函数中声明一个变量,以便在嵌套函数中使用它。这些不像你想的那样有效,实际上我怀疑这不是一种被描述的未定义的行为。还有,你有很多功能是这样设计的。相反,您应该做的是:让flvPlayer成为顶层的变量,在事件侦听器中初始化,并使用相应的事件侦听器清除播放机,并将function launchVideo(vBox, vFile)重新定位到顶层--在每个侦听器中都完全相同。此外,您应该将所有侦听器的公共部分封装到一个参数化函数中,该函数将创建movie_container并使用提供的字符串启动正确的视频。
//===================== Additional variables
var flvPlayer:FLVPlayback;
var movie_container:MovieClip;
//===================== Primary Functions
function playVideoByString(source:String):void {
attractTimer.stop();
hideTheButtons();
movie_container = new MovieClip();
addChild(movie_container);
movie_container.x = 0;
movie_container.y = 0;
launchVideo(movie_container, source); // that's it
}
function playVideoOne(event: Event): void {
playVideoByString("MPVideos/MP-01.mp4"); // that's it as well
}现在,您不能正确地删除FLVPlayback实例,它们有侦听器,所以是它们阻塞了您的内存。即使是泄露一个Object也是有害的,而且你正在泄漏一个完整的视频播放器。难怪你这么快就没记忆了。因此,如果按下了新的按钮,或者播放器完成播放,您就必须正确地停止并分离视频播放器。根据您对这个问题的更新,您已经设计了一些函数来完成这个任务,为了使它们正常工作,您只需要全局级别的变量。但是,您的错误是在初始化时添加两个侦听器,但只在两个删除函数中删除一个--另一个侦听器保留下来,防止垃圾收集movie_container或flvPlayer,从而有效地泄漏整个视频播放器实例。
// Primary functions (cont.)
function stopRemoveVideo(event: Event): void {
doCleanup();
}
function completeHandler(event: fl.video.VideoEvent): void {
doCleanup();
}
function doCleanup():void {
// these actions are common between handlers, so put em in one place
flvPlayer.stop();
flvPlayer.closeVideoPlayer(0);
showTheButtons();
removeChild(movie_container);
attractTimer.start();
movie_container.removeEventListener(MouseEvent.CLICK, stopRemoveVideo);
flvPlayer.removeEventListener(fl.video.VideoEvent.COMPLETE, completeHandler);
// clear the references just in case
movie_container=null;
flvPlayer=null;
// you need to remove both listeners in either case, otherwise you leak objects!
}
function launchVideo(vBox, vFile): void {
flvPlayer=new FLVPlayback();
flvPlayer.source = vFile;
flvPlayer.skinAutoHide = true;
flvPlayer.skinBackgroundColor = 0x000000;
flvPlayer.width = 1920;
flvPlayer.height = 1080;
flvPlayer.autoRewind = false; // this changes as well
// you close the vid if you hit movie end, why allow the player to autorewind?
vBox.addChild(flvPlayer);
// adding listeners in here
flvPlayer.addEventListener(fl.video.VideoEvent.COMPLETE, completeHandler);
movie_container.addEventListener(MouseEvent.CLICK, stopRemoveVideo);
}这可能不足以消除源代码中的所有错误,但它应该为如何优化应用程序和函数提供一个良好的开端,这些应用程序和函数使用在其中一个中实例化的对象。
https://stackoverflow.com/questions/30680625
复制相似问题