Is it possible to make “HTML to speech” same like “Text to speech”?

后端 未结 4 823
余生分开走
余生分开走 2021-02-12 14:13

I have one weird requirement that in my existing app I have Text2Speech and for that, I have used AVSpeechSynthesizer to speech text, but now my client

4条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-02-12 14:54

    There's two parts to a solution here...

    1. Presumably you don't care about the formatting in the HTML--after all, by the time it gets to the speech synthesizer, this text is to be spoken, not viewed. AVSpeechSynthesizer takes plain text, so you just need to get rid of the HTML markup. One easy way to do that is to create an NSAttributedString from the HTML, then ask that attributed string for its underlying plain-text string to pass text to the synthesizer.

    2. In iOS 10 you don't even have to extract the string from an attributed string — you can pass an attributed string directly to AVSpeechUtterance.

提交回复
热议问题