是否可以使 "HTML to speech" 与 "Text to speech" 相同?

Is it possible to make "HTML to speech" same like "Text to speech"?

我有一个奇怪的要求,在我现有的应用程序中我有 Text2Speech,为此,我使用 AVSpeechSynthesizer 来语音文本,但现在要求改变了,现在我需要转换 HTML 将数据文件转换成类似于 HTML2Speech.

的文本

我们可以想到的一个解决方案:

use HTML parsing and get all text from HTML and use same framework for Text2Speech.

但客户不想要那种类型的解析,他想要任何 API 或直接提供 HTML2Speech 功能的框架。

任何建议或帮助将不胜感激。

最安全的方法是提取文本并使用现有的 text2speech API。

尽管如果您确定浏览器 chrome 那么语音合成 API 可能会有帮助。但是这个 API 仍然没有被所有浏览器完全采用;这将是一个冒险的解决方案。

您可以在

找到有关此 API 的必要信息

除了上面提到的语音合成 API 之外,HTML 没有直接 API 到语音。尽管您可以尝试 http://responsivevoice.org/。但我认为这也是基于浏览器的语音合成或服务器端的语音生成。所以要使用这个,你必须提取文本并将文本传递给 API 以获得语音

此处的解决方案分为两部分...

  1. 想必您并不关心 HTML 中的格式——毕竟,当它到达语音合成器时,该文本将被说出,而不是被查看. AVSpeechSynthesizer 采用纯文本,因此您只需要摆脱 HTML 标记。一种简单的方法是创建一个 NSAttributedString from the HTML, then ask that attributed string for its underlying plain-text string 以将文本传递给合成器。

  2. 在 iOS10 中,您甚至不必从属性字符串中提取字符串 — 您可以传递属性字符串 directly to AVSpeechUtterance.

不管怎样,如果您不想读取文件,它总是会将 HTML 解析为其他内容。如果客户想要直接 HTML2Speech 解决方案,您可以提供一种方法,将 html 文件作为参数并读取它。只要它是干净的并且不会引起问题,这个文件在幕后发生的事情就不会打扰客户。

当客户要求 Markdown2SpeechXML2Speech 时会发生什么。因为我在你的描述中看到的是现在最好在一个框架中使用两个 public 方法 Text2SpeechHTML2Speech 作为参数 link 到文件或 NSString .

正如@rickster 建议的那样,它可以是 NSAttributedStringNSString。那里有很多解析器,或者如果你想要自己的解决方案,你可以删除 <> 中的所有内容并更改编码。

因为我在此处使用 HTML 解析和 text2speech,您可以执行 2 个步骤 1.get 来自 HTML 文件的属性字符串以及以下代码适用于 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.

第 1 步:

[[NSAttributedString alloc] initWithData:[htmlString dataUsingEncoding:NSUTF8StringEncoding] 
                                 options:@{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType,
                                           NSCharacterEncodingDocumentAttribute: @(NSUTF8StringEncoding)} 
                      documentAttributes:nil error:nil];

然后你可以在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];
    }

}

和往常一样,当您需要停止使用下面的代码来停止语音时。

/**
 *  "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];
}

希望这将帮助您获得 HTML2 语音功能。