TTS

TTS(Text To Speech 文本轉(zhuǎn)語音縮寫)

簡(jiǎn)介

涉及到AVFoundatio框架中的AVSpeechSynthesizer、AVSpeechUtterance、AVSpeechSynthesisVoice三個(gè)類,這三個(gè)類的聲明都在AVSpeechSynthesis.h里,這三個(gè)類比較簡(jiǎn)單。

需要用到的一些枚舉與宏

  • 播放暫停時(shí)是立即停止播放語音還是播放完當(dāng)前詞再暫停。

    typedef NS_ENUM(NSInteger, AVSpeechBoundary) {
        AVSpeechBoundaryImmediate,
        AVSpeechBoundaryWord
    }
    
  • 語音質(zhì)量

    typedef NS_ENUM(NSInteger, AVSpeechSynthesisVoiceQuality) {
        AVSpeechSynthesisVoiceQualityDefault = 1,
        AVSpeechSynthesisVoiceQualityEnhanced
    }
    
  • 速率

    AVF_EXPORT const float AVSpeechUtteranceMinimumSpeechRate NS_AVAILABLE_IOS(7_0);
    AVF_EXPORT const float AVSpeechUtteranceMaximumSpeechRate NS_AVAILABLE_IOS(7_0);
    AVF_EXPORT const float AVSpeechUtteranceDefaultSpeechRate NS_AVAILABLE_IOS(7_0);
    
  • 注意:設(shè)置屬性字符串時(shí),AVSpeechSynthesisIPANotationAttribute的正確使用,最后會(huì)有Demo傳送門

    AVF_EXPORT NSString *const AVSpeechSynthesisIPANotationAttribute;
    

AVSpeechUtterance

要開始說話,請(qǐng)指定AVSpeechSynthesisVoice和要說出的字符串,然后根據(jù)需要隨意更改速率,音高或音量。

四種初始化方法

+ (instancetype)speechUtteranceWithString:(NSString *)string;
+ (instancetype)speechUtteranceWithAttributedString:(NSAttributedString *)string API_AVAILABLE(ios(10.0), watchos(3.0), tvos(10.0));

- (instancetype)initWithString:(NSString *)string;
- (instancetype)initWithAttributedString:(NSAttributedString *)string API_AVAILABLE(ios(10.0), watchos(3.0), tvos(10.0));

通過以上創(chuàng)建出來的實(shí)例,其語音、語速、音調(diào)、音量都是默認(rèn)的,可以通過屬性進(jìn)行定制,但是在入隊(duì)后設(shè)置這些值將不起作用。其中rate的值應(yīng)該介于AVSpeechUtteranceMinimumSpeechRate和AVSpeechUtteranceMaximumSpeechRate之間

@property(nonatomic, retain, nullable) AVSpeechSynthesisVoice *voice;
@property(nonatomic) float rate;           
@property(nonatomic) float pitchMultiplier;  // [0.5 - 2] Default = 1
@property(nonatomic) float volume;           // [0-1] Default = 1
@property(nonatomic) NSTimeInterval preUtteranceDelay;    // Default is 0.0
@property(nonatomic) NSTimeInterval postUtteranceDelay;   // Default is 0.0

有兩個(gè)屬性可以獲得當(dāng)前播放的字符串或?qū)傩宰址?/p>

@property(nonatomic, readonly) NSString *speechString;
@property(nonatomic, readonly) NSAttributedString *attributedSpeechString

AVSpeechSynthesisVoice

通過指定應(yīng)該在其中說出文本的語言代碼來檢索語音,或者通過使用voiceWithIdentifier來獲取已知語音標(biāo)識(shí)符。

現(xiàn)支持的語音語言37種,可以用類方法speechVoices獲取

