//
// The media player must be playing or else the required time changed events
// will not be fired.
try {
Condition<?> playingCondition = new PlayingCondition(mediaPlayer) {
@Override
protected boolean onBefore() {
// You do not have to use onBefore(), but sometimes it is very convenient, and guarantees
// that the required media player event listener is added before your condition is tested
mediaPlayer.startMedia(mrl);
return true;
}
};
playingCondition.await();
long time = period;
for(int i = 0; ; i++) {
// Some special cases here...
//
// 1. The duration may not be available yet, even if the media player is playing
// 2. For some media types it is not possible to set the position past the end - this
// means that you would have to wait for playback to reach the end normally
long duration = mediaPlayer.getLength();
if(duration > 0 && time >= duration) {
break;
}
System.out.println("Snapshot " + i);
Condition<?> timeReachedCondition = new TimeReachedCondition(mediaPlayer, time) {
@Override
protected boolean onBefore() {
mediaPlayer.setTime(targetTime);
return true;
}
};
timeReachedCondition.await();
Condition<?> pausedCondition = new PausedCondition(mediaPlayer) {
@Override
protected boolean onBefore() {
mediaPlayer.pause();
return true;
}
};
pausedCondition.await();
Condition<?> snapshotTakenCondition = new SnapshotTakenCondition(mediaPlayer) {
@Override
protected boolean onBefore() {
mediaPlayer.saveSnapshot();
return true;
}
};
snapshotTakenCondition.await();
playingCondition = new PlayingCondition(mediaPlayer) {
@Override
protected boolean onBefore() {
mediaPlayer.play();
return true;
}
};
playingCondition.await();
time += period;
}
}
catch(UnexpectedErrorConditionException e) {