如何以编程方式使用 iOS 语音合成器?(文字转语音)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9939589/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 17:37:38  来源:igfitidea点击:

How to programmatically use iOS voice synthesizers? (text to speech)

iosaccessibilityvoiceaudio

提问by Dirty Henry

iOS devices have embedded voice synthesizers for Accessibility's VoiceOver feature. Is there a way you can use these synthesizers programmatically to generate text-based sounds?

iOS 设备为辅助功能的 VoiceOver 功能嵌入了语音合成器。有没有办法以编程方式使用这些合成器来生成基于文本的声音?

My problem is: I'm working on a simple app for kids to learn colors and rather than recording the names of the colors in each language i want to support and storing them as audio files, i'd rather generate the sounds at runtime with some text-to-speech feature.

我的问题是:我正在开发一个简单的应用程序,让孩子们学习颜色,而不是用我想要支持的每种语言记录颜色的名称并将它们存储为音频文件,我宁愿在运行时生成声音一些文字转语音功能。

Thanks

谢谢

[EDIT: this question was asked pre-iOS7 so you should really consider the voted answer and ignore older ones, unless you're a software archeologist]

[编辑:这个问题是在 iOS7 之前提出的,所以你应该真正考虑投票的答案并忽略旧的答案,除非你是软件考古学家]

回答by Onato

Starting from iOS 7, Apple provides thisAPI.

从 iOS 7 开始,Apple 提供了这个API。

See thisanswer.

看到这个答案。

Objective-C

目标-C

#import <AVFoundation/AVFoundation.h>
…
AVSpeechUtterance *utterance = [AVSpeechUtterance 
                            speechUtteranceWithString:@"Hello World!"];
AVSpeechSynthesizer *synth = [[AVSpeechSynthesizer alloc] init];
[synth speakUtterance:utterance];

Swift

迅速

import AVFoundation
…
let utterance = AVSpeechUtterance(string: "Hello World!")
let synth = AVSpeechSynthesizer()
synth.speakUtterance(utterance)

回答by user2518512

#import <AVFoundation/AVFoundation.h>

AVSpeechSynthesizer *av = [[AVSpeechSynthesizer alloc] init];
AVSpeechUtterance *utterance = [[AVSpeechUtterance alloc]initWithString:@"Text to say"]; 
[av speakUtterance:utterance];

回答by Alex Malko

This code worked for me with Swift and iOS 8 on both Simulator and iPhone 6. I needed to add the standard AVFoundation library:

这段代码适用于模拟器和 iPhone 6 上的 Swift 和 iOS 8。我需要添加标准的 AVFoundation 库:

import AVFoundation

// ...

func onSayMeSomething() {
    let utterance = AVSpeechUtterance(string: "Wow! I can speak!")
    utterance.pitchMultiplier = 1.3
    utterance.rate = AVSpeechUtteranceMinimumSpeechRate * 1.5
    let synth = AVSpeechSynthesizer()
    synth.speakUtterance(utterance)
}

回答by yuji

Unfortunately iOS doesn't expose a public API for programmatically generating speech.

不幸的是,iOS 没有公开用于以编程方式生成语音的公共 API。

There is a private APIyou can use, if you're not submitting to the App Store.

有一个私有API,你可以使用,如果你不提交到App Store。

Otherwise, see the responses to this questionfor a number of third-party libraries you can use.

否则,请参阅对此问题的回答,了解您可以使用的许多第三方库。

回答by Ocelot

you could find this helpful Making Your iPhone Application Accessible

您会发现这很有帮助使您的 iPhone 应用程序可访问

As stated in “iPhone Accessibility API and Tools,” standard UIKit controls and views are automatically accessible. If you use only standard UIKit controls, you probably don't have to do much additional work to make sure your application is accessible. In this case, your next step is to ensure that the default attribute information supplied by these controls makes sense in your application. To learn how to do this, see “Supply Accurate and Helpful Attribute Information.”

正如“iPhone 辅助功能 API 和工具”中所述,标准 UIKit 控件和视图可以自动访问。如果你只使用标准的 UIKit 控件,你可能不需要做很多额外的工作来确保你的应用程序是可访问的。在这种情况下,下一步是确保这些控件提供的默认属性信息在您的应用程序中有意义。要了解如何执行此操作,请参阅“提供准确且有用的属性信息”。

回答by Demz

You can try these third party APIs: iSpeechor OpenEars

您可以尝试这些第三方 API:iSpeechOpenEars