Hello,
I am using the Cordova Text To Speech Plugin in my Ionic Vue app. I call an api to receive my messages for TTS. The problem is that if the api responds with multiple messages, the TTS speaker function only reads the first function.
My Current Setup:
I save my responses in an array. Then i loop with a while-loop through my array. It works like a queue. I call the first item from my array and then send it to my tts function. When its done, the message is removed from the array. With console.logs I have also outputted everything, but only the first message is read aloud.
Handle my Array:
var messagesArr = [];
for (const rspmsg of responseMessageArr){
const message = rspmsg.text.text[0];
messagesArr.push(message);
}
while(messagesArr.length !== 0){
await this.tts(messagesArr[0]);
messagesArr.shift();
}
My tts function:
async tts(text){
TextToSpeech.speak({
text: text,
identifier: "com.apple.ttsbundle.siri_female_de-DE_compact",
locale: 'de-DE',
rate:0.5})
.then(() => console.log("Success speech"))
.catch((reason) => console.log(reason));
}
I find async
and await
a solution in search of a problem, and therefore don’t use them, so admittedly I’m not 100% confident in my understanding of their arcane details.
That being said, I believe your tts
function would (probably unexpectedly) desugar as follows:
tts(text: string): Promise<undefined> {
TextToSpeech.Speak({text, ...});
return Promise.resolve(undefined);
}
You are not explicitly returning the Promise
given to you by the TTS plugin to the caller of tts
, so I don’t think your code is actually waiting for the speak
to complete before potentially firing off the next speech request. That may be making the plugin ignore further calls that are hitting it too quickly for it to process. To see if this is the issue, try this:
async tts(text: string): Promise<any> {
return TextToSpeech.speak({text, ...});
}
Thank you for your effort. I tried your solution, and it has the same effect. I did not metion, that i also tried for testing purposes to delay the next call up to 10s. This also had no effect.
Hmm. In that case, the plugin you’re using seems to have been orphaned several years ago. I wonder if it might be worth giving this alternative a shot?
Thanks. I didn’t saw it. I’ll give it a shot. I’ll report back if it worked out
It works now. I think it was a combination of all. First I updated to the updated tts version. Then i removed the return from tts and in combination with a change at my API call everything works fine now. Thank you