控制加载的swf动画暂停和播放
外部加载进来的swf动画(无论是as2.0的还是as3.0的)可以控制暂停和播放,声音也能暂停和播放,但是注意的一点,导入的声音必须是“数据流”。请看以下代码:
Main.as:
package
{
import flash.display.Sprite;
import flash.events.MouseEvent;
import flash.text.TextField;
/**
*@author hypo.chen
*@E-mail 645338868@qq.com
*@data 2015-9-6
*/
[SWF(frameRate="60",width="1024",height="768",backgroundColor="0x669966")]
public class Main extends Sprite
{
private var _loadSwf:LoadSwfHandler;
private var _playBtn:TextField;
private var _stopBtn:TextField;
public function Main()
{
_loadSwf = new LoadSwfHandler();
this.addChild(_loadSwf);
_loadSwf.loadSwf("assest/testAnimation.swf");
this.addChild(setText("暂停",5,700,stop));
this.addChild(setText("播放",50,700,go));
}
private function go(e:MouseEvent):void
{
_loadSwf.targetPlay();
}
private function stop(e:MouseEvent):void
{
_loadSwf.targetPause();
}
private function setText(_text:String,_x:Number = 0,_y:Number = 0,_fun:Function = null):TextField
{
var _textField:TextField = new TextField();
_textField.text = _text;
_textField.selectable = false;
_textField.autoSize = "left";
_textField.x = _x;
_textField.y = _y;
_textField.addEventListener(MouseEvent.CLICK,_fun);
return _textField;
}
}
}
LoadSwfHandler.as:
package
{
import com.cndragon.api.api.utils.ForcibleLoader;
import flash.display.AVM1Movie;
import flash.display.Loader;
import flash.display.MovieClip;
import flash.display.Shape;
import flash.events.Event;
import flash.net.URLRequest;
/**
*@author hypo.chen
*@E-mail 645338868@qq.com
*@data 2015-9-6
*/
public class LoadSwfHandler extends MovieClip
{
private var _mask:Shape;
private var _stageW:Number;
private var _stageH:Number;
private var _loader:Loader;
private var _forcibleLoader:ForcibleLoader;
private var _targetMC:MovieClip;
private var _url:String;
private var _urlR:URLRequest;
public function LoadSwfHandler()
{
_mask = new Shape();
_mask.graphics.beginFill(0xffffff,1);
_mask.graphics.drawRect(0,0,10,10);
_mask.graphics.endFill();
this.mask = _mask;
}
public function loadSwf(_url:String):void
{
//加载swf
this._url = _url;
if(!_loader)
_loader = new Loader();
_urlR = new URLRequest(_url);
_loader.contentLoaderInfo.addEventListener(Event.COMPLETE,onSwfLoaded);
_loader.load(_urlR);
}
private function onSwfLoaded(e:Event):void
{
if(_loader.content is AVM1Movie)////如果是as2.0或者1.0代码生成的swf那么需要通过ForcibleLoader进行处理
{
trace("_loader.content is AVM1Movie");
_loader.unloadAndStop();
_forcibleLoader = new ForcibleLoader(this._loader);
_forcibleLoader.load(this._urlR);
return;//这个return一定要加上,否则加载as2.0的或报错
}
_loader.contentLoaderInfo.removeEventListener(Event.COMPLETE,onSwfLoaded);
trace(_loader.content,_loader,"_loader");
try
{
trace("try")
_targetMC = MovieClip(_loader.content);
this._stageW = _loader.contentLoaderInfo.width;
this._stageH = _loader.contentLoaderInfo.height;
this.addChild(_targetMC);
trace(_targetMC.totalFrames);
}
catch(error:Error)
{
trace("error---------:" + error);
this._stageW = _loader.width;
this._stageH = _loader.height;
this.addChild(_loader);
}
_mask.width = this._stageW;
_mask.height = this._stageH;
this.addChild(_mask);
}
/**
* 卸装加载的swf
*/
public function unloadAndStop():void
{
if(_loader)
{
_loader.unloadAndStop();
_loader = null;
}
if(_targetMC)
{
_targetMC.parent.removeChild(_targetMC);
_targetMC = null;
}
}
public function targetPause():void
{
_targetMC.stop();
}
public function targetPlay():void
{
_targetMC.play();
}
}
}
还有一个ForcibleLoader.as 类可以从网上下载。

浙公网安备 33010602011771号