iOS 文本轉語音AVSpeechSynthesizer

AVSpeechSynthesizer是屬于AVFounztion框架里面的AVFAudio子框架的一個類, 主要的功能是把文本內容通過系統語音讀取出來, 支持的多國語言, 中文支持普通話和粵語。

主要的類有:
AVSpeechSynthesizer: 可以理解為說讀器,為主要類
AVSpeechSynthesisVoice : 讀取文本要用什么語言說出來(輔助類)
AVSpeechUtterance : 這個可以理解為每一段話的信息設置(輔助類)

AVSpeechSynthesisVoice

這個主要是為每一段文本設置的語言, 例如: 一篇文章的不同句子或段落我可以用不同的語言讀出來, 中文的就可以設置為普通話或者粵語。

方法

+ (NSArray<AVSpeechSynthesisVoice *> *)speechVoices; //系統支持讀取文本播放的語言
+ (NSString *)currentLanguageCode;//獲取當前手機的語言

//根據上面兩個方法獲取到支持的語言來創建一個語言對象
+ (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

每一段的播放文本信息設置,

/***需要讀的文字數組***/
-(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] 根據當前的語言設置來讀
        utterance.voice = [AVSpeechSynthesisVoice voiceWithLanguage:@"zh-HK"];
        utterance.volume = 1.0; //音量 0-1;
        utterance.rate = 0.5f; //閱讀的速率 0-1
        utterance.pitchMultiplier = 0.8; ////音調, 默認為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 {
    //停止讀音,傳入的參數 立刻停止還是讀完一個單詞停止
    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對象讀取之前回調
    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對象讀取完之后回調
    NSLog(@"%s", __func__);

}

-(void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer willSpeakRangeOfSpeechString:(NSRange)characterRange utterance:(AVSpeechUtterance *)utterance {
  //根據讀取的范圍, 或者讀取的文字內容做自定義操作
    NSLog(@"%@---", synthesizer.outputChannels);
}
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容