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<NSSecureCoding>
         + (NSArray<AVSpeechSynthesisVoice *> *)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<NSCopying, NSSecureCoding>//语句
        + (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<AVSpeechSynthesizerDelegate> 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<AVAudioSessionChannelDescription *> *outputChannels API_AVAILABLE(ios(10.0), watchos(3.0), tvos(10.0));//指定输出频道 用于精确调整比如外放或是耳机。默认nil系统处理
     @end
    
  5. 代理(AVSpeechSynthesizerDelegate)

     @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;//已经停止
         - (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. 总结下来能够实现,完整句子的阅读。能够为每一句单独指定速度、音调、音量、音高、发音、前后等待或延迟时间。可以按照单词停止或是立即停止。将要读取某一段(无法指定大小)或是已经开始或是已经完成,或是已经停止已经继续或是已经取消都会有回调方法。无法导出音频文件。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 一、简介 <<UITableView(或简单地说,表视图)的一个实例是用于显示和编辑分层列出的信息的一种手段 <<...
    无邪8阅读 10,791评论 3 3
  • 前言 最先接触编程的知识是在大学里面,大学里面学了一些基础的知识,c语言,java语言,单片机的汇编语言等;大学毕...
    oceanfive阅读 3,192评论 0 7
  • 非常感谢大家利用自己宝贵的时间来阅读我的文章 , 上一篇文章写了Epub的加密一个实现方式,《ios Epub加密...
    筱贰笔阅读 4,105评论 24 9
  • 一、音频相关的iOS类库 1、音频相关的iOS类库 使用AVAudioSession负责调解APP和iOS系统里面...
    萧修阅读 645评论 0 0
  • 音频 主要的音频播放类是AudioToolbox.framework和AVFoundation.framework...
    月夜胜邪阅读 2,910评论 0 7