Skip to content

Commit

Permalink
feat: Have Polly wait for herself to finish what she's saying when la…
Browse files Browse the repository at this point in the history
…nguage auto switching
  • Loading branch information
jolzee committed Jun 22, 2020
1 parent a1071c7 commit 3743971
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion src/utils/polly.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ export default class Polly {
this.audio = null;
}

say(text, voice) {
async say(text, voice) {
if (text && voice) {
await waitForPollyToFinish();
this.stop();
this.audio = new Audio(
`${window.leopardConfig.tts.url}?text=${encodeURIComponent(text)}&voice=${voice}`
Expand All @@ -13,6 +14,19 @@ export default class Polly {
}
}

async waitForPollyToFinish() {
while (true) {
if (!isPlaying()) {
return;
}
await null; // prevents app from hanging
}
}

isPlaying() {
return !this.audio.paused;
}

stop() {
if (this.audio) {
// console.log("Pausing Audio!!!");
Expand Down

0 comments on commit 3743971

Please sign in to comment.