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 requirement he wants to speech HTML
file as he has many HTML
file in his DB
.
My Suggestion:
use
HTML
parsing and get all text from HTML and use same framework for Text2Speech.
But the client doesn't want that type of parsing and he wants any API
or framework which is providing directly HTML2Speech
feature.
Any suggestion or help will be highly appreciated.
There's two parts to a solution here...
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 anNSAttributedString
from the HTML, then ask that attributed string for its underlying plain-textstring
to pass text to the synthesizer.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
.One way or another it will always be parsing HTML to something else if you don't want to read files. If the client want direct
HTML2Speech
solution you can provide a method that takes html file as an argument and read it. What's happening with this file under the hood should not bother client that much as long as it's clean and not causing problems.What happen when client will ask for
Markdown2Speech
orXML2Speech
. For what i see in your desciption is better to have it for now in one framework with two public methodsText2Speech
andHTML2Speech
that will take as argument link to file or NSString.So as @rickster suggest it can be
NSAttributedString
orNSString
. There is a lot of parsers out there, Or if you want own solution you can remove everything what's inside<
and>
and change encoding.The safest method will be to extract the text and use existing text2speech API.
Though if you are sure that the browser will be chrome then Speech Synthesis API maybe helpful. But this API still not fully adopted by all browsers; it will be a risky solution.
You can find necessary info regarding this API at
There is no direct API for HTML to Speech except Speech Synthesis API mentioned above. Though you can try http://responsivevoice.org/. But I think this one is also based on browser's Speech Synthesis or Speech generation at server. So to use this one, you would have to extract text and pass the text to API to get the speech
As I have worked with HTML parsing and text2speech here you can go with 2 steps 1.get Attribute string from HTML file with below code works in
iOS7+
Step 1:
Then you can pass this Attributed String in
AVSpeechUtterance
Step 2: use below method to get HTML2String:
and as usual while you need to stop use below code to stop Speech.
Hope This will help you to get HTML2Speech feature.