AVSpeechSynthesizer ios 原生语音合成与播放

AVSpeechSynthesizer

1. 现代语言翻译的基础:将所说的单词转录成文本,将文本翻译成目标语言,并为所翻译的文本合成语音。

2. AvspeechSynther 在iOS 7中引入,在MacOS 10.14 Mojave中可用

3.Voiceover支持30多种语言。 有关可用内容的最新列表,请调用avspeechSynthesVoice类方法speechVoices() 默认情况下,AvSpeech合成器将使用基于用户当前语言首选项的语音进行讲话。

4.https://www.jianshu.com/c/f4d8d50984d8  《AVFoundation》专辑 待深入了解

5. 核心的部件就是AVSpeechSynthesizer

6.基础和其他系统框架中的许多API使用ISO 681代码来识别语言。然而,avspeechSynthesVoice采用了指定的bcp 47文档系列的IETF语言标记。如果一个发声串和声音不是同一种语言,那么语音合成就失败了。

7. 并不是所有的语言都预先加载到设备上,在合成语音之前,可能需要在后台下载。


看一下 AVFundtion中 AVFAudo 中的 AVSpeechSynthesis.h

1.首先是枚举

typedefNS_ENUM(NSInteger, AVSpeechBoundary) {

    AVSpeechBoundaryImmediate,

    AVSpeechBoundaryWord

} NS_ENUM_AVAILABLE(10_14, 7_0);

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

AVSpeechBoundaryImmediate: 立即停

AVSpeechBoundaryWord : 说完一个整词再停

2. 语音质量

typedefNS_ENUM(NSInteger, AVSpeechSynthesisVoiceQuality) {

    AVSpeechSynthesisVoiceQualityDefault =1,

    AVSpeechSynthesisVoiceQualityEnhanced

} NS_ENUM_AVAILABLE(10_14, 9_0);

AVSpeechSynthesisVoiceQualityDefault  语音质量默认值

AVSpeechSynthesisVoiceQualityEnhanced 语音质量增强

3. 声明。播放语音时的速率

AVF_EXPORT const float AVSpeechUtteranceMinimumSpeechRate API_AVAILABLE(ios(7.0), watchos(1.0), tvos(7.0), macos(10.14));  

 播放语音时的最小速率

AVF_EXPORT const float AVSpeechUtteranceMaximumSpeechRate API_AVAILABLE(ios(7.0), watchos(1.0), tvos(7.0), macos(10.14));

播放语音时的最大速率

AVF_EXPORT const float AVSpeechUtteranceDefaultSpeechRate API_AVAILABLE(ios(7.0), watchos(1.0), tvos(7.0), macos(10.14));

播放语音时的默认速率

-----------

AVF_EXPORT NSString*const AVSpeechSynthesisVoiceIdentifierAlex API_AVAILABLE(ios(9.0), watchos(2.0), tvos(9.0), macos(10.14));

 用于初始化声音语言,仅仅代表 en-US

-----------

AVF_EXPORTN SString*const AVSpeechSynthesisIPANotationAttribute API_AVAILABLE(ios(10.0), watchos(3.0), tvos(10.0), macos(10.14));

用于包含国际音标(ipa)符号。控制某个词或短语的发音,例如正确的名称。

AVSpeechSynthesizerDelegate

@interface AVSpeechSynthesisVoice :NSObject<NSSecureCoding>


AVSpeechSynthesisVoice 封装了用于在系统上合成语音的语音属性。

+ (NSArray<AVSpeechSynthesisVoice *> *)speechVoices;

可以 通过 NSLog(@"AVSpeechSynthesisVoice所有语言的最新列表 %@",AVSpeechSynthesisVoice.speechVoices);

+ (NSString*)currentLanguageCode;

获取设备当前语言语音码

+ (nullable AVSpeechSynthesisVoice*)voiceWithLanguage:(nullable NSString*)languageCode;

设置语言

+ (nullable AVSpeechSynthesisVoice*)voiceWithIdentifier:(NSString*)identifierAPI_AVAILABLE(ios(9.0), watchos(2.0), tvos(9.0), macos(10.14));

通过标识符检索获取语音,传递无效的标识符将返回nil。如果标识符有效,但语音在设备上不可用(即用户尚未下载),则返回nil。

@interface AVSpeechUtterance : NSObject<NSCopying, NSSecureCoding>

要开始讲话,请指定要讲话的AvSpeechSynthesVoice和字符串,然后根据需要更改速率、音调或音量。

+ (instancetype)speechUtteranceWithString:(NSString*)string;

语言播报 //初始化类方法,需要传一个字符串进去 

