Cod*_*ger 19 html text-to-speech ios
我有一个奇怪的要求,在我现有的应用程序Text2Speech
中,我已经习惯AVSpeechSynthesizer
了语音文本,但现在我的客户要求他想要语音HTML
文件,因为他有很多HTML
文件DB
.
我的建议:
使用
HTML
解析并从HTML获取所有文本并使用Text2Speech的相同框架.
但客户端不希望这种类型的解析,他想要任何API
直接提供HTML2Speech
功能的框架.
任何建议或帮助将受到高度赞赏.
由于我在这里使用HTML解析和text2speech,你可以使用2步1.get HTML文件中的属性字符串,以下代码适用于 iOS7+
根据您的客户观点:如果HTML2Speech的市场中有任何API可能是其付费,或者如果您使用任何API,则依赖于该API.虽然Native框架可以帮助你/客户想要的.
步骤1:
[[NSAttributedString alloc] initWithData:[htmlString dataUsingEncoding:NSUTF8StringEncoding]
options:@{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType,
NSCharacterEncodingDocumentAttribute: @(NSUTF8StringEncoding)}
documentAttributes:nil error:nil];
Run Code Online (Sandbox Code Playgroud)
然后你可以传入这个Attributed String AVSpeechUtterance
第2步: 使用下面的方法获取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];
}
}
Run Code Online (Sandbox Code Playgroud)
和往常一样,你需要停止使用下面的代码停止语音.
/**
* "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];
}
Run Code Online (Sandbox Code Playgroud)
希望这将帮助您获得HTML2Speech功能.