[英]iOS Text To Speech Api
我似乎無法找到任何相關的東西。 在iOS7中是否有任何Siri類或API可以讓您進行文本到語音轉換? 我想要做的就是以下內容:
[siriInstance say:@"This is a test"];
然后讓Siri從我的應用程序中說出來。
看來我們應該有能力做到這一點,不是嗎? 看起來像是一件微不足道的事情。
從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")
然后說
在Swift 2 synthesizer.speakUtterance(utterance)
在Swift 3 synthesizer.speak(utterance)
不要忘記import AVFoundation
有用的方法
您可以使用以下兩種方法停止或暫停所有語音:
- (BOOL)pauseSpeakingAtBoundary:(AVSpeechBoundary)boundary;
- (BOOL)stopSpeakingAtBoundary:(AVSpeechBoundary)boundary;
AVSpeechBoundary
指示語音是應該暫停還是立即停止( AVSpeechBoundaryImmediate
),或者它應該在當前被說出的單詞之后暫停或停止( AVSpeechBoundaryWord
)。
這是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)
我從未做過專門針對Siri的任何工作。 我可能錯了,但我認為使用私有API很難與Siri集成。
我會看一下IOS的openears框架。 我過去曾做過一些基本的工作,它既可以進行離線語音識別,也可以進行合成語音/文本到語音轉換
希望這對你有所幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.