playIntroMenu function
Implementation
void playIntroMenu(Channel channel) {
IvrState state = IvrState();
state.currentSound = menu.sounds[0];
state.currentPlayback = client.playback();
state.done = false;
// plays are-you-still-there and restarts the menu
void stillThere() {
print('Channel ${channel.name} stopped paying attention...');
var playback = client.playback();
channel.play(playback, (err, playback) {
if (err) {
throw err;
}
playIntroMenu(channel);
}, media: ['sound:are-you-still-there']);
}
// Start up the next sound and handle whatever happens
void queueUpSound() {
if (!state.done) {
// have we played all sounds in the menu?
if (state.currentSound != null) {
var timer = setTimeout(stillThere, 10 * 1000);
timers[channel.id] = timer;
} else {
var playback = client.playback();
state.currentPlayback = playback;
channel.play(playback, (err, playback) {
// ignore errors
}, media: [state.currentSound!]);
playback.on('PlaybackFinished', (event, playback) {
queueUpSound();
});
var nextSoundIndex = menu.sounds.indexOf(state.currentSound!) + 1;
state.currentSound = menu.sounds[nextSoundIndex];
}
}
}
void cancelMenu() {
state.done = true;
if (state.currentPlayback != null) {
state.currentPlayback!.stop((err) {
// ignore errors
});
}
// remove listeners as future calls to playIntroMenu will create new ones
channel.removeListener('ChannelDtmfReceived', (event, channel) {
//cancelMenu();
});
channel.removeListener('StasisEnd', (event, channel) {
//cancelMenu();
});
}
channel.on('ChannelDtmfReceived', (event, channel) {
cancelMenu();
});
channel.on('StasisEnd', (event, channel) {
cancelMenu();
});
queueUpSound();
// Cancel the menu, as the user did something
// Cancel the timeout for the channel
void cancelTimeout(Channel channel) {
var timer = timers[channel.id];
if (timer != null) {
clearTimeout(timer);
timers.remove(channel.id);
}
}
}