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;
}
print("Playing intro menu");
playIntroMenu(channel);
}, media: ['sound:are-you-still-there']);
}
// Start up the next sound and handle whatever happens
void queueUpSound() {
if (state.done == false) {
// 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) {
print("playback finshed");
queueUpSound();
});
print("Playing next sound");
var nextSoundIndex = menu.sounds.indexOf(state.currentSound!) + 1;
if (menu.sounds.length > nextSoundIndex) {
state.currentSound = menu.sounds[nextSoundIndex];
} else {
state.currentSound = null;
//state.done = true;
}
print("Current sound: ${state.currentSound}");
}
}
}
// Cancel the menu, as the user did something
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', cancelMenu);
// channel.removeListener('StasisEnd', cancelMenu);
}
channel.on('ChannelDtmfReceived', (err, channel) {
cancelMenu();
});
//channel.on('StasisEnd', cancelMenu);
queueUpSound();
}