• iOS自带TTS技术的实现即语音播报


    文本转语音技术, 也叫TTS, 是Text To Speech的缩写. iOS如果想做有声书等功能的时候, 会用到这门技术.

    一,使用iOS自带TTS需要注意的几点:

    1. iOS7之后才有该功能
    2. 需要 AVFoundation 库
    3. AVSpeechSynthesizer: 语音合成器, 可以假想成一个可以说话的人, 是最主要的接口
    4. AVSpeechSynthesisVoice: 可以假想成人的声音
    5. AVSpeechUtterance: 可以假想成要说的一段话

    二,代码示例, 播放语音

        //语音播报
        AVSpeechUtterance *utterance = [AVSpeechUtterance speechUtteranceWithString:@"床前明月光,疑是地上霜。"];
    
        utterance.pitchMultiplier=0.8;
        
        //中式发音
        AVSpeechSynthesisVoice *voice = [AVSpeechSynthesisVoice voiceWithLanguage:@"zh-CN"];
        //英式发音
    //    AVSpeechSynthesisVoice *voice = [AVSpeechSynthesisVoice voiceWithLanguage:@"en-GB"];
        
        utterance.voice = voice;
        
        NSLog(@"%@",[AVSpeechSynthesisVoice speechVoices]);
        
        AVSpeechSynthesizer *synth = [[AVSpeechSynthesizer alloc]init];
        
        [synth speakUtterance:utterance];

    三,AVSpeechSynthesizer介绍

    这个类就像一个会说话的人, 可以”说话”, 可以”暂停”说话, 可以”继续”说话, 可以判断他当前是否正在说话.有以下的方法或者属性:

    • 说话: speakUtterance
    • 控制: continueSpeaking(继续说), pauseSpeakingAtBoundary(暂停说话), paused(暂停状态的属性), speaking(说话的状态), stopSpeakingAtBoundary(停止说话)
    • 委托: delegate
    
    

    四,AVSpeechBoundary介绍

    这是一个枚举. 在暂停, 或者停止说话的时候, 停下的方式用这个枚举标示. 包括两种:

    • AVSpeechBoundaryImmediate: 立即停
    • AVSpeechBoundaryWord : 说完一个整词再停

    五,AVSpeechSynthesizerDelegate介绍

    合成器的委托, 对于一些事件, 提供了响应的接口.

    • didCancelSpeechUtterance: 已经取消说话
    • didContinueSpeechUtterance: 已经继续说话
    • didFinishSpeechUtterance: 已经说完
    • didPauseSpeechUtterance: 已经暂停
    • didStartSpeechUtterance:已经开始
    • willSpeakRangeOfSpeechString:将要说某段话

    六,AVSpeechSynthesisVoice介绍

    AVSpeechSynthesisVoice定义了一系列的声音, 主要是不同的语言和地区.

    • voiceWithLanguage: 根据制定的语言, 获得一个声音.
    • speechVoices: 获得当前设备支持的声音
    • currentLanguageCode: 获得当前声音的语言字符串, 比如”ZH-cn”
    • language: 获得当前的语言

    七,AVSpeechUtterance介绍

    这个类就是一段要说的话. 主要的属性和方法有:

    • pitchMultiplier: 音高
    • postUtteranceDelay: 读完一段后的停顿时间
    • preUtteranceDelay: 读一段话之前的停顿
    • rate: 读地速度, 系统提供了三个速度: AVSpeechUtteranceMinimumSpeechRate, AVSpeechUtteranceMaximumSpeechRate, AVSpeechUtteranceDefaultSpeechRate
    • speechString: 要读的字符串
    • voice: 使用的声音, 是AVSpeechSynthesisVoice对象
    • volume: 音量

    八,UML关系图

    这些类的关系如下:

  • 相关阅读:
    windows cmd 编码
    ARM伪指令
    System.load 和 System.loadLibrary
    用GDB调试程序
    ARM指令集
    ARM寻址方式
    abortion
    Oxford City Jealous Lover
    everyday words
    【转】高效率的C++函数返回值
  • 原文地址:https://www.cnblogs.com/luerniu/p/5901350.html
Copyright © 2020-2023  润新知