【发布时间】:2021-11-07 16:26:28
【问题描述】:
这个函数应该从 localStorage 读取一个值或从 API 获取它并以两种方式返回一个 blob。
这是服务功能:
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))
}
这是我的组件函数:
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();
});
}
此代码在应从 API 获取日期但何时从本地存储读取时运行良好,console.log(res) 什么也不显示!
【问题讨论】:
-
您可能希望首先将您的 blob 数据转换为本地存储中的数据 uri,您可能需要检查这一点,link
-
试过了,没有任何区别! @NoxinDVictus
标签: angular observable blob