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

标签 html ios text-to-speech

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

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

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

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

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

最佳答案

由于我在此处使用 HTML 解析和 text2speech,您可以执行 2 个步骤 1.使用以下代码从 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];
}

希望这将帮助您获得 HTML2Speech 功能。

关于html - 是否可以使 "HTML to speech"与 "Text to speech"相同?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39285959/

相关文章:

javascript - Node.js 上的文本到音频文件

css - 使用绝对定位使 div 居中

html - <textarea> 是否自动编码内部 html?

ios - iOS SDK 中新应用的应用商店指南

ios - 如何在 Objective-C 中使自定义类线程安全

C# 语音识别 - 这是用户所说的吗?

html - HTML 内衬问题中的元素

javascript - 一次滑动 2 个 div?

objective-c - 在应用内购买自动引用计算 : SKProductsRequestDelegate crashes

Android TTS男声