iOS自带文本转语音播报

本文主要简单介绍下IOS自带的语音播报功能,有需要的可以参考下

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

1.iOS7之后才有该功能

2.需要 AVFoundation 库

3.AVSpeechSynthesizer: 语音合成器, 可以假想成一个可以说话的人, 是最主要的接口

4.AVSpeechSynthesisVoice: 可以假想成人的声音

5.AVSpeechUtterance: 可以假想成要说的一段话

二,代码示例, 播放语音

//语音播报

AVSpeechUtterance *utterance = [AVSpeechUtterance speechUtteranceWithString:@"床前明月光,疑是地上霜。举头望明月,低头思故乡。"];

//设置播报语速

utterance.rate =0.5;

//中式发音

AVSpeechSynthesisVoice *voice = [AVSpeechSynthesisVoice voiceWithLanguage:@"zh-CN"]; 

下面给出详细的发音列表,其他语言请大家自己试吧:

avspeech支持的语言种类包括:

"[AVSpeechSynthesisVoice 0x978a0b0] Language: th-TH",

"[AVSpeechSynthesisVoice 0x977a450] Language: pt-BR",

"[AVSpeechSynthesisVoice 0x977a480] Language: sk-SK",

"[AVSpeechSynthesisVoice 0x978ad50] Language: fr-CA",

"[AVSpeechSynthesisVoice 0x978ada0] Language: ro-RO",

"[AVSpeechSynthesisVoice 0x97823f0] Language: no-NO",

"[AVSpeechSynthesisVoice 0x978e7b0] Language: fi-FI",

"[AVSpeechSynthesisVoice 0x978af50] Language: pl-PL",

"[AVSpeechSynthesisVoice 0x978afa0] Language: de-DE",

"[AVSpeechSynthesisVoice 0x978e390] Language: nl-NL",

"[AVSpeechSynthesisVoice 0x978b030] Language: id-ID",

"[AVSpeechSynthesisVoice 0x978b080] Language: tr-TR",

"[AVSpeechSynthesisVoice 0x978b0d0] Language: it-IT",

"[AVSpeechSynthesisVoice 0x978b120] Language: pt-PT",

"[AVSpeechSynthesisVoice 0x978b170] Language: fr-FR",

"[AVSpeechSynthesisVoice 0x978b1c0] Language: ru-RU",

"[AVSpeechSynthesisVoice 0x978b210] Language: es-MX",

"[AVSpeechSynthesisVoice 0x978b2d0] Language: zh-HK",中文(香港)粤语

"[AVSpeechSynthesisVoice 0x978b320] Language: sv-SE",

"[AVSpeechSynthesisVoice 0x978b010] Language: hu-HU",

"[AVSpeechSynthesisVoice 0x978b440] Language: zh-TW",中文(台湾)

"[AVSpeechSynthesisVoice 0x978b490] Language: es-ES",

"[AVSpeechSynthesisVoice 0x978b4e0] Language: zh-CN",中文(普通话)

"[AVSpeechSynthesisVoice 0x978b530] Language: nl-BE",

"[AVSpeechSynthesisVoice 0x978b580] Language: en-GB",英语(英国)

"[AVSpeechSynthesisVoice 0x978b5d0] Language: ar-SA",

"[AVSpeechSynthesisVoice 0x978b620] Language: ko-KR",

"[AVSpeechSynthesisVoice 0x978b670] Language: cs-CZ",

"[AVSpeechSynthesisVoice 0x978b6c0] Language: en-ZA",

"[AVSpeechSynthesisVoice 0x978aed0] Language: en-AU",

"[AVSpeechSynthesisVoice 0x978af20] Language: da-DK",

"[AVSpeechSynthesisVoice 0x978b810] Language: en-US",英语(美国)

"[AVSpeechSynthesisVoice 0x978b860] Language: en-IE",

"[AVSpeechSynthesisVoice 0x978b8b0] Language: hi-IN",

"[AVSpeechSynthesisVoice 0x978b900] Language: el-GR",

"[AVSpeechSynthesisVoice 0x978b950] Language: ja-JP"

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: 音量

你可能感兴趣的:(iOS自带文本转语音播报)