T
- type of result that may be returned when the desired condition arisespublic abstract class Condition<T> extends MediaPlayerEventAdapter
Instances of this class, or its sub-classes, are not reusable.
This implementation works by adding a temporary event listener to an associated
media player then waiting, via await()
, for an internal synchronisation
object to trigger, via ready()
or ready(Object)
when one or
other of the event listener's implementation methods detects the desired media
player state. The temporary event listener is then removed and the await()
method is unblocked and returns.
Commonly needed triggers are implemented for error()
and finished()
.
This facilitates a semblance of synchronous or sequential media player programming.
Sub-classes have access to the associated mediaPlayer
if needed.
Sub-classes may also override onBefore()
and onAfter(Object)
to
implement behaviour that executes respectively before awaiting the condition and
after the condition state is reached.
Using onBefore()
guarantees that the media player event listener has been
registered with the media player before the condition implementation is executed.
Note that as with other MediaPlayerEventListener
implementations the
event callbacks are running in a background thread.
Example:
try { Condition<?> playingCondition = new PlayingCondition(mediaPlayer) { @Override protected void onBefore() { mediaPlayer.play(); } }; playingCondition.await(); // Do some interesting things, wait for some other conditions... } catch(UnexpectedErrorConditionException e) { // Whatever... } catch(UnexpectedFinishedConditionException e) { // Whatever... }
DefaultCondition
Constructor and Description |
---|
Condition(MediaPlayer mediaPlayer)
Create a new waiter.
|
Modifier and Type | Method and Description |
---|---|
T |
await()
Wait for the required condition to occur.
|
backward, buffering, elementaryStreamAdded, elementaryStreamDeleted, elementaryStreamSelected, endOfSubItems, error, finished, forward, lengthChanged, mediaChanged, mediaDurationChanged, mediaFreed, mediaMetaChanged, mediaParsedChanged, mediaStateChanged, mediaSubItemAdded, mediaSubItemTreeAdded, newMedia, opening, pausableChanged, paused, playing, positionChanged, scrambledChanged, seekableChanged, snapshotTaken, stopped, subItemFinished, subItemPlayed, timeChanged, titleChanged, videoOutput
public Condition(MediaPlayer mediaPlayer)
mediaPlayer
- media playerpublic final T await() throws InterruptedException, UnexpectedErrorConditionException, UnexpectedFinishedConditionException
InterruptedException
- if the condition was interrupted while waitingUnexpectedErrorConditionException
- if an unexpected error occurredUnexpectedFinishedConditionException
- if the condition finished unexpectedlyCopyright © 2009–2015 Caprica Software Limited. All rights reserved.