Ignore:
Timestamp:
01/16/09 07:00:07 (4 years ago)
Author:
jeroen
Message:

several bugfixes and the ability to restrict files from start to duration

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/as3/com/jeroenwijering/models/VideoModel.as

    r104 r135  
    66 
    77import com.jeroenwijering.events.*; 
    8 import com.jeroenwijering.models.ModelInterface; 
     8import com.jeroenwijering.models.BasicModel; 
    99import com.jeroenwijering.player.Model; 
    1010import com.jeroenwijering.utils.NetClient; 
     11 
    1112import flash.events.*; 
    12 import flash.display.DisplayObject; 
    13 import flash.media.SoundTransform; 
    14 import flash.media.Video; 
     13import flash.media.*; 
    1514import flash.net.*; 
    16 import flash.utils.clearInterval; 
    17 import flash.utils.setInterval; 
     15import flash.utils.*; 
    1816 
    1917 
    20 public class VideoModel implements ModelInterface { 
     18public class VideoModel extends BasicModel { 
    2119 
    2220 
    23         /** reference to the model. **/ 
    24         private var model:Model; 
    2521        /** Video object to be instantiated. **/ 
    26         private var video:Video; 
     22        protected var video:Video; 
    2723        /** NetConnection object for setup of the video stream. **/ 
    28         private var connection:NetConnection; 
     24        protected var connection:NetConnection; 
    2925        /** NetStream instance that handles the stream IO. **/ 
    30         private var stream:NetStream; 
     26        protected var stream:NetStream; 
    3127        /** Sound control object. **/ 
    32         private var transform:SoundTransform; 
    33         /** Interval ID for the time. **/ 
    34         private var timeinterval:Number; 
     28        protected var transform:SoundTransform; 
    3529        /** Interval ID for the loading. **/ 
    36         private var loadinterval:Number; 
    37         /** Metadata received switch. **/ 
    38         private var metadata:Boolean; 
     30        protected var loadinterval:Number; 
    3931 
    4032 
    4133        /** Constructor; sets up the connection and display. **/ 
    4234        public function VideoModel(mod:Model):void { 
    43                 model = mod; 
     35                super(mod); 
    4436                connection = new NetConnection(); 
    45                 connection.addEventListener(NetStatusEvent.NET_STATUS,statusHandler); 
    46                 connection.addEventListener(SecurityErrorEvent.SECURITY_ERROR,errorHandler); 
    47                 connection.objectEncoding = ObjectEncoding.AMF0; 
    4837                connection.connect(null); 
    4938                stream = new NetStream(connection); 
    5039                stream.addEventListener(NetStatusEvent.NET_STATUS,statusHandler); 
    5140                stream.addEventListener(IOErrorEvent.IO_ERROR,errorHandler); 
    52                 stream.addEventListener(AsyncErrorEvent.ASYNC_ERROR,metaHandler); 
     41                stream.addEventListener(AsyncErrorEvent.ASYNC_ERROR,errorHandler); 
    5342                stream.bufferTime = model.config['bufferlength']; 
    5443                stream.client = new NetClient(this); 
    5544                video = new Video(320,240); 
     45                video.smoothing = model.config['smoothing']; 
    5646                video.attachNetStream(stream); 
    5747                transform = new SoundTransform(); 
    58                 stream.soundTransform = transform; 
    59                 quality(model.config['quality']); 
    6048                model.config['mute'] == true ? volume(0): volume(model.config['volume']); 
    6149        }; 
     
    6351 
    6452        /** Catch security errors. **/ 
    65         private function errorHandler(evt:ErrorEvent):void { 
     53        protected function errorHandler(evt:ErrorEvent):void { 
     54                stop(); 
    6655                model.sendEvent(ModelEvent.ERROR,{message:evt.text}); 
    6756        }; 
     
    6958 
    7059        /** Load content. **/ 
    71         public function load():void { 
    72                 video.clear(); 
     60        override public function load(itm:Object):void { 
     61                super.load(itm); 
    7362                model.mediaHandler(video); 
    74                 stream.play(model.playlist[model.config['item']]['file']); 
     63                stream.play(item['file']); 
     64                interval = setInterval(positionInterval,100); 
    7565                loadinterval = setInterval(loadHandler,200); 
    76                 timeinterval = setInterval(timeHandler,100); 
    77                 model.sendEvent(ModelEvent.STATE,{newstate:ModelStates.BUFFERING}); 
    7866        }; 
    7967 
    8068 
    8169        /** Interval for the loading progress **/ 
    82         private 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; 
    8573                model.sendEvent(ModelEvent.LOADED,{loaded:ldd,total:ttl}); 
    8674                if(ldd == ttl && ldd > 0) { 
     
    9078 
    9179 
    92         /** Catch noncritical errors. **/ 
    93         private function metaHandler(evt:ErrorEvent):void { 
    94                 model.sendEvent(ModelEvent.META,{error:evt.text}); 
    95         }; 
    96  
    97  
    9880        /** Get metadata information from netstream class. **/ 
    9981        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']; 
    10988                } 
    11089                model.sendEvent(ModelEvent.META,dat); 
     
    11392 
    11493        /** Pause playback. **/ 
    115         public function pause():void { 
    116                 clearInterval(timeinterval); 
     94        override public function pause():void { 
     95                super.pause(); 
    11796                stream.pause(); 
    118                 model.sendEvent(ModelEvent.STATE,{newstate:ModelStates.PAUSED}); 
    11997        }; 
    12098 
    12199 
    122100        /** Resume playing. **/ 
    123         public function play():void { 
     101        override public function play():void { 
     102                super.play(); 
    124103                stream.resume(); 
    125                 timeinterval = setInterval(timeHandler,100); 
    126                 model.sendEvent(ModelEvent.STATE,{newstate:ModelStates.PLAYING}); 
    127104        }; 
    128105 
    129106 
    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}); 
    138125                } 
    139126        }; 
    140127 
    141128 
    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); 
    147133        }; 
    148134 
    149135 
    150136        /** 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; 
    158150                } 
    159                 model.sendEvent(ModelEvent.META,{info:evt.info.code}); 
    160151        }; 
    161152 
    162153 
    163154        /** 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(); 
    170157                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(); 
    189159        }; 
    190160 
    191161 
    192162        /** Set the volume level. **/ 
    193         public function volume(vol:Number):void { 
     163        override public function volume(vol:Number):void { 
    194164                transform.volume = vol/100; 
    195165                stream.soundTransform = transform; 
Note: See TracChangeset for help on using the changeset viewer.