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

Dir*_*nry 39 audio voice accessibility ios

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

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

谢谢

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

Ona*_*ato 62

从iOS 7开始,Apple提供 API.

看到这个答案.

Objective-C的

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

迅速

import AVFoundation
…
let utterance = AVSpeechUtterance(string: "Hello World!")
let synth = AVSpeechSynthesizer()
synth.speakUtterance(utterance)
Run Code Online (Sandbox Code Playgroud)


小智 11

#import <AVFoundation/AVFoundation.h>

AVSpeechSynthesizer *av = [[AVSpeechSynthesizer alloc] init];
AVSpeechUtterance *utterance = [[AVSpeechUtterance alloc]initWithString:@"Text to say"]; 
[av speakUtterance:utterance];
Run Code Online (Sandbox Code Playgroud)


小智 5

这段代码适用于Simulator和iPhone 6上的Swift和iOS8。我需要添加标准的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)
}
Run Code Online (Sandbox Code Playgroud)