iOS 文本到语音 Api

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22561926/
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 23:47:15  来源:igfitidea点击:

iOS Text To Speech Api

iostext-to-speechsiri

提问by Jesse

I can't seem to find anything on this. Are there any Siri classes or API's in iOS7 that let you do text to speech? All I am trying to do is something like the following:

我似乎无法找到任何关于此的信息。iOS7 中是否有任何 Siri 类或 API 可以让您进行文本到语音的转换?我想要做的就是以下内容:

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

And then have Siri say it from my app.

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

It seems we should be capable of doing this, no? Seems like a trivial thing.

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

回答by Ali Abbas

Since iOS 7 you have a new TTS Api.

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

In Objective C

在目标 C

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

In Swift

在斯威夫特

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

You can also change the voice like this :

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

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

And then speek

然后说话

  • In Swift 2synthesizer.speakUtterance(utterance)

  • In Swift 3synthesizer.speak(utterance)

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

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

Don't forget to import AVFoundation

不要忘记 import AVFoundation

Helpful methods

有用的方法

You can Stop or Pause all speech using these two methods :

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

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

The AVSpeechBoundaryindicates if the speech should pause or stop immediately (AVSpeechBoundaryImmediate) or it should pause or stop after the word currently being spoken (AVSpeechBoundaryWord).

AVSpeechBoundary指示如果语音应暂停或停止立即(AVSpeechBoundaryImmediate)或字当前正在发言后应暂停或停止(AVSpeechBoundaryWord)。

Check the AVSpeechSynthesizer Doc

检查AVSpeechSynthesizer 文档

回答by Matt Bearson

This is Ali ABBAS' answer for use in a playground:

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

回答by Tim

I have never done any work specifically with Siri. I may be wrong, but I think integrating with Siri is very difficult using private API's.

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

I would take a look at the openears framework for IOS. I have done some basic work with this in the past and it does both offline speech recognition and synthesized speech/text-to-speech

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

Hope thishelps you.

希望对你有帮助。

回答by d1jhoni1b

Hereyou will find a text to speech (TTS) sample app (Objective-C) based on this

在这里,你会发现基于文本到语音转换(TTS)的示例应用程序(Objective-C的),