Menu

[r329]: / trunk / framework / OSMF / org / osmf / traits / TimeTrait.as  Maximize  Restore  History

Download this file

282 lines (260 with data), 7.6 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
/*****************************************************
*
* Copyright 2009 Adobe Systems Incorporated. All Rights Reserved.
*
*****************************************************
* The contents of this file are subject to the Mozilla Public License
* Version 1.1 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS"
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
* License for the specific language governing rights and limitations
* under the License.
*
*
* The Initial Developer of the Original Code is Adobe Systems Incorporated.
* Portions created by Adobe Systems Incorporated are Copyright (C) 2009 Adobe Systems
* Incorporated. All Rights Reserved.
*
*****************************************************/
package org.osmf.traits
{
import org.osmf.events.TimeEvent;
/**
* Dispatched when the duration of the trait changed.
*
* @eventType org.osmf.events.TimeEvent.DURATION_CHANGE
*
* @langversion 3.0
* @playerversion Flash 10
* @playerversion AIR 1.5
* @productversion OSMF 1.0
*/
[Event(name="durationChange", type="org.osmf.events.TimeEvent")]
/**
* Dispatched when the currentTime of the trait has changed to a value
* equal to its duration.
*
* @eventType org.osmf.events.TimeEvent.COMPLETE
*
* @langversion 3.0
* @playerversion Flash 10
* @playerversion AIR 1.5
* @productversion OSMF 1.0
*/
[Event(name="complete",type="org.osmf.events.TimeEvent")]
/**
* TimeTrait defines the trait interface for media that have a duration and
* a currentTime. It can also be used as the base class for a more specific
* TimeTrait subclass.
*
* <p>Use the <code>MediaElement.hasTrait(MediaTraitType.TIME)</code> method to query
* whether a media element has a trait of this type.
* If <code>hasTrait(MediaTraitType.TIME)</code> returns <code>true</code>,
* use the <code>MediaElement.getTrait(MediaTraitType.TIME)</code> method
* to get an object that is of this type.</p>
*
* @see org.osmf.media.MediaElement
*
* @langversion 3.0
* @playerversion Flash 10
* @playerversion AIR 1.5
* @productversion OSMF 1.0
*/
public class TimeTrait extends MediaTraitBase
{
/**
* Constructor.
*
* @param duration The duration of the media, in seconds. The default
* is NaN (no duration).
*
* @langversion 3.0
* @playerversion Flash 10
* @playerversion AIR 1.5
* @productversion OSMF 1.0
*/
public function TimeTrait(duration:Number=NaN)
{
super(MediaTraitType.TIME);
_duration = duration;
}
/**
* The duration of the media, in seconds.
*
* @langversion 3.0
* @playerversion Flash 10
* @playerversion AIR 1.5
* @productversion OSMF 1.0
*/
public function get duration():Number
{
return _duration;
}
/**
* The current time of the media, in seconds. Must never
* exceed the <code>duration</code>.
*
* @langversion 3.0
* @playerversion Flash 10
* @playerversion AIR 1.5
* @productversion OSMF 1.0
*/
public function get currentTime():Number
{
return _currentTime;
}
// Internals
//
/**
* Called immediately before the <code>duration</code> property is changed.
* <p>Subclasses can override this method to communicate the change to the media.</p>
*
* @param newDuration New <code>duration</code> value.
*
* @langversion 3.0
* @playerversion Flash 10
* @playerversion AIR 1.5
* @productversion OSMF 1.0
*/
protected function durationChangeStart(newDuration:Number):void
{
}
/**
* Called just after the <code>duration</code> property has changed.
* Dispatches the change event.
* <p>Subclasses that override should call this method to
* dispatch the durationChange event.</p>
*
* @param oldDuration Previous <code>duration</code> value.
*
*
* @langversion 3.0
* @playerversion Flash 10
* @playerversion AIR 1.5
* @productversion OSMF 1.0
*/
protected function durationChangeEnd(oldDuration:Number):void
{
dispatchEvent(new TimeEvent(TimeEvent.DURATION_CHANGE, false, false, _duration));
}
/**
* Called immediately before the <code>currentTime</code> property is changed.
* <p>Subclasses can override this method to communicate the change to the media.</p>
* @param newCurrentTime New <code>currentTime</code> value.
*
* @langversion 3.0
* @playerversion Flash 10
* @playerversion AIR 1.5
* @productversion OSMF 1.0
*/
protected function currentTimeChangeStart(newCurrentTime:Number):void
{
}
/**
* Called just after the <code>currentTime</code> property has changed.
* @param oldCurrentTime Previous <code>currentTime</code> value.
*
*
* @langversion 3.0
* @playerversion Flash 10
* @playerversion AIR 1.5
* @productversion OSMF 1.0
*/
protected function currentTimeChangeEnd(oldCurrentTime:Number):void
{
}
/**
* @private
*
* Called when a subclass or a media element that has the temporal trait first detects
* that <code>currentTime</code> equals <code>duration</code>.
* <p>Not called when both <code>currentTime</code> and <code>duration</code> equal zero.</p>
*
* <p>Dispatches the complete event.</p>
*
* <p>Exposed as protected (though undocumented) because some subclasses need to
* prevent event dispatch.</p>
*
* @langversion 3.0
* @playerversion Flash 10
* @playerversion AIR 1.5
* @productversion OSMF 1.0
*/
protected function signalComplete():void
{
dispatchEvent(new TimeEvent(TimeEvent.COMPLETE));
}
/**
* Invoking this setter will result in the trait's currentTime
* value changing if it differs from currentTime's current value.
*
* @langversion 3.0
* @playerversion Flash 10
* @playerversion AIR 1.5
* @productversion OSMF 1.0
*/
protected final function setCurrentTime(value:Number):void
{
if (!isNaN(value))
{
// Don't ever let the currentTime exceed the duration.
if (!isNaN(_duration))
{
value = Math.min(value, _duration);
}
else
{
value = 0;
}
}
if ( _currentTime != value
&& !
( isNaN(_currentTime)
&& isNaN(value)
)
)
{
currentTimeChangeStart(value);
var oldCurrentTime:Number = _currentTime;
_currentTime = value;
currentTimeChangeEnd(oldCurrentTime);
if (currentTime == duration && currentTime > 0)
{
signalComplete();
}
}
}
/**
* Invoking this setter will result in the trait's duration
* value changing if it differs from duration's current value.
*
* @langversion 3.0
* @playerversion Flash 10
* @playerversion AIR 1.5
* @productversion OSMF 1.0
*/
protected final function setDuration(value:Number):void
{
if (_duration != value)
{
durationChangeStart(value);
var oldDuration:Number = _duration;
_duration = value;
durationChangeEnd(oldDuration);
// Current time cannot exceed duration.
if ( !isNaN(_currentTime)
&& !isNaN(_duration)
&& _currentTime > _duration
)
{
setCurrentTime(duration);
}
}
}
private var _duration:Number;
private var _currentTime:Number;
}
}
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.