iOS TTS详解(附demo)

前言

在IOS7之前一般语音识别是靠一些第三方库,iOS7之后,苹果提供了文字转语音的API可以使用。

导入框架

导入AVFoundation框架:

iOS TTS详解(附demo)_第1张图片

#import  

用法详解

所用的类,主要有三个AVSpeechSynthesizer(声音合成器,控制播放,暂定,继续,停止等),AVSpeechUtterance(表达器,控制内容,音调,速率等),AVSpeechSynthesisVoice(语言)。

1:AVSpeechSynthesizer

//开始,传入AVSpeechUtterance参数

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

//停止

- (BOOL)stopSpeakingAtBoundary:(AVSpeechBoundary)boundary;

typedef NS_ENUM(NSInteger, AVSpeechBoundary) {

AVSpeechBoundaryImmediate,//立即停止

AVSpeechBoundaryWord         //读完最后一个词停止

}

//暂停

- (BOOL)pauseSpeakingAtBoundary:(AVSpeechBoundary)boundary;

//继续

- (BOOL)continueSpeaking;

2:AVSpeechUtterance

//初始化

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

+ (instancetype)speechUtteranceWithAttributedString:(NSAttributedString *)string NS_AVAILABLE_IOS(10_0);

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

- (instancetype)initWithAttributedString:(NSAttributedString *)string NS_AVAILABLE_IOS(10_0);

//属性

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

@property(nonatomic, readonly) NSString *speechString;//内容字符串

@property(nonatomic, readonly) NSAttributedString *attributedSpeechString NS_AVAILABLE_IOS(10_0);//内容富文本

@property(nonatomic) float rate;            // 速率AVSpeechUtteranceMinimumSpeechRate 之间 AVSpeechUtteranceMaximumSpeechRate.

@property(nonatomic) float pitchMultiplier;  // 音调0.5-2

@property(nonatomic) float volume;          // 音量0-1

@property(nonatomic) NSTimeInterval preUtteranceDelay;    //每句前延迟时间 默认0

@property(nonatomic) NSTimeInterval postUtteranceDelay;  // 美剧后延迟时间 默认0

3:AVSpeechSynthesisVoice

+ (NSArray*)speechVoices;//所有语言

+ (NSString *)currentLanguageCode;//当前语言

+ (nullable AVSpeechSynthesisVoice *)voiceWithLanguage:(nullable NSString *)languageCode;//通过languageCode初始化

支持语言:(可通过speechVoices获取所有,currentLanguageCode获取当前)

Arabic (ar-SA)

Chinese (zh-CN, zh-HK, zh-TW)

Czech (cs-CZ)

Danish (da-DK)

Dutch (nl-BE, nl-NL)

English (en-AU, en-GB, en-IE, en-US, en-ZA)

Finnish (fi-FI)

French (fr-CA, fr-FR)

German (de-DE)

Greek (el-GR)

Hebrew (he-IL)

Hindi (hi-IN)

Hungarian (hu-HU)

Indonesian (id-ID)

Italian (it-IT)

Japanese (ja-JP)

Korean (ko-KR)

Norwegian (no-NO)

Polish (pl-PL)

Portuguese (pt-BR, pt-PT)

Romanian (ro-RO)

Russian (ru-RU)

Slovak (sk-SK)

Spanish (es-ES, es-MX)

Swedish (sv-SE)

Thai (th-TH)

Turkish (tr-TR)

+ (nullable AVSpeechSynthesisVoice *)voiceWithIdentifier:(NSString *)identifier NS_AVAILABLE_IOS(9_0);//通过identifier初始化

4:AVSpeechSynthesizerDelegate(控制状态完成操作)

//开始执行

- (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;

demo接口设计与地址


iOS TTS详解(附demo)_第2张图片
iOS TTS详解(附demo)_第3张图片

github.com/wxcGit/TTS(demo地址)

你可能感兴趣的:(iOS TTS详解(附demo))