声明:欢迎任何人和组织转载本blog中文章,但必须标记文章原始链接和作者信息。
本文链接:http://blog.csdn.net/li_007/archive/2009/03/30/4037090.aspx
开拓进取的小乌龟------->CSDN点滴点点滴滴Blog
今天加载了一个有声音的swf,然后在没播放完成的时候,又加载了另一个swf,同样是有声音的,结果发现奇怪的事情出现了,先前的swf视频是销毁,unload不见了,可是声音还是在播放的,结果发现内存也没有被释放,过了好久才释放(我的代码中如果播放完成或者什么的都是有销毁的过程的,不过有点对Actionscript 3的垃圾回收机制搞不懂了,现在还在思考中。。。。),还害的PM等了我好长时间。
首先来说说声音的问题吧,其实以前是没注意,今天才发现,后来查查Adobe Docs后发现也很简单,先看看Adobe Docs中flash.media.sound class的描述截图:
特别是最后红色线标记的,告诉我们控制外部加载的swf file,使用SoundMixer Class的属性,OK。看看SoundMixer类后就发现很简单,这个类提供了静态的方法StopAll来停止当前正在播放的声音。也看截图吧:
好了,修改前面文章发的那个类(http://blog.csdn.net/li_007/archive/2009/03/28/4032276.aspx)如下:
// // CLoadMedia.as // // you can load external swf and pictures used this class // // Written by Leezhm, 7th Nov, 2008 // Contact : Leezhm@126.com // Copyright @ Luxoom.cn // Last Modified by Leezhm on 30th March, 2009 // package Srcs { import flash.display.DisplayObjectContainer; import flash.display.Loader; import flash.display.MovieClip; import flash.display.LoaderInfo; import flash.events.Event; import flash.events.MouseEvent; import flash.net.URLRequest; import flash.media.SoundMixer; public class CLoadMedia { private var _file:String = ""; private var _loader:Loader; private var _request:URLRequest; private var _display:DisplayObjectContainer; private var _currentMC:MovieClip; public static const VIDEO_IS_OVER:String = "videoisover"; public function CLoadMedia(str:String, dis:DisplayObjectContainer) { if ("" != str) { this._file = str; } else { trace("the path of extern file is null!!!"); } if (null != dis) { this._display = dis; } } public function Loading():void { if (null == this._request) { this._request = new URLRequest(this._file); } if (null == this._loader) { this._loader = new Loader(); } this._loader.load(this._request); this._loader.contentLoaderInfo.addEventListener(Event.INIT, OnContentInit); this._display.addChild(this._loader); } public function get CurrentMovieClip():MovieClip { if (null != this._currentMC) { return this._currentMC; } else { return null; } } private function OnContentInit(evt:Event = null):void { this._loader.contentLoaderInfo.removeEventListener(Event.INIT, OnContentInit); if (3 == evt.target.actionScriptVersion) { evt.target.addEventListener(Event.COMPLETE, OnLoadCompleted); } else { trace("the actionscript version of swf file is incorrect!!!"); } } private function OnLoadCompleted(evt:Event = null):void { this._display.visible = true; this._loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, OnLoadCompleted); this._currentMC = MovieClip(evt.target.content); evt.target.content.addEventListener(Event.ENTER_FRAME, OnLoaderEnterFrame); this._loader.addEventListener(MouseEvent.CLICK, OnLoaderClick); } private function OnLoaderEnterFrame(evt:Event = null):void { if (null != this._currentMC && MovieClip(evt.target).currentFrame == this._currentMC.totalFrames) { SoundMixer.stopAll(); this._currentMC = null; if (null != this._request) { this._request = null; } if (null != this._loader) { this._loader.removeEventListener(MouseEvent.CLICK, OnLoaderClick); this._loader.dispatchEvent(new Event(CLoadMedia.VIDEO_IS_OVER)); this._display.removeChild(this._loader); this._display = null; this._loader.unload(); this._loader = null; } evt.target.removeEventListener(Event.ENTER_FRAME, OnLoaderEnterFrame); } } private function OnLoaderClick(evt:MouseEvent = null):void { SoundMixer.stopAll(); this._currentMC = null; if (null != this._request) { this._request = null; } if (null != this._loader) { this._loader.removeEventListener(MouseEvent.CLICK, OnLoaderClick); this._loader.dispatchEvent(new Event(CLoadMedia.VIDEO_IS_OVER)); this._display.removeChild(this._loader); this._display = null; this._loader.unload(); this._loader = null; } evt.target.removeEventListener(Event.ENTER_FRAME, OnLoaderEnterFrame); } } }
测试,能跟达到要求,实现控制,OK,大功告成,回家了。。。。。。。
关于那个内存的释放的问题,要慢慢仔细研究后在来同大家分享。