I have a controller class which needs to perform various asynchronous operations for each item in a list:
- for each A
- play audio and wait for completion
- wait for t seconds
- For each sub-part B of A:
- play audio and wait
- wait for t2 seconds
I know the audio is complete when my OnCompletionListener.onCompletion
method is fired, and I know when the timer is completed when my CountDownTimer.onFinish()
method is fired. I've tried returning the listeners:
for (final A a : getAs()) {
show(a);
playAudio(a.audioBegin, new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
wait(a.secs, new OnFinishListener() {
@Override
public void onFinish() {
if (a.hasSubparts) {
for (final B b : a.getBs()) {
playAudio(b.audioBegin, new MediaPlayer.OnCompletionListener() {
...
}
}
}
}
But then at the end I can't get out of the inner loop, which makes me think this whole idea doesn't work. I've looked at the Future
interface, but that doesn't seem to be what I want either.
I know I could solve this with a bunch of variables to keep track of where I would be in the state flow, but a loop with some framework would be much cleaner. Thanks :)