是否有可能使“HTML转换为语音”就像“文字到语音”一样? [英] is it possible to make "HTML to speech" same like "Text to speech"?
问题描述
我有一个有线要求,在我退出的应用程序中,我有 Text2Speech
,为此我已经使用 AVSpeechSynthesizer
语音文本,但现在我的客户要求他希望发言 HTML
文件,因为他的 HTML
文件在他的 DB
。
我的建议:
使用
HTML
解析并获取HTML中的所有文本,并为Text2Speech使用相同的框架
。
但客户端不需要这种类型的解析,他希望任何 API
或直接提供的框架 HTML2Speech
功能。
任何建议或帮助将不胜感激。因为我已经使用HTML解析和text2speech在这里,你可以用两步走
1.get HTML文件中的属性字符串与下面的代码工作 iOS7 +
根据客户的观点:如果在市场上有任何API2Speech可能是其付费或
,那么如果您使用任何API,则依赖于该API。而原生框架
将有助于您的客户所需。 第1步
[[NSAttributedString alloc] initWithData:[htmlString dataUsingEncoding:NSUTF8StringEncoding]
options:@ {NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType,
NSCharacterEncodingDocumentAttribute:@(NSUTF8StringEncoding)}
documentAttributes:nil error:nil];
然后,您可以在
AVSpeechUtterance
第2步:
使用下面的方法获取HTML2String:/ **
*ConvertHTMLtoStrAndPlay:该方法将HTML转换为String
合成器。
*
* @param aURLHtmlFilePath:html文件路径的对象
* /
- (void)ConvertHTMLtoStrAndPlay :( UIButton *)aBtnPlayPause
isSpeechPaused:(BOOL )speechPaused
stringWithHTMLAttributes:(NSAttributedString *)aStrWithHTMLAttributes
{
if(synthesizer.speaking == NO&& speechPaused == NO){
AVSpeechUtterance * utterance = [[AVSpeechUtterance alloc] initWithString:aStrWithHTMLAttributes.string];
//utterance.rate = AVSpeechUtteranceMinimumSpeechRate;
if(IS_ARABIC){
utterance.voice = [AVSpeechSynthesisVoice voiceWithLanguage:@ar-au];
} else {
utterance.voice = [AVSpeechSynthesisVoice voiceWithLanguage:@en-au];
}
[合成器speakUtterance:utterance];
}
else {
[合成器pauseSpeakingAtBoundary:AVSpeechBoundaryImmediate];
}
if(speechPaused == NO){
[synthesizer continueSpeaking];
}其他{
[合成器pauseSpeakingAtBoundary:AVSpeechBoundaryImmediate];
}
}
和往常一样,您需要停止使用下面的代码停止演讲。
/ **
*StopPlayWithAVSpeechSynthesizer:此方法将停止播放的音频在应用程序上。
* /
- (void)StopPlayWithAVSpeechSynthesizer {
//加载视图后通常从笔尖执行任何其他设置。
[synthesizer stopSpeakingAtBoundary:AVSpeechBoundaryImmediate];
}
希望这可以帮助您获得HTML2Speech功能。
I have one wired requirement that in my exiting app I have
Text2Speech
and for that I have usedAVSpeechSynthesizer
to speech text, but now my client requirement he wants to speechHTML
file as he has manyHTML
file in hisDB
.My Suggestion :
use
HTML
parsing and get all text from HTML and use same framework for Text2Speech.But client don't want that type of parsing and he wants any
API
or framework which is providing directlyHTML2Speech
feature.Any suggestion or help will be highly appreciated.
解决方案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+
As per your client perspective : if there is any API in market for HTML2Speech may be its Paid or you are depended on that API if you use any. While Native framework will help same what you/client wants.
Step 1:
[[NSAttributedString alloc] initWithData:[htmlString dataUsingEncoding:NSUTF8StringEncoding] options:@{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType, NSCharacterEncodingDocumentAttribute: @(NSUTF8StringEncoding)} documentAttributes:nil error:nil];
Then you can pass this Attributed String in
AVSpeechUtterance
Step 2: use below method to get HTML2String:
/** * "ConvertHTMLtoStrAndPlay" : This method will convert the HTML to String synthesizer. * * @param aURLHtmlFilePath : "object of html file path" */ -(void)ConvertHTMLtoStrAndPlay:(UIButton*)aBtnPlayPause isSpeechPaused:(BOOL)speechPaused stringWithHTMLAttributes:(NSAttributedString*)aStrWithHTMLAttributes { if (synthesizer.speaking == NO && speechPaused == NO) { AVSpeechUtterance *utterance = [[AVSpeechUtterance alloc] initWithString:aStrWithHTMLAttributes.string]; //utterance.rate = AVSpeechUtteranceMinimumSpeechRate; if (IS_ARABIC) { utterance.voice = [AVSpeechSynthesisVoice voiceWithLanguage:@"ar-au"]; }else{ utterance.voice = [AVSpeechSynthesisVoice voiceWithLanguage:@"en-au"]; } [synthesizer speakUtterance:utterance]; } else{ [synthesizer pauseSpeakingAtBoundary:AVSpeechBoundaryImmediate]; } if (speechPaused == NO) { [synthesizer continueSpeaking]; } else { [synthesizer pauseSpeakingAtBoundary:AVSpeechBoundaryImmediate]; } }
and as usual while you need to stop use below code to stop Speech.
/** * "StopPlayWithAVSpeechSynthesizer" : this method will stop the playing of audio on the application. */ -(void)StopPlayWithAVSpeechSynthesizer{ // Do any additional setup after loading the view, typically from a nib. [synthesizer stopSpeakingAtBoundary:AVSpeechBoundaryImmediate]; }
Hope This will help you to get HTML2Speech feature.
这篇关于是否有可能使“HTML转换为语音”就像“文字到语音”一样?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!