【问题标题】:iOS Text To Speech ApiiOS 文字转语音 API
【发布时间】:2014-04-29 00:10:30
【问题描述】:

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

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

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

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

【问题讨论】:

  • Siri 没有 API

标签: ios text-to-speech siri


【解决方案1】:

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

在目标 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

你也可以这样改变声音:

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 Doc

【讨论】:

【解决方案2】:

这是 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)    

【讨论】:

    【解决方案3】:

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

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

    希望this能帮到你。

    【讨论】:

    • 你能用 openears 添加新声音吗?
    【解决方案4】:

    Here你会发现一个基于this的文本到语音(TTS)示例应用程序(Objective-C)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-04-11
      • 2021-06-02
      • 2015-03-11
      • 1970-01-01
      • 2018-03-29
      • 2012-09-29
      • 1970-01-01
      相关资源
      最近更新 更多