1
8
9 import com.sekati.core.App;
10 import com.sekati.core.CoreObject;
11 import com.sekati.events.FramePulse;
12 import com.sekati.utils.Delegate;
13
14
17 class com.sekati.data.FLV extends CoreObject {
18
19 private var _this:FLV;
20 private var _ns:NetStream;
21 private var _nc:NetConnection;
22 private var _video:Video;
23 private var _videoURL:String;
24 private var _audio:Sound;
25 private var _audioContainer:MovieClip;
26 private var _paused:Boolean;
27 private var _started:Boolean;
28 private var _duration:Number;
29 private var _metadata:Object;
30 private var _lastSeekableTime:Number;
31
32
33 public function onProgress():Void {
34 }
35
36 public function onEvent():Void {
37 }
38
39
40 public function FLV() {
41 super( );
42 _this = this;
43 _paused = false;
44 _started = false;
45
46 _duration = 1000000000;
47 }
48
49
58 public function load(url:String, videoInstance:Video, width:Number, height:Number, audioContainer:MovieClip):Void {
59 _videoURL = url;
60 _video = videoInstance;
61 _audioContainer = audioContainer;
62
63 _nc = new NetConnection( );
64 _nc.connect( null );
65 _ns = new NetStream( _nc );
66
67 audioContainer.attachAudio( _ns );
68 _audio = new Sound( audioContainer );
69
70 _video._width = width;
71 _video._height = height;
72
73 _video.attachVideo( _ns );
74
75 var buffer:Number = (!App.FLV_BUFFER_TIME) ? 4 : App.FLV_BUFFER_TIME;
76
77 _ns.setBufferTime( buffer );
78
79 _ns.onMetaData = Delegate.create( _this, ns_onMetaData );
80 _ns.onStatus = Delegate.create( _this, ns_onStatus );
81 _nc.onResult = Delegate.create( _this, nc_onResult );
82 _nc.onStatus = Delegate.create( _this, nc_onStatus );
83
84 playVideo( );
85 stopVideo( );
86
87
88 FramePulse.getInstance( ).addFrameListener( _this );
89 }
90
91
92
93 private function _onEnterFrame():Void {
94
95
96 onProgress( getPercent( ), getPercentTimePlayed( ) );
97 }
98
99 public function clean(scope:MovieClip, obj:String):Void {
100
101 delete _audioContainer.onEnterFrame;
102 _audioContainer.onEnterFrame = null;
103 delete scope[obj];
104 }
105
106
107
108 public function playVideo():Void {
109 _started = true;
110 _ns.play( _videoURL );
111 }
112
113 public function stopVideo():Void {
114 _ns.pause( true );
115
116
117 _started = false;
118 _paused = false;
119 }
120
121 public function pause():Void {
122 if (!_paused && _started) {
123 _paused = true;
124 _ns.pause( true );
125 }
126 }
127
128 public function resume():Void {
129 if (_paused && _started) {
130 _paused = false;
131 _ns.pause( false );
132 }
133 }
134
135 public function pauseResume():Void {
136 if(_paused) {
137 resume( );
138 } else {
139 pause( );
140 }
141 }
142
143 public function setVolume(val:Number):Void {
144 _audio.setVolume( val );
145 }
146
147
148
149 public function seek(time:Number):Void {
150 _ns.seek( resolveTime( time ) );
151 }
152
153 public function seekToPercent(percent:Number):Void {
154 seek( _duration * percent / 100 );
155 }
156
157 public function ff():Void {
158
159 seek( getTime( ) + 2 );
160 }
161
162 public function rew():Void {
163
164 seek( getTime( ) - 2 );
165 }
166
167 private function resolveTime(time:Number):Number {
168
169 var maxTime:Number = (_lastSeekableTime != null) ? _lastSeekableTime : _duration;
170 return Math.max( Math.min( time, maxTime ), 0 );
171 }
172
173
174
175 public function isPaused():Boolean {
176 return _paused;
177 }
178
179 public function isPlaying():Boolean {
180 return _started;
181 }
182
183 public function isStopped():Boolean {
184 return !_started;
185 }
186
187 public function getTime():Number {
188 return _ns.time;
189 }
190
191 public function getTotalTime():Number {
192 return _duration;
193 }
194
195 public function getPercentTimePlayed():Number {
196 return getTime( ) * 100 / _duration;
197 }
198
199
200
201 public function getPercent():Number {
202 return Math.round( _ns.bytesLoaded / _ns.bytesTotal * 100 );
203 }
204
205 public function getBufferPercent():Number {
206 var total:Number = Math.min( _duration, _ns.bufferTime );
207 return Math.min( Math.round( _ns.bufferLength / total * 100 ), 100 );
208 }
209
210
211
212 private function nc_onResult(info:Object):Void {
213 trace( "unknown ncOnResult: " + info.code );
214 }
215
216 private function nc_onStatus(info:Object):Void {
217 trace( "unknown ncOnStatus: " + info.code );
218 }
219
220 private function ns_onStatus(info:Object):Void {
221 switch (info.code) {
222 case "NetStream.Buffer.Empty" :
223 onEvent( "bufferEmpty" );
224 break;
225 case "NetStream.Buffer.Full" :
226 onEvent( "bufferFull" );
227 break;
228 case "NetStream.Buffer.Flush" :
229 onEvent( "bufferFlush" );
230 break;
231 case "NetStream.Play.Start" :
232 onEvent( "start" );
233 break;
234 case "NetStream.Play.Stop" :
235 onEvent( "stop" );
236 break;
237 case "NetStream.Play.StreamNotFound" :
238 onEvent( "play_streamNotFound" );
239 break;
240 case "NetStream.Seek.InvalidTime" :
241 onEvent( "seek_InvalidTime" );
242 break;
243 case "NetStream.Seek.Notify" :
244 onEvent( "seek_notify" );
245 break;
246 default :
247 trace( "unrecognized onStatus value: " + info.code );
248 }
249 }
250
251 private function ns_onMetaData(metadata:Object):Void {
252 _duration = metadata.duration;
253 _lastSeekableTime = metadata.lastkeyframetimestamp;
254 _metadata = metadata;
255
256
257
270
271
283 }
284
285
289 public function destroy():Void {
290 FramePulse.getInstance( ).removeFrameListener( _this );
291 super.destroy( );
292 }
293 }