+ (instancetype)speechUtteranceWithAttributedString:(NSAttributedString*)stringAPI_AVAILABLE(ios(10.0), watchos(3.0), tvos(10.0), macos(10.14));

语言播报 //初始化类方法,需要传一个 NSAttributedString类型字符串进去 

- (instancetype)initWithString:(NSString*)string;

语言播报 //初始化对象方法,需要一个字符串作为参数

- (instancetype)initWithAttributedString:(NSAttributedString*)stringAPI_AVAILABLE(ios(10.0), watchos(3.0), tvos(10.0), macos(10.14));

语言播报 //初始化对象方法,需要一个 NSAttributedString类型字符串作为参数

@property(nonatomic, retain, nullable) AVSpeechSynthesisVoice *voice;

*****需要用什么语言来朗读,系统提供了很多语言选项,如果有中文,一定要选择中文语言,要不然读不出来。

如果未指定语音,将使用系统的默认值。 AVSpeechSynthesisVoice 语言对象


在语音话语排队之后设置这些值将没有效果。

@property(nonatomic)floatrate;

语速0.0f~1.0f AVSpeechUtteranceMinimumSpeechRate -AVSpeechUtteranceMaximumSpeechRate.之间

@property(nonatomic)floatpitchMultiplier;

声音的音调0.5f~2.0f 默认是1

@property(nonatomic)floatvolume;

音量 0-1 默认是1

@property(nonatomic)NSTimeIntervalpreUtteranceDelay;

播放下下一句话的时候有多长时间的延迟 默认是0.0

@property(nonatomic)NSTimeIntervalpostUtteranceDelay;

开始播放之前需要等待多久 默认是0.0

AvSpeechSyntheter允许使用基本排队机制说出语音。


@property(nonatomic, weak, nullable) id<AVSpeechSynthesizerDelegate> delegate;

代理方法

---------------------属性

@property(nonatomic,readonly,getter=isSpeaking)BOOLspeaking;

是否正在朗读(只读)

@property(nonatomic,readonly,getter=isPaused)BOOL paused;

是否已经暂停(只读)

- (void)speakUtterance:(AVSpeechUtterance*)utterance;

朗读方法 ,如果将已经排队或正在说话的同一个avspeechutrance排队,会引起异常。

- (BOOL)stopSpeakingAtBoundary:(AVSpeechBoundary)boundary;

停止朗读,会清理掉当前正在执行朗读操作的队列

- (BOOL)pauseSpeakingAtBoundary:(AVSpeechBoundary)boundary;

暂停朗读,这里面需要传递一个AVSpeechBoundary类型参数,两种选择,是立即停止还是读完这个单词再停止。

- (BOOL)continueSpeaking;

继续朗读

@property(nonatomic,retain,nullable)NSArray *outputChannelsAPI_AVAILABLE(ios(10.0), watchos(3.0), tvos(10.0), macos(10.14));

指定要用于合成语音的音频通道 默认为nil 。 不太懂

-----------------

代理方法 AVSpeechSynthesizerDelegate

- (void)speechSynthesizer:(AVSpeechSynthesizer*)synthesizer didStartSpeechUtterance:(AVSpeechUtterance*)utteranceAPI_AVAILABLE(ios(7.0), watchos(1.0), tvos(7.0), macos(10.14));

开始朗读的代理方法

- (void)speechSynthesizer:(AVSpeechSynthesizer*)synthesizer didFinishSpeechUtterance:(AVSpeechUtterance*)utteranceAPI_AVAILABLE(ios(7.0), watchos(1.0), tvos(7.0), macos(10.14));

//结束朗读的代理方法

- (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*)utter

将要播放的语音文字代理方法


 

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 文字阅读例子AVSpeechSynthesizer * synthesizer = [[AVSpeechSynth...
    CAICAI0阅读 2,195评论 0 50
  • 一、音频相关的iOS类库 1、音频相关的iOS类库 使用AVAudioSession负责调解APP和iOS系统里面...
    萧修阅读 687评论 0 0
  • 前言 在IOS7之前一般语音识别是靠一些第三方库,iOS7之后,苹果提供了文字转语音的API可以使用。 导入框架 ...
    超_爱编程阅读 6,026评论 5 2
  • 请原谅,请允许我记个随笔,文章结构不讲究,用词造句不讲究,就是随便记点,写点,当下一个真实的自己,当下一个真实的状...
    Maple_郑小波阅读 268评论 0 0
  • 蛙声、蝉鸣声此起彼伏的盛夏夜晚,凉风阵阵袭来,吹得人舒爽轻松,卸下一整天的劳累,仿佛能够闭上眼即刻入睡。月藏星稀,...
    胡少敏阅读 297评论 0 0

友情链接更多精彩内容