TTS

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

簡介

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

需要用到的一些枚舉與宏

  • 播放暫停時是立即停止播放語音還是播放完當前詞再暫停。

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

    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);
    
  • 注意:設置屬性字符串時,AVSpeechSynthesisIPANotationAttribute的正確使用,最后會有Demo傳送門

    AVF_EXPORT NSString *const AVSpeechSynthesisIPANotationAttribute;
    

AVSpeechUtterance

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

四種初始化方法

+ (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));

通過以上創建出來的實例,其語音、語速、音調、音量都是默認的,可以通過屬性進行定制,但是在入隊后設置這些值將不起作用。其中rate的值應該介于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

有兩個屬性可以獲得當前播放的字符串或屬性字符串

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

AVSpeechSynthesisVoice

通過指定應該在其中說出文本的語言代碼來檢索語音,或者通過使用voiceWithIdentifier來獲取已知語音標識符。

現支持的語音語言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

獲取設備當前語言語音碼

+ (NSString *)currentLanguageCode;

根據指定的語言語音碼獲取語音

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

根據指定的語言語音標識碼獲取語音 iOS9.0之后可用

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

獲取某個語音的信息,只讀

@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加入會引發異常。

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

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

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

指定要用于合成語音的音頻通道,如AVAudioSession當前路徑中的通道描述所述。
語音音頻將復制到每個指定的頻道。默認值為nil,表示系統默認值。

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

學習資源

#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"];
        // 這里的國際音標可以隨意修改 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的同學請移駕-->TTS

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

推薦閱讀更多精彩內容

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