iOS 语音合成

  1. 文字阅读例子

    AVSpeechSynthesizer * synthesizer = [[AVSpeechSynthesizer alloc] init];//语音合成器
    AVSpeechUtterance *utterance = [[AVSpeechUtterance alloc] initWithString:@"string1"];//说话
    utterance.voice = [AVSpeechSynthesisVoice voiceWithLanguage:@"en-US"];//发音
    utterance.rate = 0.5f;//速率
    utterance.pitchMultiplier = 0.8f;//音调
    utterance.postUtteranceDelay = 0.1f;//延迟
    [synthesizer speakUtterance:utterance];//添加到语音合成器,不会等待,程序继续执行 此时语音开始合成
    
    AVSpeechUtterance *utterance2 = [[AVSpeechUtterance alloc] initWithString:@"string2"];
    utterance2.voice = [AVSpeechSynthesisVoice voiceWithLanguage:@"en-GB"];
    utterance2.rate = 0.5f;
    utterance2.pitchMultiplier = 0.8f;
    utterance2.postUtteranceDelay = 0.1f;
    [synthesizer speakUtterance:utterance2];//此时会等第一句结束后才会播放这句。程序不会阻塞。
    
  2. 发音(AVSpeechSynthesisVoice)

     @interface AVSpeechSynthesisVoice : NSObject
         + (NSArray *)speechVoices;//系统支持的列表
         + (NSString *)currentLanguageCode;//系统当前语言代码
         + (nullable AVSpeechSynthesisVoice *)voiceWithLanguage:(nullable NSString *)languageCode;//通过语言代码创建实例  使用BCP-47语言标记
         + (nullable AVSpeechSynthesisVoice *)voiceWithIdentifier:(NSString *)identifier NS_AVAILABLE_IOS(9_0);//通过identifier创建实例
         @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);
    
     @end
    
  3. 语句(AVSpeechUtterance)

    @interface AVSpeechUtterance : NSObject//语句
        + (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));
    
        @property(nonatomic, retain, nullable) AVSpeechSynthesisVoice *voice;//发音
        @property(nonatomic, readonly) NSString *speechString;//阅读的文字
        @property(nonatomic, readonly) NSAttributedString *attributedSpeechString API_AVAILABLE(ios(10.0), watchos(3.0), tvos(10.0));//阅读的属性字符串
        @property(nonatomic) float rate;             // 速率 AVSpeechUtteranceMinimumSpeechRate and AVSpeechUtteranceMaximumSpeechRate.
        @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  之后等待
    @end
    
  4. 语音合成器(AVSpeechSynthesizer)

     @interface AVSpeechSynthesizer : NSObject
         @property(nonatomic, weak, nullable) id delegate;
         @property(nonatomic, readonly, getter=isSpeaking) BOOL speaking;//状态 是否在阅读
         @property(nonatomic, readonly, getter=isPaused) BOOL paused;//状态  是否暂停
         - (void)speakUtterance:(AVSpeechUtterance *)utterance; //utterance默认进入队列。加入同一个在队里的utterace会异常
         - (BOOL)stopSpeakingAtBoundary:(AVSpeechBoundary)boundary;//停止并清空阅读队列。YES成功
         - (BOOL)pauseSpeakingAtBoundary:(AVSpeechBoundary)boundary;//暂停阅读。AVSpeechBoundaryImmediate 立即停止,AVSpeechBoundaryWord 按单词停止(把最后一个词读完)
         - (BOOL)continueSpeaking;//继续
         @property(nonatomic, retain, nullable) NSArray *outputChannels API_AVAILABLE(ios(10.0), watchos(3.0), tvos(10.0));//指定输出频道 用于精确调整比如外放或是耳机。默认nil系统处理
     @end
    
  5. 代理(AVSpeechSynthesizerDelegate)

     @protocol AVSpeechSynthesizerDelegate 
         @optional
         - (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didStartSpeechUtterance:(AVSpeechUtterance *)utterance;//已经开始
         - (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didFinishSpeechUtterance:(AVSpeechUtterance *)utterance;//已经完成
         - (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didPauseSpeechUtterance:(AVSpeechUtterance *)utterance;//已经停止
         - (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didContinueSpeechUtterance:(AVSpeechUtterance *)utterance;//已经继续
         - (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didCancelSpeechUtterance:(AVSpeechUtterance *)utterance;//已经取消
         - (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer willSpeakRangeOfSpeechString:(NSRange)characterRange utterance:(AVSpeechUtterance *)utterance;//将要开始读某一段文字
     @end
    
  6. 总结下来能够实现,完整句子的阅读。能够为每一句单独指定速度、音调、音量、音高、发音、前后等待或延迟时间。可以按照单词停止或是立即停止。将要读取某一段(无法指定大小)或是已经开始或是已经完成,或是已经停止已经继续或是已经取消都会有回调方法。无法导出音频文件。

你可能感兴趣的:(iOS 语音合成)