我有一个奇怪的要求,在我现有的应用程序中,我有一个Text2Speech
,为此,我已经使用AVSpeechSynthesizer
语音文本,但现在的要求发生了变化,现在我需要将HTML文件数据转换为类似HTML2Speech
的文本。
我们可以想到的一个解决方案是:
使用
HTML
解析并从HTML中获取所有文本,并对Text2Speech使用相同的框架。
但是客户不想要这种类型的解析,他想要任何直接提供HTML2Speech
特性的API
或框架。
我们将非常感谢您的任何建议或帮助。
发布于 2016-09-05 18:32:25
由于我已经在这里使用过HTML解析和text2speech,所以您可以执行两个步骤:1.在iOS7+
中使用以下代码从HTML文件中获取属性字符串
根据您的客户角度,
:如果市场上有针对HTML2Speech的任何API,则可能是付费的,或者如果您使用任何API,则依赖于该API。而原生框架将帮助你/客户想要的东西。
第1步:
[[NSAttributedString alloc] initWithData:[htmlString dataUsingEncoding:NSUTF8StringEncoding]
options:@{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType,
NSCharacterEncodingDocumentAttribute: @(NSUTF8StringEncoding)}
documentAttributes:nil error:nil];
然后,您可以在AVSpeechUtterance
中传递此属性字符串
第二步:使用下面的方法获取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];
}
}
和往常一样,当你需要停止的时候,使用下面的代码来停止讲话。
/**
* "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];
}
希望这能帮助你获得HTML2Speech的特性。
发布于 2016-09-05 14:45:32
这里的解决方案有两个部分。
AVSpeechSynthesizer
接受纯文本,所以您只需要去掉HTML标记。一种简单的方法是创建一个NSAttributedString
from the HTML,然后请求其底层纯文本string
的属性字符串将文本传递给合成器。
AVSpeechUtterance
.发布于 2016-09-05 17:39:14
不管怎样,如果你不想读取文件,它总是会把HTML解析成其他的东西。如果客户想要直接的HTML2Speech
解决方案,你可以提供一个以html文件作为参数并读取它的方法。这个文件在幕后发生的事情应该不会给客户端带来太多麻烦,只要它是干净的,不会造成问题。
当客户端请求Markdown2Speech
或XML2Speech
时会发生什么情况。因为我在你的描述中看到的是,现在最好把它放在一个框架中,有两个公共方法Text2Speech
和HTML2Speech
,它们将作为参数链接到文件或NSString。
因此,正如@rickster建议的那样,它可以是NSAttributedString
或NSString
。有很多解析器,或者如果您想要自己的解决方案,您可以删除<
和>
中的所有内容并更改编码。
https://stackoverflow.com/questions/39285959
复制相似问题