This function is supposed to read a value from localStorage or get it from API and return a blob in both ways.
This is the service function:
getAudioFile (text: string): Observable<Blob> {
if (localStorage.getItem(text)) {
return new Observable(() => {
return new Blob([localStorage.getItem(text)], {type: 'audio/wav'});
});
}
return this.http.post(this.cors + this.apiUrl, {}, {
headers: {
'Content-Type' : 'application/json',
'Accept' : 'audio/wav',
},
params: {
voice: 'en-US_AllisonV3Voice',
text : text
},
responseType: 'blob'
}).pipe(map(res => {
res.text().then((strBlob => {
localStorage.setItem(text, strBlob);
}))
return res;
}),catchError(this.handleError))
}
And this is my component function:
toSpeech() {
this.toSpeechService.getAudioFile(this.text).subscribe(res => {
console.log(res) // show nothing when blob is from localstorage
const EL_audio = document.querySelector("audio");
EL_audio.src = URL.createObjectURL(res);
EL_audio.load();
});
}
This code works fine when the date should be fetched from API but when is it read from localstorage, console.log(res)
shows nothing!