Menu

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

Download this file

207 lines (186 with data), 6.7 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
/*****************************************************
*
* 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 __AS3__.vec.Vector;
import org.osmf.events.SeekEvent;
import org.osmf.media.MediaElement;
import org.osmf.traits.MediaTraitBase;
import org.osmf.traits.MediaTraitType;
import org.osmf.traits.PlayState;
import org.osmf.traits.PlayTrait;
import org.osmf.traits.SeekTrait;
import org.osmf.traits.TimeTrait;
/**
* Implementation of SeekTrait which can be a composite media trait.
*
* This is the parallel case in which all child media elements seek together.
*
* @langversion 3.0
* @playerversion Flash 10
* @playerversion AIR 1.5
* @productversion OSMF 1.0
*/
internal class ParallelSeekTrait extends CompositeSeekTrait
{
public function ParallelSeekTrait(traitAggregator:TraitAggregator, owner:MediaElement)
{
super(traitAggregator, CompositionMode.PARALLEL, owner);
this.owner = owner;
}
// Overrides
//
/**
* @private
*/
override protected function doSeek(seekOp:CompositeSeekOperationInfo):void
{
inSeek = true;
// How to seek has been done by a call to the canSeekTo method. For parallel
// seekable trait, it needs to instruct each child to seek.
var parallelSeek:ParallelSeekOperationInfo = seekOp as ParallelSeekOperationInfo;
var childSeekOperations:Vector.<ChildSeekOperation> = parallelSeek.childSeekOperations;
for (var i:int = 0; i < childSeekOperations.length; i++)
{
doChildSeek(childSeekOperations[i] as ChildSeekOperation);
}
inSeek = false;
}
private function doChildSeek(childSeekOperation:ChildSeekOperation):void
{
var childSeekTrait:SeekTrait = childSeekOperation.child.getTrait(MediaTraitType.SEEK) as SeekTrait;
childSeekTrait.addEventListener(SeekEvent.SEEKING_CHANGE, onChildSeekingChange);
childSeekTrait.seek(childSeekOperation.time);
function onChildSeekingChange(event:SeekEvent):void
{
if (event.seeking == false)
{
childSeekTrait.removeEventListener(SeekEvent.SEEKING_CHANGE, onChildSeekingChange);
// When the seek completes, make sure the child is playing if the
// composite element is playing. But if a child has reached its
// duration, then there's no need to play it.
var childPlayTrait:PlayTrait = childSeekOperation.child.getTrait(MediaTraitType.PLAY) as PlayTrait;
var childTimeTrait:TimeTrait = childSeekOperation.child.getTrait(MediaTraitType.TIME) as TimeTrait;
var parentPlayTrait:PlayTrait = owner.getTrait(MediaTraitType.PLAY) as PlayTrait;
if ( childPlayTrait != null
&& parentPlayTrait != null
&& ( childTimeTrait == null
|| childTimeTrait.currentTime != childTimeTrait.duration
)
)
{
if (parentPlayTrait.playState == PlayState.PLAYING)
{
childPlayTrait.play();
}
else if (parentPlayTrait.playState == PlayState.PAUSED && childPlayTrait.canPause)
{
childPlayTrait.pause();
}
}
}
}
}
/**
* @private
*/
override protected function prepareSeekOperationInfo(time:Number):CompositeSeekOperationInfo
{
var seekToOp:ParallelSeekOperationInfo = new ParallelSeekOperationInfo();
for (var i:int = 0; i < traitAggregator.numChildren; i++)
{
var childSeekTrait:SeekTrait = traitAggregator.getChildAt(i).getTrait(MediaTraitType.SEEK) as SeekTrait;
var childTimeTrait:TimeTrait = traitAggregator.getChildAt(i).getTrait(MediaTraitType.TIME) as TimeTrait;
if (childTimeTrait == null || isNaN(childTimeTrait.duration))
{
// If the child has no TimeTrait (such as an image element) or the duration is undefined, ignore it.
continue;
}
if (childSeekTrait == null)
{
// This is the condition where TimeTrait is not null and duration is well defined while
// the SeekTrait is absent, so we cannot seek.
seekToOp.canSeekTo = false;
break;
}
var seekTime:Number = Math.min(time, childTimeTrait.duration);
seekToOp.canSeekTo = childSeekTrait.canSeekTo(seekTime);
if (seekToOp.canSeekTo)
{
var childSeekOp:ChildSeekOperation = new ChildSeekOperation
( traitAggregator.getChildAt(i)
, seekTime
);
seekToOp.childSeekOperations.push(childSeekOp);
}
else
{
break;
}
}
// If no seek operations were prepared, then we can't actually seek.
if (seekToOp.childSeekOperations.length == 0)
{
seekToOp.canSeekTo = false;
}
return seekToOp;
}
/**
* @private
*/
override protected function checkSeeking():Boolean
{
// The parallel seek trait is in the seeking state if at least one
// of the children is in the seeking state.
var seekingState:Boolean = false;
traitAggregator.forEachChildTrait
(
function(mediaTrait:MediaTraitBase):void
{
seekingState ||= SeekTrait(mediaTrait).seeking;
}
, MediaTraitType.SEEK
);
return seekingState;
}
/**
* @private
*/
override protected function onSeekingChanged(event:SeekEvent):void
{
if (event.seeking && !inSeek)
{
// In this case, the child seek causes its siblings (in
// parallel) to seek.
seek(event.time);
}
else if (event.seeking == false && checkSeeking() == false)
{
// The child is exiting the seeking state, so we just
// update the composite seeking state.
setSeeking(false, event.time);
}
}
private var inSeek:Boolean = false;
private var owner:MediaElement;
}
}
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.