AVSpeechSynthesizer是屬于AVFounztion框架里面的AVFAudio子框架的一個類, 主要的功能是把文本內(nèi)容通過系統(tǒng)語音讀取出來慌核, 支持的多國語言绵载, 中文支持普通話和粵語蛔翅。
主要的類有:
AVSpeechSynthesizer
: 可以理解為說讀器门驾,為主要類
AVSpeechSynthesisVoice
: 讀取文本要用什么語言說出來(輔助類)
AVSpeechUtterance
: 這個可以理解為每一段話的信息設(shè)置(輔助類)
AVSpeechSynthesisVoice
這個主要是為每一段文本設(shè)置的語言, 例如: 一篇文章的不同句子或段落我可以用不同的語言讀出來娃善, 中文的就可以設(shè)置為普通話或者粵語醋旦。
方法
+ (NSArray<AVSpeechSynthesisVoice *> *)speechVoices; //系統(tǒng)支持讀取文本播放的語言
+ (NSString *)currentLanguageCode;//獲取當(dāng)前手機的語言
//根據(jù)上面兩個方法獲取到支持的語言來創(chuàng)建一個語言對象
+ (nullable AVSpeechSynthesisVoice *)voiceWithLanguage:(nullable NSString *)languageCode;
+ (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);
AVSpeechUtterance
每一段的播放文本信息設(shè)置,
/***需要讀的文字?jǐn)?shù)組***/
-(NSArray *)defaultStringNeedToSpeechArray{
_stringNeedToSpeechArray = @[@"你這個傻逼",
@"ZFPlayer是對AVPlayer的封裝会放,有人會問它支持什么格式的視頻播放饲齐,問這個問題的可以自行搜索AVPlayer支持的格式",
@"Are you excited about the book?",
@"Very! I have always felt so misunderstood.",
@"What's your favorite feature?",
@"Oh, they're all my babies. I couldn't possibly choose.",
@"It was great to speak with you!",
@"The pleasure was all mine! Have fun!",
@"Hello AV Foundation. How are you?",
@"I'm well! Thanks for asking.",
@"Are you excited about the book?",
@"Very! I have always felt so misunderstood.",
@"What's your favorite feature?",
@"Oh, they're all my babies. I couldn't possibly choose.",
@"It was great to speak with you!",
@"The pleasure was all mine! Have fun!"];;
return _stringNeedToSpeechArray;
}
-(void)play {
for (NSUInteger i = 0; i < self.stringNeedToSpeechArray.count; i++) {
AVSpeechUtterance *utterance = [AVSpeechUtterance speechUtteranceWithString:self.stringNeedToSpeechArray[i]];
//zh-CN 普通話, zh-HK粵語 [AVSpeechSynthesisVoice currentLanguageCode] 根據(jù)當(dāng)前的語言設(shè)置來讀
utterance.voice = [AVSpeechSynthesisVoice voiceWithLanguage:@"zh-HK"];
utterance.volume = 1.0; //音量 0-1咧最;
utterance.rate = 0.5f; //閱讀的速率 0-1
utterance.pitchMultiplier = 0.8; ////音調(diào)捂人, 默認(rèn)為1.0 , 取值范圍為0.5 - 2.0
utterance.postUtteranceDelay = 0.1f; //在讀下一句的時候矢沿, 延時0.1s
[self.speechSynthesizer speakUtterance:utterance];//開始播放
}
}
AVSpeechSynthesizer
這個類主要控制文本的讀取滥搭, 暫停, 停止
-(void)puse {
if ([self.speechSynthesizer pauseSpeakingAtBoundary:AVSpeechBoundaryImmediate]) {
NSLog(@"成功暫停");
}else {
NSLog(@"暫停失敗");
}
}
-(void)stop {
//停止讀音捣鲸,傳入的參數(shù) 立刻停止還是讀完一個單詞停止
if( [self.speechSynthesizer stopSpeakingAtBoundary:AVSpeechBoundaryWord]){
NSLog(@"成功停止");
}else {
NSLog(@"停止失敗");
}
}
-(void)contiune {
if( [self.speechSynthesizer continueSpeaking]){
NSLog(@"成功播放");
}else {
NSLog(@"播放失敗");
}
}
它的代理也比較簡單
-(void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didStartSpeechUtterance:(AVSpeechUtterance *)utterance {
//每一個utterance對象讀取之前回調(diào)
NSLog(@"%s", __func__);
}
-(void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didPauseSpeechUtterance:(AVSpeechUtterance *)utterance {
NSLog(@"%s", __func__);
}
-(void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didCancelSpeechUtterance:(AVSpeechUtterance *)utterance {
NSLog(@"%s", __func__);
}
-(void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didContinueSpeechUtterance:(AVSpeechUtterance *)utterance {
NSLog(@"%s", __func__);
}
-(void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didFinishSpeechUtterance:(AVSpeechUtterance *)utterance {
//每一個utterance對象讀取完之后回調(diào)
NSLog(@"%s", __func__);
}
-(void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer willSpeakRangeOfSpeechString:(NSRange)characterRange utterance:(AVSpeechUtterance *)utterance {
//根據(jù)讀取的范圍瑟匆, 或者讀取的文字內(nèi)容做自定義操作
NSLog(@"%@---", synthesizer.outputChannels);
}