2012-03-30 74 views
38

iOS設備爲嵌入式語音合成器提供了輔助功能的VoiceOver功能。有沒有一種方法可以通過編程方式使用這些合成器來生成基於文本的聲音?如何以編程方式使用iOS語音合成器? (文本到語音)

我的問題是:我正在一個簡單的應用程序,爲孩子們學習顏色,而不是記錄的顏色的名字在我想要支持的每種語言,並將其保存爲音頻文件,我寧願生成在運行時發出一些文本到語音的功能。

感謝

[編輯:這個問題被問前iOS7所以你真的應該考慮表決的答案而忽略舊的,除非你是一個軟件考古學家]

回答

61

從iOS的7起,蘋果提供this API 。

this答案。

Objective-C的

#import <AVFoundation/AVFoundation.h> 
… 
AVSpeechUtterance *utterance = [AVSpeechUtterance 
          speechUtteranceWithString:@"Hello World!"]; 
AVSpeechSynthesizer *synth = [[AVSpeechSynthesizer alloc] init]; 
[synth speakUtterance:utterance]; 

斯威夫特

import AVFoundation 
… 
let utterance = AVSpeechUtterance(string: "Hello World!") 
let synth = AVSpeechSynthesizer() 
synth.speakUtterance(utterance) 
+0

我們可以將說出的聲音保存爲mp3文件嗎? – 2016-07-01 10:51:18

+0

@Onato如何停止在文本到語音。? – ShreePool 2017-03-29 11:48:00

4

不幸的是iOS不公開一個用於編程生成語音的公共API。

有一個private API你可以使用,如果你不提交到App Store。

否則,看到一個數字,你可以使用第三方庫來this question響應。

+7

讓我們希望蘋果的人都會過來這裏看這是一個「請,讓最API公開」的要求;) – 2012-03-30 09:10:43

+1

蘋果並沒有理會,甚至直接發佈到他們的要求,所以我不會期望太多:( – MrTJ 2012-03-30 11:09:33

+7

事實上,蘋果公司做到了。 :) – 2013-09-19 10:32:42

0

你能有所幫助Making Your iPhone Application Accessible

如表示:「iPhone輔助功能的API和工具,」標準的UIKit控件和視圖自動訪問。如果您僅使用標準的UIKit控件,則可能不需要做太多的額外工作來確保您的應用程序可訪問。在這種情況下,下一步是確保這些控件提供的默認屬性信息在您的應用程序中有意義。 「提供準確和有用的屬性信息」要了解如何做到這一點,看到

11
#import <AVFoundation/AVFoundation.h> 

AVSpeechSynthesizer *av = [[AVSpeechSynthesizer alloc] init]; 
AVSpeechUtterance *utterance = [[AVSpeechUtterance alloc]initWithString:@"Text to say"]; 
[av speakUtterance:utterance]; 
+0

我們可以將發音保存爲mp3文件嗎? – 2016-07-01 10:51:48

4

此代碼爲我工作用斯威夫特和iOS 8兩個模擬器和iPhone 6.我需要添加標準AVFoundation庫:

import AVFoundation 

// ... 

func onSayMeSomething() { 
    let utterance = AVSpeechUtterance(string: "Wow! I can speak!") 
    utterance.pitchMultiplier = 1.3 
    utterance.rate = AVSpeechUtteranceMinimumSpeechRate * 1.5 
    let synth = AVSpeechSynthesizer() 
    synth.speakUtterance(utterance) 
} 
相關問題