当前位置: 代码迷 >> Web前端 >> as3播发avm1的视频
  详细解决方案

as3播发avm1的视频

热度:77   发布时间:2012-11-14 10:12:18.0
as3播放avm1的视频
/**
* thanks for http://nsdevaraj.wordpress.com/2008/10/16/avm1movie-controller-in-flex/ 
 * and http://troyworks.com/blog/2007/09/17/avm2loader-converting-avm1-to-avm2/
 * 
 * AVM1MovieProxy
 * 
 * modified by sban 2009/2/21
 * http://blog.sban.com.cn/
*/

package sban.flexStudy.avm1to2
{
	import flash.display.Loader;
	import flash.display.MovieClip;
	import flash.errors.EOFError;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.SecurityErrorEvent;
	import flash.net.URLLoader;
	import flash.net.URLLoaderDataFormat;
	import flash.net.URLRequest;
	import flash.utils.ByteArray;
	import flash.utils.Endian;
	
	import mx.core.UIComponent; 
	
	[Event(name="enterFrame", type="flash.events.Event")]
	
	[Event(name="complete", type="flash.events.Event")]
	
	[Event(name="ioError", type="flash.events.IOErrorEvent")]
	
	[Event(name="securityError", type="flash.events.SecurityErrorEvent")]
	
	public class AVM1MvoieProxy extends UIComponent
	{
		public function AVM1MvoieProxy()
		{
			this.addChild(_loader);
		}		
		
		private var _urlLoader : URLLoader;
		private var _loader:Loader = new Loader();
		private var _isReady : Boolean = false;
		private var _movieClip : MovieClip;
			
		public var url : String;
		public var autoPlay : Boolean = false;
		
		public function get currentFrame() : int
		{
			return isReady ? _movieClip.currentFrame : 0;
		}
		
		public function get totalFrames() : int
		{
			return isReady ? _movieClip.totalFrames : 0;
		}
		
		public function get isReady() : Boolean
		{
			return _isReady;
		}
		
		public function nextFrame() : void
		{
			if(isReady) _movieClip.nextFrame();
		}
		
		public function prevFrame() : void
		{
			if(isReady) _movieClip.prevFrame();
		}
		
		public function nextScene() : void
		{
			if(isReady) _movieClip.nextScene();
		}
		
		public function prevScene() : void
		{
			if(isReady) _movieClip.prevScene();
		}
		
		public function stop() : void
		{
			if(isReady) _movieClip.stop();
		}
		
		public function play() : void
		{
			if(isReady) _movieClip.play();
		}
		
		public function gotoAndStop(frame : Object, scene : String = null) : void
		{
			if(isReady) _movieClip.gotoAndStop(frame, scene);
		}
		
		public function gotoAndPlay(frame : Object, scene : String = null) : void
		{
			if(isReady) _movieClip.gotoAndPlay(frame, scene);
		}
		
		override protected function initializationComplete():void
		{
			super.initializationComplete();
			
			if(url) load(url);
		}
		
		public function load(url : String = null):void
		{
			var path : String = url ? url : this.url;
			
			if(!path) return;
			
			_urlLoader = new URLLoader();
			_urlLoader.dataFormat = URLLoaderDataFormat.BINARY;
			
			_urlLoader.addEventListener(Event.COMPLETE, completeHandler);
			_urlLoader.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
			_urlLoader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
			
			_urlLoader.load( new URLRequest(path));
		}		
		
		private function completeHandler(event:Event):void
		{
			event.currentTarget.removeEventListener(Event.COMPLETE, completeHandler);
			
			var inputBytes:ByteArray = ByteArray(_urlLoader.data);
			inputBytes.endian = Endian.LITTLE_ENDIAN;
				
			if (isCompressed(inputBytes)) {
				uncompress(inputBytes);
			}			
			var version:uint = uint(inputBytes[3]); 
			if (version <= 10) { 
				if (version == 8 || version == 9 || version == 10){
					flagSWF9Bit(inputBytes);
				}
				else if (version <= 7) {
					insertFileAttributesTag(inputBytes);
				}
				updateVersion(inputBytes, 9);
			}
			
			_loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadCompleteHandler);
			_loader.loadBytes(inputBytes);
		}
		
		private function loadCompleteHandler(event : Event) : void
		{
			event.currentTarget.removeEventListener(Event.COMPLETE, loadCompleteHandler);
			
			_movieClip = _loader.content as MovieClip;
			_isReady = true;
			dispatchEvent(new Event(Event.COMPLETE));
			
			if(!autoPlay) stop();
			
			_movieClip.addEventListener(Event.ENTER_FRAME, enterFrameHandler);
		}
		
		private function enterFrameHandler(event : Event) : void
		{
			dispatchEvent(event);
		}
		
		/**
		 *  
		 * @param bytes
		 * @return 
		 * 
		 */		
		private function isCompressed(bytes:ByteArray):Boolean
		{
			trace(bytes.toString());
			return bytes[0] == 0x43;
		}		
		
		private function uncompress(bytes:ByteArray):void
		{
			var cBytes:ByteArray = new ByteArray();
			cBytes.writeBytes(bytes, 8);
			bytes.length = 8;
			bytes.position = 8;
			cBytes.uncompress();
			bytes.writeBytes(cBytes);
			bytes[0] = 0x46;
			cBytes.length = 0;
		}		
		
		private function getBodyPosition(bytes:ByteArray):uint
		{
			var result:uint = 0;			
			result += 3; // FWS/CWS
			result += 1; // version(byte)
			result += 4; // length(32bit-uint)			
			var rectNBits:uint = bytes[result] >>> 3;
			result += (5 + rectNBits * 4) / 8; // stage(rect)			
			result += 2;			
			result += 1; // frameRate(byte)
			result += 2; // totalFrames(16bit-uint)			
			return result;
		}		
		
		private function findFileAttributesPosition(offset:uint, bytes:ByteArray):uint
		{
			bytes.position = offset;			
			try {
				for (;;) {
					var byte:uint = bytes.readShort();
					var tag:uint = byte >>> 6;
					if (tag == 69) {
						return bytes.position - 2;
					}
					var length:uint = byte & 0x3f;
					if (length == 0x3f) {
						length = bytes.readInt();
					}
					bytes.position += length;
				}
			}
			catch (e:EOFError) {
			}			
			return NaN;
		}
		
		private function flagSWF9Bit(bytes:ByteArray):void
		{
			var pos:uint = findFileAttributesPosition(getBodyPosition(bytes), bytes);
			if (!isNaN(pos)) {
				bytes[pos + 2] |= 0x08;
			}
		}
		private function insertFileAttributesTag(bytes:ByteArray):void
		{
			var pos:uint = getBodyPosition(bytes);
			var afterBytes:ByteArray = new ByteArray();
			afterBytes.writeBytes(bytes, pos);
			bytes.length = pos;
			bytes.position = pos;
			bytes.writeByte(0x44);
			bytes.writeByte(0x11);
			bytes.writeByte(0x08);
			bytes.writeByte(0x00);
			bytes.writeByte(0x00);
			bytes.writeByte(0x00);
			bytes.writeBytes(afterBytes);
			afterBytes.length = 0;
		}
		private function updateVersion(bytes:ByteArray, version:uint):void
		{
			bytes[3] = version;
		}
		private function ioErrorHandler(event:IOErrorEvent):void
		{
			dispatchEvent(new IOErrorEvent(IOErrorEvent.IO_ERROR));
		}
		private function securityErrorHandler(event:SecurityErrorEvent):void
		{
			dispatchEvent(new SecurityErrorEvent(SecurityErrorEvent.SECURITY_ERROR));
		}
	}

}
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" 
	xmlns:avm1to2="sban.flexStudy.avm1to2.*" fontSize="12">
	
	<mx:Script>
		<![CDATA[
			
			[Bindable]
			private var totalFrames : int = 0;
			
			[Bindable]
			private var currentFrame : int = 0;
			
			
		]]>
	</mx:Script>
	
	<mx:Label x="19" y="22" text="This's a avm1 swf:"/>
	
	<mx:Canvas width="135" height="40" backgroundColor="#D9DCE2" x="19" y="48">
		<avm1to2:AVM1MvoieProxy id="avm1" url="sban/flexStudy/avm1to2/avm1.swf"><!--是Avm1的视频-->
			<avm1to2:complete>
				<![CDATA[
					totalFrames = avm1.totalFrames;
				]]>
			</avm1to2:complete>
			<avm1to2:enterFrame>
				<![CDATA[
					currentFrame = avm1.currentFrame;
				]]>
			</avm1to2:enterFrame>
		</avm1to2:AVM1MvoieProxy>
	</mx:Canvas>
	
	<mx:Label x="19" y="111" text="current frame:{currentFrame}"/>
	<mx:Label x="19" y="137" text="total frames:{totalFrames}"/>
	
	<mx:Button x="19" y="172" label="play" click="avm1.play()"/>
	<mx:Button x="19" y="202" label="stop" click="avm1.stop()"/>
	<mx:Button x="19" y="232" label="gotoAndStop(10)" click="avm1.gotoAndStop(1000)"/>
	<mx:Button x="19" y="262" label="gotoAndPlay(20)" click="avm1.gotoAndPlay(20)"/>
	
	
	
</mx:Application>
?原文:http://blog.sban.com.cn/2009/02/21/how-to-load-and-play-stop-avm1-swf-in-as3-or-flex.html
  相关解决方案