in src/core/awspack/AbstractTextToSpeechFeature.js [1019:1098]
_startSpeech(text, config, playMethod = 'play') {
// If no text is provided, try to use the current speech
if (text === undefined && playMethod === 'resume' && this._currentSpeech) {
text = this._currentSpeech.text;
}
const currentPromise = this._currentPromise || {
play: new Deferred(
undefined,
() => {
currentPromise.speech.cancel();
},
() => {
currentPromise.speech.cancel();
},
() => {
currentPromise.speech.cancel();
}
),
speech: new Deferred(),
};
this._currentPromise = currentPromise;
this._getSpeech(text, config)
.then(speech => {
// Exit if the promise is no longer pending because of user interaction
if (!currentPromise.play.pending) {
return;
} else if (this._currentPromise !== currentPromise) {
// Cancel if another call to play has already been made
currentPromise.play.cancel();
return;
}
// Reset current speech when the speech ends
const onFinish = () => {
this._currentSpeech = null;
this._currentPromise = null;
};
// Cancel the currently playing speech
if (this._currentSpeech && this._currentSpeech.playing) {
if (playMethod === 'play') {
this._currentSpeech.cancel();
} else if (
playMethod === 'resume' &&
this._currentSpeech.audio !== speech.audio
) {
this._currentSpeech.cancel();
}
}
this._setCurrentSpeech(speech);
// Play the speech
currentPromise.speech = speech[playMethod](
this._host.now,
onFinish,
onFinish,
onFinish
);
currentPromise.speech
.then(() => {
if (currentPromise.speech.resolved) {
currentPromise.play.resolve();
} else {
currentPromise.play.cancel();
}
})
.catch(error => {
currentPromise.play.reject(error);
});
})
.catch(e => {
e = `Cannot ${playMethod} speech ${text} on host ${this.host.id}. ${e}`;
currentPromise.play.reject(e);
});
return currentPromise.play;
}