+ (NSArray<AVSpeechSynthesisVoice *> *)speechVoices;
language name quality identifier
ar-SA Maged Default com.apple.ttsbundle.Maged-compact
cs-CZ Zuzana 同上 com.apple.ttsbundle.Zuzana-compact
ar-SA Maged 同上 com.apple.ttsbundle.Maged-compact
cs-CZ Zuzana 同上 com.apple.ttsbundle.Zuzana-compact
da-DK Sara 同上 com.apple.ttsbundle.Sara-compact
de-DE Anna 同上 com.apple.ttsbundle.Anna-compact
el-GR Melina 同上 com.apple.ttsbundle.Melina-compact
en-AU Karen 同上 com.apple.ttsbundle.Karen-compact
en-GB Daniel 同上 com.apple.ttsbundle.Daniel-compact
en-IE Moira 同上 com.apple.ttsbundle.Moira-compact
en-US Samantha 同上 com.apple.ttsbundle.Samantha-compact
en-ZA Tessa 同上 com.apple.ttsbundle.Tessa-compact
es-ES Monica 同上 com.apple.ttsbundle.Monica-compact
es-MX Paulina 同上 com.apple.ttsbundle.Paulina-compact
fi-FI Satu 同上 com.apple.ttsbundle.Satu-compact
fr-CA Amelie 同上 com.apple.ttsbundle.Amelie-compact
fr-FR Thomas 同上 com.apple.ttsbundle.Thomas-compact
he-IL Carmit 同上 com.apple.ttsbundle.Carmit-compact
hi-IN Lekha 同上 com.apple.ttsbundle.Lekha-compact
hu-HU Mariska 同上 com.apple.ttsbundle.Mariska-compact
id-ID Damayanti 同上 com.apple.ttsbundle.Damayanti-compact
it-IT Alice 同上 com.apple.ttsbundle.Alice-compact
ja-JP Kyoko 同上 com.apple.ttsbundle.Kyoko-compact
ko-KR Yuna 同上 com.apple.ttsbundle.Yuna-compact
nl-BE Ellen 同上 com.apple.ttsbundle.Ellen-compact
nl-NL Xander 同上 com.apple.ttsbundle.Xander-compact
no-NO Nora 同上 com.apple.ttsbundle.Nora-compact
pl-PL Zosia 同上 com.apple.ttsbundle.Zosia-compact
pt-BR Luciana 同上 com.apple.ttsbundle.Luciana-compact
pt-PT Joana 同上 com.apple.ttsbundle.Joana-compact
ro-RO Ioana 同上 com.apple.ttsbundle.Ioana-compact
ru-RU Milena 同上 com.apple.ttsbundle.Milena-compact
sk-SK Laura 同上 com.apple.ttsbundle.Laura-compact
sv-SE Alva 同上 com.apple.ttsbundle.Alva-compact
th-TH Kanya 同上 com.apple.ttsbundle.Kanya-compact
tr-TR Yelda 同上 com.apple.ttsbundle.Yelda-compact
zh-CN Ting-Ting 同上 com.apple.ttsbundle.Ting-Ting-compact
zh-HK Sin-Ji 同上 com.apple.ttsbundle.Sin-Ji-compact
zh-TW Mei-Jia 同上 com.apple.ttsbundle.Mei-Jia-compact

獲取設(shè)備當(dāng)前語言語音碼

+ (NSString *)currentLanguageCode;

根據(jù)指定的語言語音碼獲取語音

+ (nullable AVSpeechSynthesisVoice *)voiceWithLanguage:(nullable NSString *)languageCode;

根據(jù)指定的語言語音標(biāo)識(shí)碼獲取語音 iOS9.0之后可用

+ (nullable AVSpeechSynthesisVoice *)voiceWithIdentifier:(NSString *)identifier NS_AVAILABLE_IOS(9_0);

獲取某個(gè)語音的信息,只讀

@property(nonatomic, readonly) NSString *language;
@property(nonatomic, readonly) NSString *identifier NS_AVAILABLE_IOS(9_0);
@property(nonatomic, readonly) NSString *name NS_AVAILABLE_IOS(9_0);
@property(nonatomic, readonly) AVSpeechSynthesisVoiceQuality quality NS_AVAILABLE_IOS(9_0);

AVSpeechSynthesizer

合成器

是否正在播放、暫停

@property(nonatomic, readonly, getter=isSpeaking) BOOL speaking;
@property(nonatomic, readonly, getter=isPaused) BOOL paused;

將已加入或正在講話的相同AVSpeechUtterance加入會(huì)引發(fā)異常。

- (void)speakUtterance:(AVSpeechUtterance *)utterance;

以下方法只對(duì)正在講話的語音進(jìn)行操作才有效。 如果成功則返回YES,失敗則返回NO。

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

指定要用于合成語音的音頻通道,如AVAudioSession當(dāng)前路徑中的通道描述所述。
語音音頻將復(fù)制到每個(gè)指定的頻道。默認(rèn)值為nil,表示系統(tǒng)默認(rèn)值。

@property(nonatomic, retain, nullable) NSArray<AVAudioSessionChannelDescription *> *outputChannels;

學(xué)習(xí)資源

#import "ViewController.h"
#import <AVFoundation/AVFoundation.h>

@interface ViewController ()

