Changeset 135 for trunk/as3/com/jeroenwijering/models/VideoModel.as
- Timestamp:
- 01/16/09 07:00:07 (4 years ago)
- File:
-
- 1 edited
-
trunk/as3/com/jeroenwijering/models/VideoModel.as (modified) (5 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/as3/com/jeroenwijering/models/VideoModel.as
r104 r135 6 6 7 7 import com.jeroenwijering.events.*; 8 import com.jeroenwijering.models. ModelInterface;8 import com.jeroenwijering.models.BasicModel; 9 9 import com.jeroenwijering.player.Model; 10 10 import com.jeroenwijering.utils.NetClient; 11 11 12 import flash.events.*; 12 import flash.display.DisplayObject; 13 import flash.media.SoundTransform; 14 import flash.media.Video; 13 import flash.media.*; 15 14 import flash.net.*; 16 import flash.utils.clearInterval; 17 import flash.utils.setInterval; 15 import flash.utils.*; 18 16 19 17 20 public class VideoModel implements ModelInterface{18 public class VideoModel extends BasicModel { 21 19 22 20 23 /** reference to the model. **/24 private var model:Model;25 21 /** Video object to be instantiated. **/ 26 pr ivatevar video:Video;22 protected var video:Video; 27 23 /** NetConnection object for setup of the video stream. **/ 28 pr ivatevar connection:NetConnection;24 protected var connection:NetConnection; 29 25 /** NetStream instance that handles the stream IO. **/ 30 pr ivatevar stream:NetStream;26 protected var stream:NetStream; 31 27 /** Sound control object. **/ 32 private var transform:SoundTransform; 33 /** Interval ID for the time. **/ 34 private var timeinterval:Number; 28 protected var transform:SoundTransform; 35 29 /** Interval ID for the loading. **/ 36 private var loadinterval:Number; 37 /** Metadata received switch. **/ 38 private var metadata:Boolean; 30 protected var loadinterval:Number; 39 31 40 32 41 33 /** Constructor; sets up the connection and display. **/ 42 34 public function VideoModel(mod:Model):void { 43 model = mod;35 super(mod); 44 36 connection = new NetConnection(); 45 connection.addEventListener(NetStatusEvent.NET_STATUS,statusHandler);46 connection.addEventListener(SecurityErrorEvent.SECURITY_ERROR,errorHandler);47 connection.objectEncoding = ObjectEncoding.AMF0;48 37 connection.connect(null); 49 38 stream = new NetStream(connection); 50 39 stream.addEventListener(NetStatusEvent.NET_STATUS,statusHandler); 51 40 stream.addEventListener(IOErrorEvent.IO_ERROR,errorHandler); 52 stream.addEventListener(AsyncErrorEvent.ASYNC_ERROR, metaHandler);41 stream.addEventListener(AsyncErrorEvent.ASYNC_ERROR,errorHandler); 53 42 stream.bufferTime = model.config['bufferlength']; 54 43 stream.client = new NetClient(this); 55 44 video = new Video(320,240); 45 video.smoothing = model.config['smoothing']; 56 46 video.attachNetStream(stream); 57 47 transform = new SoundTransform(); 58 stream.soundTransform = transform;59 quality(model.config['quality']);60 48 model.config['mute'] == true ? volume(0): volume(model.config['volume']); 61 49 }; … … 63 51 64 52 /** Catch security errors. **/ 65 private function errorHandler(evt:ErrorEvent):void { 53 protected function errorHandler(evt:ErrorEvent):void { 54 stop(); 66 55 model.sendEvent(ModelEvent.ERROR,{message:evt.text}); 67 56 }; … … 69 58 70 59 /** Load content. **/ 71 public function load():void {72 video.clear();60 override public function load(itm:Object):void { 61 super.load(itm); 73 62 model.mediaHandler(video); 74 stream.play(model.playlist[model.config['item']]['file']); 63 stream.play(item['file']); 64 interval = setInterval(positionInterval,100); 75 65 loadinterval = setInterval(loadHandler,200); 76 timeinterval = setInterval(timeHandler,100);77 model.sendEvent(ModelEvent.STATE,{newstate:ModelStates.BUFFERING});78 66 }; 79 67 80 68 81 69 /** Interval for the loading progress **/ 82 pr ivate function loadHandler():void {83 var ldd = stream.bytesLoaded;84 var ttl = stream.bytesTotal;70 protected function loadHandler():void { 71 var ldd:Number = stream.bytesLoaded; 72 var ttl:Number = stream.bytesTotal; 85 73 model.sendEvent(ModelEvent.LOADED,{loaded:ldd,total:ttl}); 86 74 if(ldd == ttl && ldd > 0) { … … 90 78 91 79 92 /** Catch noncritical errors. **/93 private function metaHandler(evt:ErrorEvent):void {94 model.sendEvent(ModelEvent.META,{error:evt.text});95 };96 97 98 80 /** Get metadata information from netstream class. **/ 99 81 public function onData(dat:Object):void { 100 if(dat.type == 'metadata' && !metadata) { 101 metadata = true; 102 if(dat.width) { 103 video.width = dat.width; 104 video.height = dat.height; 105 } 106 if(model.playlist[model.config['item']]['start'] > 0) { 107 seek(model.playlist[model.config['item']]['start']); 108 } 82 if(dat.width) { 83 video.width = dat.width; 84 video.height = dat.height; 85 } 86 if(dat.duration) { 87 dat.duration -= item['start']; 109 88 } 110 89 model.sendEvent(ModelEvent.META,dat); … … 113 92 114 93 /** Pause playback. **/ 115 public function pause():void {116 clearInterval(timeinterval);94 override public function pause():void { 95 super.pause(); 117 96 stream.pause(); 118 model.sendEvent(ModelEvent.STATE,{newstate:ModelStates.PAUSED});119 97 }; 120 98 121 99 122 100 /** Resume playing. **/ 123 public function play():void { 101 override public function play():void { 102 super.play(); 124 103 stream.resume(); 125 timeinterval = setInterval(timeHandler,100);126 model.sendEvent(ModelEvent.STATE,{newstate:ModelStates.PLAYING});127 104 }; 128 105 129 106 130 /** Change the smoothing mode. **/ 131 public function quality(qua:Boolean):void { 132 if(qua == true) { 133 video.smoothing = true; 134 video.deblocking = 3; 135 } else { 136 video.smoothing = false; 137 video.deblocking = 1; 107 /** Interval for the position progress **/ 108 override protected function positionInterval():void { 109 position = Math.round(stream.time*10)/10; 110 var bfr:Number = Math.round(stream.bufferLength/stream.bufferTime*100); 111 if(bfr < 95 && position < Math.abs(item['duration']-stream.bufferTime-1)) { 112 model.sendEvent(ModelEvent.BUFFER,{percentage:bfr}); 113 if(model.config['state'] != ModelStates.BUFFERING && bfr < 25) { 114 model.sendEvent(ModelEvent.STATE,{newstate:ModelStates.BUFFERING}); 115 } 116 } else if (bfr > 95 && model.config['state'] != ModelStates.PLAYING) { 117 model.sendEvent(ModelEvent.STATE,{newstate:ModelStates.PLAYING}); 118 } 119 if(position-item['start'] < item['duration']) { 120 var pos:Number = Math.max(0,Math.round((position-item['start'])*10)/10); 121 model.sendEvent(ModelEvent.TIME,{position:pos,duration:item['duration']}); 122 } else if (item['duration'] > 0) { 123 pause(); 124 model.sendEvent(ModelEvent.STATE,{newstate:ModelStates.COMPLETED}); 138 125 } 139 126 }; 140 127 141 128 142 /** Change the smoothing mode. **/ 143 public function seek(pos:Number):void { 144 clearInterval(timeinterval); 145 stream.seek(pos); 146 play(); 129 /** Seek to a new position. **/ 130 override public function seek(pos:Number):void { 131 super.seek(pos); 132 stream.seek(position); 147 133 }; 148 134 149 135 150 136 /** Receive NetStream status updates. **/ 151 private function statusHandler(evt:NetStatusEvent):void { 152 if(evt.info.code == "NetStream.Play.Stop" && stream.bytesLoaded == stream.bytesTotal) { 153 clearInterval(timeinterval); 154 model.sendEvent(ModelEvent.STATE,{newstate:ModelStates.COMPLETED}); 155 } else if (evt.info.code == "NetStream.Play.StreamNotFound") { 156 stop(); 157 model.sendEvent(ModelEvent.ERROR,{message:'Video not found: '+model.playlist[model.config['item']]['file']}); 137 protected function statusHandler(evt:NetStatusEvent):void { 138 switch (evt.info.code) { 139 case "NetStream.Play.Stop": 140 clearInterval(interval); 141 model.sendEvent(ModelEvent.STATE,{newstate:ModelStates.COMPLETED}); 142 break; 143 case "NetStream.Play.StreamNotFound": 144 stop(); 145 model.sendEvent(ModelEvent.ERROR,{message:'Video not found: '+item['file']}); 146 break; 147 default: 148 model.sendEvent(ModelEvent.META,{info:evt.info.code}); 149 break; 158 150 } 159 model.sendEvent(ModelEvent.META,{info:evt.info.code});160 151 }; 161 152 162 153 163 154 /** Destroy the video. **/ 164 public function stop():void { 165 stream.pause(); 166 if(stream.bytesLoaded != stream.bytesTotal) { 167 stream.close(); 168 } 169 metadata = false; 155 override public function stop():void { 156 stream.close(); 170 157 clearInterval(loadinterval); 171 clearInterval(timeinterval); 172 }; 173 174 175 /** Interval for the position progress **/ 176 private function timeHandler():void { 177 var bfr = Math.round(stream.bufferLength/stream.bufferTime*100); 178 var pos = Math.round(stream.time*10)/10; 179 var dur = model.playlist[model.config['item']]['duration']; 180 if(bfr < 95 && pos < Math.abs(dur-stream.bufferTime*2)) { 181 model.sendEvent(ModelEvent.BUFFER,{percentage:bfr}); 182 if(model.config['state'] != ModelStates.BUFFERING && bfr < 10) { 183 model.sendEvent(ModelEvent.STATE,{newstate:ModelStates.BUFFERING}); 184 } 185 } else if (model.config['state'] == ModelStates.BUFFERING) { 186 model.sendEvent(ModelEvent.STATE,{newstate:ModelStates.PLAYING}); 187 } 188 model.sendEvent(ModelEvent.TIME,{position:pos}); 158 super.stop(); 189 159 }; 190 160 191 161 192 162 /** Set the volume level. **/ 193 public function volume(vol:Number):void {163 override public function volume(vol:Number):void { 194 164 transform.volume = vol/100; 195 165 stream.soundTransform = transform;
Note: See TracChangeset
for help on using the changeset viewer.
