52

我似乎在这方面找不到任何东西。iOS7 中是否有任何 Siri 类或 API 可以让您将文本转语音?我要做的只是如下所示:

[siriInstance say:@"This is a test"];

然后让 Siri 从我的应用程序中说出来。

看来我们应该有能力做到这一点,不是吗?似乎是一件微不足道的事情。

4

4 回答 4

156

从 iOS 7 开始,您有了一个新的 TTS Api。

在目标 C

AVSpeechSynthesizer *synthesizer = [[AVSpeechSynthesizer alloc]init];
AVSpeechUtterance *utterance = [AVSpeechUtterance speechUtteranceWithString:@"Some text"];
[utterance setRate:0.2f];
[synthesizer speakUtterance:utterance];

在斯威夫特

let synthesizer = AVSpeechSynthesizer()
let utterance = AVSpeechUtterance(string: "Some text")
utterance.rate = 0.2

你也可以像这样改变声音:

utterance.voice = AVSpeechSynthesisVoice(language: "fr-FR")

然后说话

  • 在斯威夫特 2 synthesizer.speakUtterance(utterance)

  • 在斯威夫特 3 synthesizer.speak(utterance)

不要忘记import AVFoundation

有用的方法

您可以使用以下两种方法停止或暂停所有语音:

- (BOOL)pauseSpeakingAtBoundary:(AVSpeechBoundary)boundary;
- (BOOL)stopSpeakingAtBoundary:(AVSpeechBoundary)boundary;

AVSpeechBoundary指示语音是否应该立即暂停或停止 ( ) 或者它应该在AVSpeechBoundaryImmediate当前正在说出的单词之后暂停或停止 ( AVSpeechBoundaryWord)。

检查AVSpeechSynthesizer 文档

于 2014-03-21T15:09:15.957 回答
9

这是 Ali ABBAS 在操场上使用的答案:

import UIKit
import AVKit
import AVFoundation
import PlaygroundSupport

var str = "Hello, playground"

let synthesizer = AVSpeechSynthesizer()
let utterance = AVSpeechUtterance(string: str)
utterance.rate = 0.4
utterance.voice = AVSpeechSynthesisVoice(language: "en-US")

//for playground only
let playerViewController = AVPlayerViewController()
PlaygroundPage.current.liveView = playerViewController.view
//

synthesizer.speak(utterance)    
于 2017-06-19T23:56:25.627 回答
3

我从未专门针对 Siri 做过任何工作。我可能错了,但我认为使用私有 API 与 Siri 集成非常困难。

我会看看 IOS 的 openears 框架。我过去做过一些基本的工作,它既可以进行离线语音识别,也可以进行合成语音/文本到语音

希望对您有所帮助。

于 2014-03-21T14:50:31.710 回答
1

在这里,您将找到基于的文本转语音 (TTS) 示例应用程序 (Objective-C)

于 2016-08-04T09:12:35.720 回答