简体   繁体   English

每个订阅之间的RxJS运行操作

[英]RxJS run operation between each subscribe

I've implemented an angular pipe which speaks out the text passed. 我实现了一个有角度的管道,可以说出所传递的文本。

I'm basically doing text to speech on a few sentences. 我基本上是用几个句子来做文字转语音。 The user needs to be able to see the sentence while the audio is playing... Not after the audio had finished (I have it currently like that). 用户需要能够在播放音频的同时看到句子……不是在音频播放完之后(我现在这样)。

Given an Observable of strings, how can I subscribe to each immediately, then play the audio until completion, before continuing with the rest of the strings? 给定一个可观察到的字符串,我该如何立即订阅每个字符串,然后播放音频直到完成,然后再继续其余字符串?

transform(input: Observable<string>, ...args) {
  if (input) {
    return input.concatMap(text => {

      if (!text)
        return Observable.empty()

      let promise = Promise.resolve()
      .then(() =>
        this.service.textToSpeech(text)
      )
      .then((audio) =>
        new Promise<string>((resolve) => {
          player.play(audio)
          player.addEventListener("ended", () => {
            resolve(text)  // The user sees the text when the audio stops..
          })
        })
      )

      return Observable.fromPromise(promise)

    })
  }
}

I solved my problem by zipping with an offset like this (where f(a) is the audio for text a ): 我通过用这样的偏移量压缩解决了我的问题(其中f(a)是文本a的音频):

a, null
b, f(a)
c, f(b)
null, f(c)

The code isn't pretty but it works... On the plus side, now the text-to-speech API requests aren't blocked by audio playback. 该代码不是很漂亮,但是可以正常工作……从好的方面来说,现在语音转换不会阻止文本到语音API请求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM