Menu

[r13]: / trunk / libs / OSMF / org / osmf / elements / compositeClasses / CompositePlayTrait.as  Maximize  Restore  History

Download this file

328 lines (299 with data), 9.8 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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
/*****************************************************
*
* 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.elements.compositeClasses
{
import org.osmf.events.PlayEvent;
import org.osmf.traits.MediaTraitBase;
import org.osmf.traits.MediaTraitType;
import org.osmf.traits.PlayState;
import org.osmf.traits.PlayTrait;
internal class CompositePlayTrait extends PlayTrait implements IReusable
{
/**
* Constructor.
*
* @param traitAggregator The object which is aggregating all instances
* of the PlayTrait within this composite trait.
* @param mode The composition mode to which this composite trait
* should adhere. See CompositionMode for valid values.
*
* @langversion 3.0
* @playerversion Flash 10
* @playerversion AIR 1.5
* @productversion OSMF 1.0
*/
public function CompositePlayTrait(traitAggregator:TraitAggregator, mode:String)
{
super();
this.traitAggregator = traitAggregator;
this.mode = mode;
traitAggregationHelper = new TraitAggregationHelper
( traitType
, traitAggregator
, processAggregatedChild
, processUnaggregatedChild
);
}
/**
* @private
*/
public function attach():void
{
traitAggregationHelper.attach();
}
/**
* @private
*/
public function detach():void
{
traitAggregationHelper.detach();
}
/**
* @private
*/
override protected function playStateChangeStart(newPlayState:String):void
{
if (newPlayState != playState && !playStateIsChanging)
{
// Prevent this from being reentrant.
playStateIsChanging = true;
if (mode == CompositionMode.PARALLEL)
{
// Invoke the appropriate method on all children.
if (newPlayState == PlayState.PLAYING)
{
traitAggregator.invokeOnEachChildTrait("play", [], MediaTraitType.PLAY);
}
else if (newPlayState == PlayState.PAUSED)
{
traitAggregator.invokeOnEachChildTrait("pause", [], MediaTraitType.PLAY);
}
else // STOPPED
{
traitAggregator.invokeOnEachChildTrait("stop", [], MediaTraitType.PLAY);
}
}
else // SERIAL
{
// We want to set the playState on the current child. But doing
// so could trigger events which would affect the state of this
// trait, which might cause it to get out of sync (since this call
// is generally followed by the set to the actual playState var).
// It is the responsibility of this trait to ensure that it's own
// state doesn't get out of sync. It does this by remembering
// the operation to invoke, and invoking it at the next "safe" time
// (which is basically when the postProcess method gets called).
deferredPlayTraitToSet = traitOfCurrentChild;
deferredPlayStateToSet = newPlayState;
}
playStateIsChanging = false;
}
}
/**
* @private
*/
override protected function playStateChangeEnd():void
{
// Never dispatch the event while we're in the middle of
// processing.
if (playStateIsChanging == false)
{
super.playStateChangeEnd();
}
// If we have a deferred operation to complete, do so now.
if (deferredPlayTraitToSet != null)
{
setPlayState(deferredPlayTraitToSet, deferredPlayStateToSet);
}
deferredPlayTraitToSet = null;
deferredPlayStateToSet = null;
}
// Internals
//
private function processAggregatedChild(child:MediaTraitBase):void
{
child.addEventListener(PlayEvent.PLAY_STATE_CHANGE, onPlayStateChange, false, 0, true);
child.addEventListener(PlayEvent.CAN_PAUSE_CHANGE, onCanPauseChange, false, 0, true);
var playTrait:PlayTrait = child as PlayTrait;
if (mode == CompositionMode.PARALLEL)
{
if (traitAggregator.getNumTraits(MediaTraitType.PLAY) == 1)
{
// The first added child's properties are applied to the
// composite trait.
setPlayState(this, playTrait.playState);
}
else
{
// All subsequently added children inherit their properties
// from the composite trait.
setPlayState(playTrait, this.playState);
}
updateCanPauseState();
}
else if (child == traitOfCurrentChild)
{
// The first added child's properties are applied to the
// composite trait.
setPlayState(this, playTrait.playState);
updateCanPauseState();
}
}
private function processUnaggregatedChild(child:MediaTraitBase):void
{
child.removeEventListener(PlayEvent.PLAY_STATE_CHANGE, onPlayStateChange);
child.removeEventListener(PlayEvent.CAN_PAUSE_CHANGE, onCanPauseChange);
updateCanPauseState();
}
private function onPlayStateChange(event:PlayEvent):void
{
var playTrait:PlayTrait = event.target as PlayTrait;
if (mode == CompositionMode.PARALLEL)
{
// The composition should reflect what its children do.
var computedPlayState:String = playTrait.playState;
// If this child reached the STOPPED state by virtue of
// reaching the end, then we only want to set the composite
// trait's state to STOPPED if all other children are
// similarly STOPPED.
if (computedPlayState == PlayState.STOPPED)
{
traitAggregator.forEachChildTrait
(
function(mediaTrait:PlayTrait):void
{
if (mediaTrait.playState != PlayState.STOPPED)
{
computedPlayState = mediaTrait.playState;
}
}
, MediaTraitType.PLAY
);
}
setPlayState(this, computedPlayState);
}
else if (playTrait == traitOfCurrentChild)
{
// The composition should reflect what its children do.
setPlayState(this, playTrait.playState);
// Typically, the CompositeTimeTrait will handle transitioning
// from one child to the next based on the receipt of the
// complete event. However, if the current child
// doesn't have the TimeTrait, then it obviously can't do so.
// So we check here for that case.
if (playTrait.playState == PlayState.STOPPED &&
traitAggregator.listenedChild.hasTrait(MediaTraitType.TIME) == false)
{
// If the current child has another sibling ahead of it,
// then the next sibling with a PlayTrait should be played.
SerialElementTransitionManager.playNextPlayableChild
( traitAggregator
, null
);
}
}
}
private function onCanPauseChange(event:PlayEvent):void
{
updateCanPauseState();
}
private function setPlayState(playTrait:PlayTrait, value:String):void
{
if (value != playTrait.playState)
{
if (value == PlayState.PLAYING)
{
playTrait.play();
}
else if (value == PlayState.PAUSED)
{
// If we can't pause, leave it alone. The composition may
// get out of sync, but that's acceptable.
if (playTrait.canPause)
{
playTrait.pause();
}
}
else // STOPPED
{
playTrait.stop();
}
}
}
private function updateCanPauseState():void
{
if (mode == CompositionMode.PARALLEL)
{
// The composition can be paused if at least one child can be paused.
var newCanPause:Boolean = false;
traitAggregator.forEachChildTrait
(
function(mediaTrait:MediaTraitBase):void
{
newCanPause ||= PlayTrait(mediaTrait).canPause;
}
, MediaTraitType.PLAY
);
if (canPause != newCanPause)
{
setCanPause(newCanPause);
// If the composite trait changes from pausable to unpausable
// while it's paused, we need to get the composite trait's state
// in sync with the state of its children (whether that be playing
// or stopped).
if (newCanPause == false && playState == PlayState.PAUSED)
{
var newPlayState:String;
traitAggregator.forEachChildTrait
(
function(mediaTrait:MediaTraitBase):void
{
newPlayState ||= PlayTrait(mediaTrait).playState;
}
, MediaTraitType.PLAY
);
if (newPlayState != null)
{
setPlayState(this, newPlayState);
}
}
}
}
else
{
setCanPause(traitOfCurrentChild.canPause);
}
}
private function get traitOfCurrentChild():PlayTrait
{
return traitAggregator.listenedChild
? traitAggregator.listenedChild.getTrait(MediaTraitType.PLAY) as PlayTrait
: null;
}
private var mode:String;
private var traitAggregator:TraitAggregator;
private var traitAggregationHelper:TraitAggregationHelper;
private var playStateIsChanging:Boolean;
private var deferredPlayTraitToSet:PlayTrait;
private var deferredPlayStateToSet:String;
}
}
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.