我似乎找不到关于这个的任何信息。iOS7中有没有任何Siri类或API可以让您进行文本转语音?我只是想做类似以下的事情:
[siriInstance say:@"This is a test"];
然后让Siri从我的应用中说出它。
看起来我们应该能够做到这一点,是吗?似乎这是一件微不足道的事情。
我似乎找不到关于这个的任何信息。iOS7中有没有任何Siri类或API可以让您进行文本转语音?我只是想做类似以下的事情:
[siriInstance say:@"This is a test"];
然后让Siri从我的应用中说出它。
看起来我们应该能够做到这一点,是吗?似乎这是一件微不足道的事情。
自 iOS 7 开始,您将拥有一个新的 TTS Api。
在 Objective C 中
AVSpeechSynthesizer *synthesizer = [[AVSpeechSynthesizer alloc]init];
AVSpeechUtterance *utterance = [AVSpeechUtterance speechUtteranceWithString:@"Some text"];
[utterance setRate:0.2f];
[synthesizer speakUtterance:utterance];
在 Swift 中
let synthesizer = AVSpeechSynthesizer()
let utterance = AVSpeechUtterance(string: "Some text")
utterance.rate = 0.2
synthesizer.speak(utterance)
您也可以像这样更改语音:
utterance.voice = AVSpeechSynthesisVoice(language: "fr-FR")
然后说话
在Swift 2中:synthesizer.speakUtterance(utterance)
在Swift 3中:synthesizer.speak(utterance)
别忘了import AVFoundation
有用的方法
您可以使用这两个方法停止或暂停所有语音:
- (BOOL)pauseSpeakingAtBoundary:(AVSpeechBoundary)boundary;
- (BOOL)stopSpeakingAtBoundary:(AVSpeechBoundary)boundary;
AVSpeechBoundary
表示语音合成应该立即暂停或停止 (AVSpeechBoundaryImmediate
) 还是在当前正在说的单词后暂停或停止 (AVSpeechBoundaryWord
)。
请查阅AVSpeechSynthesizer文档。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)
我之前从未专门与Siri一起工作过。我可能错了,但我认为使用私有API集成Siri非常困难。
我建议您查看IOS的openears框架。我之前做过一些基本的工作,它可以进行离线语音识别和合成语音/文本到语音。
希望这个对您有所帮助。