@property(nonatomic, strong) AVSpeechSynthesizer *speechSynthesizer;
@property(nonatomic, strong) AVSpeechUtterance *utterance;
@property(nonatomic, strong) UILabel *label;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.speechSynthesizer = [[AVSpeechSynthesizer alloc] init];
    [self commonInit];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)commonInit {
    UISwitch *senderSwitch = [[UISwitch alloc] init];
    senderSwitch.on = YES;
    senderSwitch.frame = CGRectMake(0, 0, 100, 20);
    senderSwitch.center = self.view.center;
    [senderSwitch addTarget:self action:@selector(updateUtterance:) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:senderSwitch];
    
    self.label = [[UILabel alloc] initWithFrame:CGRectMake((self.view.bounds.size.width - 200)/2.0f, CGRectGetMaxY(senderSwitch.frame) - 80, 200, 20)];
    self.label.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:self.label];
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake((self.view.bounds.size.width - 128)/2.0f, CGRectGetMaxY(senderSwitch.frame) + 20, 128, 128);
    [btn setImage:[UIImage imageNamed:@"audio"] forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];
    
    [self updateUtterance:senderSwitch];
}

- (void)updateUtterance:(UISwitch *)sender {
    sender.on = !sender.isOn;
    if (sender.on) {
        NSMutableAttributedString *attString = [[NSMutableAttributedString alloc] initWithString:@"Tsutsumi"];
        // 這里的國際音標(biāo)可以隨意修改 eg:"t?n.t?n.m?"  "t?n.m?" ... 可以多試試其他的情況
        [attString addAttribute:AVSpeechSynthesisIPANotationAttribute value:@"t?n.t?n.mi" range:NSMakeRange(0, attString.length)];
        self.utterance = [AVSpeechUtterance speechUtteranceWithAttributedString:attString];
        
        self.label.text = @"Tsutsumi(Attributed)";
    } else {
        self.utterance = [AVSpeechUtterance speechUtteranceWithString:@"Tsutsumi"];
        self.label.text = @"Tsutsumi";
    }
}

- (void)btnClick:(UIButton *)sender {
    if (self.speechSynthesizer.isSpeaking) {
        NSLog(@"正在朗讀中....");
        return;
    }
    [self.speechSynthesizer speakUtterance:self.utterance];
}

@end

想看Demo的同學(xué)請(qǐng)移駕-->TTS

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 228,238評(píng)論 6 531
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 98,430評(píng)論 3 415
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 176,134評(píng)論 0 373
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)。 經(jīng)常有香客問我,道長(zhǎng),這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 62,893評(píng)論 1 309
  • 正文 為了忘掉前任,我火速辦了婚禮,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 71,653評(píng)論 6 408
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 55,136評(píng)論 1 323
  • 那天,我揣著相機(jī)與錄音,去河邊找鬼。 笑死,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,212評(píng)論 3 441
  • 文/蒼蘭香墨 我猛地睜開眼,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 42,372評(píng)論 0 288
  • 序言:老撾萬榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 48,888評(píng)論 1 334
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 40,738評(píng)論 3 354
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 42,939評(píng)論 1 369
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,482評(píng)論 5 359
  • 正文 年R本政府宣布,位于F島的核電站,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 44,179評(píng)論 3 347
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 34,588評(píng)論 0 26
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 35,829評(píng)論 1 283
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 51,610評(píng)論 3 391
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 47,916評(píng)論 2 372

推薦閱讀更多精彩內(nèi)容

  • 前言 在IOS7之前一般語音識(shí)別是靠一些第三方庫,iOS7之后,蘋果提供了文字轉(zhuǎn)語音的API可以使用。 導(dǎo)入框架 ...
    超_愛編程閱讀 5,765評(píng)論 5 2
  • 前言: 本文僅供學(xué)習(xí)之用,如果侵權(quán)請(qǐng)聯(lián)系刪除!文本轉(zhuǎn)語音技術(shù)也叫TTS,是Text To Speech的縮寫.iO...
    麥穗0615閱讀 6,315評(píng)論 1 9
  • 1、通過CocoaPods安裝項(xiàng)目名稱項(xiàng)目信息 AFNetworking網(wǎng)絡(luò)請(qǐng)求組件 FMDB本地?cái)?shù)據(jù)庫組件 SD...
    陽明先生_X自主閱讀 16,000評(píng)論 3 119
  • 項(xiàng)目中使用了TTS(語音合成功能)剛開始自己準(zhǔn)備使用科大訊飛的TTS SDK 但是公司經(jīng)過半天調(diào)研(省錢)決定使用...
    liliLearn閱讀 2,311評(píng)論 0 1
  • 這周,周一周二在上班,周三周四周五高考放假,周末補(bǔ)課。 周二上午十點(diǎn)左右,高三年級(jí)拍畢業(yè)照。我拿著微單,戴著帽子也...
    Ruth0419閱讀 203評(píng)論 0 0