前言
最近在研究
AV Foundation框架 發(fā)現(xiàn)有一本書叫做
AV Foundation開發(fā)秘籍:實踐掌握iOS & OS X 應(yīng)用的視聽處理技術(shù)
然后google查了一下英文版叫
Learning AV Foundation: A Hands-on Guide to Mastering the AV Foundation Framework
看著國人的翻譯不僅慨嘆的想說一句話: 為啥不自己寫一本書 何必這么費勁翻譯它搞得原來很有技術(shù)含量 這么直譯就沒技術(shù)含量了??粗_發(fā)秘籍這名字不禁想起大學時那些書 從開發(fā)到入門… 21天學會xxx… 開發(fā)指南… 開發(fā)秘籍… 我大學讀的都是假書
今天給大家分享的是 iOS上如何 把漢字轉(zhuǎn)換成語音朗讀, 當然這個沒什么技術(shù)含量(大神可以飛過).
AVFoundation整體架構(gòu)
研究這個功能之前先介紹一下AV Foundation整體架構(gòu)

這是iOS上的架構(gòu)設(shè)計 (上圖)

這是macOS上的架構(gòu)設(shè)計(上圖)
看完之后我們就來用代碼實現(xiàn)這個demo
首先導入<AVFoundation/AVFoundation.h>
這我需要使用的是iOS上的AVSpeechSynthesizer,macOS上叫NSSpeechSynthesizer
@property (strong, nonatomic) AVSpeechSynthesizer *synthesizer;
AVSpeechSynthesizer 它的功能
- 將文字添加到語音, 就是用語音播放一段文字
初始化
- (void)awakeFromNib {
[super awakeFromNib];
//創(chuàng)建語音合成器
self.synthesizer = [[AVSpeechSynthesizer alloc] init];
self.synthesizer.delegate = self;
//播放的國家的語言
self.voices = @[[AVSpeechSynthesisVoice voiceWithLanguage:@"zh-CN"],[AVSpeechSynthesisVoice voiceWithLanguage:@"en-US"]
];
self.speechStrings = [[NSMutableArray alloc] init];
}
這里的[AVSpeechSynthesisVoice voiceWithLanguage:@"zh-CN"]
設(shè)置的是簡體中文語音 文章末尾會列出所有語音播放信息不用擔心寫錯.
AVSpeechSynthesizer的delegate方法如下 主要是對語音播放狀態(tài)的監(jiān)聽
@protocol AVSpeechSynthesizerDelegate <NSObject>
// 代理方法
@optional
// 開始播放 語音單元
- (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didStartSpeechUtterance:(AVSpeechUtterance *)utterance;
// 完成播放 語音單元
- (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didFinishSpeechUtterance:(AVSpeechUtterance *)utterance;
// 暫停播放 語音單元
- (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didPauseSpeechUtterance:(AVSpeechUtterance *)utterance;
// 繼續(xù)播放 語音單元
- (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didContinueSpeechUtterance:(AVSpeechUtterance *)utterance;
// 取消播放 語音單元
- (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didCancelSpeechUtterance:(AVSpeechUtterance *)utterance;
// 這里 指的是 又來監(jiān)聽 播放 字符范圍
- (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer willSpeakRangeOfSpeechString:(NSRange)characterRange utterance:(AVSpeechUtterance *)utterance;
@end
這里的AVSpeechSynthesizer主要的方法有
/* 添加 播放話語 到 播放語音 隊列, 可以設(shè)置utterance的屬性來控制播放 */
- (void)speakUtterance:(AVSpeechUtterance *)utterance;
// 對于 stopSpeakingAtBoundary: 語音單元的操作, 如果中斷, 會清空隊列
// 中斷
- (BOOL)stopSpeakingAtBoundary:(AVSpeechBoundary)boundary;
// 暫停
- (BOOL)pauseSpeakingAtBoundary:(AVSpeechBoundary)boundary;
// 繼續(xù)
- (BOOL)continueSpeaking;
這里我們用的
speakUtterance方法來播放文字
speakUtterance:(AVSpeechUtterance *)utterance
AVSpeechUtterance是對文字朗讀的封裝- 被播放的語音文字, 可以理解為一段需要播放的文字
這里我們設(shè)置AVSpeechUtterance朗讀播放的信息
//播放語音
NSArray *speechStringsArray = [self buildSpeechStrings]; //buildSpeechStrings播放字符串的數(shù)組
for (NSUInteger i = 0; i < speechStringsArray.count; i++) {
//創(chuàng)建AVSpeechUtterance 對象 用于播放的語音文字
AVSpeechUtterance *utterance = [[AVSpeechUtterance alloc] initWithString:speechStringsArray[i]];
//設(shè)置使用哪一個國家的語言播放
utterance.voice = self.voices[0];
//本段文字播放時的 語速, 應(yīng)介于AVSpeechUtteranceMinimumSpeechRate 和 AVSpeechUtteranceMaximumSpeechRate 之間
utterance.rate = 0.5;
//在播放特定語句時改變聲音的聲調(diào), 一般取值介于0.5(底音調(diào))~2.0(高音調(diào))之間
utterance.pitchMultiplier = 0.8f;
//聲音大小, 0.0 ~ 1.0 之間
utterance.volume = 1.0f;
//播放后的延遲, 就是本次文字播放完之后的停頓時間, 默認是0
utterance.preUtteranceDelay = 0;
//播放前的延遲, 就是本次文字播放前停頓的時間, 然后播放本段文字, 默認是0
utterance.postUtteranceDelay = 0.1f;
[self.synthesizer speakUtterance:utterance];
}
AVSpeechUtterance的屬性如下
// 設(shè)置使用哪一個國家的語言播放
@property(nonatomic, retain, nullable) AVSpeechSynthesisVoice *voice;
// 獲取當前需要播放的文字, 只讀屬性
@property(nonatomic, readonly) NSString *speechString;
// 獲取當前需要播放的文字 - 富文本, 只讀屬性, iOS10以后可用
@property(nonatomic, readonly) NSAttributedString *attributedSpeechString;
// 本段文字播放時的 語速, 應(yīng)介于AVSpeechUtteranceMinimumSpeechRate 和 AVSpeechUtteranceMaximumSpeechRate 之間
@property(nonatomic) float rate;
// 在播放特定語句時改變聲音的聲調(diào), 一般取值介于0.5(底音調(diào))~2.0(高音調(diào))之間
@property(nonatomic) float pitchMultiplier;
// 聲音大小, 0.0 ~ 1.0 之間
@property(nonatomic) float volume;
// 播放后的延遲, 就是本次文字播放完之后的停頓時間, 默認是0
@property(nonatomic) NSTimeInterval preUtteranceDelay;
// 播放前的延遲, 就是本次文字播放前停頓的時間, 然后播放本段文字, 默認是0
@property(nonatomic) NSTimeInterval postUtteranceDelay;
AVSpeechUtterance的方法如下
以下全部都是初始化方法, 分為 類方法 和 對象方法, 富文本的初始化方法要在iOS10以后才可以用
+ (instancetype)speechUtteranceWithString:(NSString *)string;
+ (instancetype)speechUtteranceWithAttributedString:(NSAttributedString *)string NS_AVAILABLE_IOS(10_0);
- (instancetype)initWithString:(NSString *)string;
- (instancetype)initWithAttributedString:(NSAttributedString *)string
可以使用[AVSpeechSynthesisVoice speechVoices]代碼打印出支持朗讀語言的國家
ar-SA 沙特阿拉伯(阿拉伯文)
en-ZA, 南非(英文)
nl-BE, 比利時(荷蘭文)
en-AU, 澳大利亞(英文)
th-TH, 泰國(泰文)
de-DE, 德國(德文)
en-US, 美國(英文)
pt-BR, 巴西(葡萄牙文)
pl-PL, 波蘭(波蘭文)
en-IE, 愛爾蘭(英文)
el-GR, 希臘(希臘文)
id-ID, 印度尼西亞(印度尼西亞文)
sv-SE, 瑞典(瑞典文)
tr-TR, 土耳其(土耳其文)
pt-PT, 葡萄牙(葡萄牙文)
ja-JP, 日本(日文)
ko-KR, 南朝鮮(朝鮮文)
hu-HU, 匈牙利(匈牙利文)
cs-CZ, 捷克共和國(捷克文)
da-DK, 丹麥(丹麥文)
es-MX, 墨西哥(西班牙文)
fr-CA, 加拿大(法文)
nl-NL, 荷蘭(荷蘭文)
fi-FI, 芬蘭(芬蘭文)
es-ES, 西班牙(西班牙文)
it-IT, 意大利(意大利文)
he-IL, 以色列(希伯萊文,阿拉伯文)
no-NO, 挪威(挪威文)
ro-RO, 羅馬尼亞(羅馬尼亞文)
zh-HK, 香港(中文)
zh-TW, 臺灣(中文)
sk-SK, 斯洛伐克(斯洛伐克文)
zh-CN, 中國(中文)
ru-RU, 俄羅斯(俄文)
en-GB, 英國(英文)
fr-FR, 法國(法文)
hi-IN 印度(印度文)
總結(jié)
為了學習AVFoundation我先從一個簡單的知識點入手,唯一覺得遺憾的是我不太確定是否這個合成器支持自定義語音朗讀;
最終的demo 支持iOS和macOS,如果需要Demo:加iOS高級技術(shù)交流群:624212887,獲取Demo,以及更多iOS學習資料
參考:
AV Foundation Apple 官方文檔
AVSpeechSynthesizer 和 AVSpeechUtterance
AVSpeechSynthesizer詳解
文章來源于網(wǎng)絡(luò),如有侵權(quán)請聯(lián)系小編刪除