iOS文字转音频
一、首先来认识一下iOS系统API (AVFoundation框架)
1.AVSpeechSynthesizer类(用于合成播放声音)
@property(nonatomic, assign, nullable) id<AVSpeechSynthesizerDelegate> delegate;//设置代理
@property(nonatomic, readonly, getter=isSpeaking) BOOL speaking;//是否播放
@property(nonatomic, readonly, getter=isPaused) BOOL paused;//是否暂停
- (void)speakUtterance:(AVSpeechUtterance *)utterance;//播放声音
- (BOOL)stopSpeakingAtBoundary:(AVSpeechBoundary)boundary;//停止播放声音,释放队列
- (BOOL)pauseSpeakingAtBoundary:(AVSpeechBoundary)boundary;//暂停播放声音
typedef NS_ENUM(NSInteger, AVSpeechBoundary) {
AVSpeechBoundaryImmediate,
AVSpeechBoundaryWord//
} NS_ENUM_AVAILABLE_IOS(7_0);
- (BOOL)continueSpeaking;//继续播放声音
AVSpeechSynthesizer执行状态回调
- (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;//将要播放变化字符时回调
2.AVSpeechSynthesisVoice类(用来配置发音,支持的发音非常多)
+ (NSArray<AVSpeechSynthesisVoice *> *)speechVoices;//系统支持的语言
+ (NSString *)currentLanguageCode;//当前语言码
+ (nullable AVSpeechSynthesisVoice *)voiceWithLanguage:(nullable NSString *)languageCode;//用语言码初始化声音对象
+ (nullable AVSpeechSynthesisVoice *)voiceWithIdentifier:(NSString *)identifier NS_AVAILABLE_IOS(9_0);//用语言ID初始化声音对象
@property(nonatomic, readonly) NSString *language;//声音语言
语言种类 : Language
zh-CN, 中国(中文) ru-RU, 俄罗斯(俄文) en-GB, 英国(英文)
fr-FR, 法国(法文) th-TH, 泰国(泰文) de-DE, 德国(德文)
en-US, 美国(英文) pt-BR, 巴西(葡萄牙文) pl-PL, 波兰(波兰文)
en-IE, 爱尔兰(英文) el-GR, 希腊(希腊文) id-ID, 印度尼西亚(印度尼西亚文)
sv-SE, 瑞典(瑞典文) tr-TR, 土耳其(土耳其文) pt-PT, 葡萄牙(葡萄牙文)
ja-JP, 日本(日文) ko-KR, 南朝鲜(朝鲜文) hu-HU, 匈牙利(匈牙利文)
cs-CZ, 捷克共和国(捷克文) da-DK, 丹麦(丹麦文) es-MX, 墨西哥(西班牙文)
fr-CA, 加拿大(法文) nl-NL, 荷兰(荷兰文) fi-FI, 芬兰(芬兰文)
es-ES, 西班牙(西班牙文) it-IT, 意大利(意大利文) he-IL, 以色列(希伯莱文,阿拉伯文)
no-NO, 挪威(挪威文) ro-RO, 罗马尼亚(罗马尼亚文) zh-HK, 香港(中文)
zh-TW, 台湾(中文) sk-SK, 斯洛伐克(斯洛伐克文) ar-SA 沙特阿拉伯(阿拉伯文)
en-ZA, 南非(英文) nl-BE, 比利时(荷兰文) en-AU, 澳大利亚(英文)
hi-IN 印度(印度文)
@property(nonatomic, readonly) NSString *identifier NS_AVAILABLE_IOS(9_0);//声音对象ID
NSString *const AVSpeechSynthesisVoiceIdentifierAlex NS_ENUM_AVAILABLE_IOS(9_0);//用于初始化声音语言,仅仅代表 en-US(美语).
@property(nonatomic, readonly) NSString *name NS_AVAILABLE_IOS(9_0);//声音对象名称
@property(nonatomic, readonly) AVSpeechSynthesisVoiceQuality quality NS_AVAILABLE_IOS(9_0);//声音质量
typedef NS_ENUM(NSInteger, AVSpeechSynthesisVoiceQuality) {
AVSpeechSynthesisVoiceQualityDefault = 1,
AVSpeechSynthesisVoiceQualityEnhanced
} NS_ENUM_AVAILABLE_IOS(9_0);
3.AVSpeechUtterance类(将字符串合成为语音对象提供给AVSpeechSynthesizer来播放)
//用字符串初始化要转换为音频的文字
+ (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; //声音播放速率
const float AVSpeechUtteranceMinimumSpeechRate NS_AVAILABLE_IOS(7_0);//慢
const float AVSpeechUtteranceMaximumSpeechRate NS_AVAILABLE_IOS(7_0);//快
const float AVSpeechUtteranceDefaultSpeechRate NS_AVAILABLE_IOS(7_0);//中(默认)
@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
二.简单实现文字转音频,详细操作可以看上面的API,自己选择
//初始化语音合成器
AVSpeechSynthesizer *mySpeech=[[AVSpeechSynthesizer alloc]init];
//使用中文
AVSpeechSynthesisVoice *myLanguage=[AVSpeechSynthesisVoice voiceWithLanguage:@"zh-CN"];
//想要转换的文字
AVSpeechUtterance *myUtterance=[[AVSpeechUtterance alloc]initWithString:@"这是叶阳的博客!!!"];
myUtterance.rate=AVSpeechUtteranceDefaultSpeechRate;//音速
myUtterance.volume=1;//音量
myUtterance.pitchMultiplier=1;//音调
myUtterance.postUtteranceDelay=1;//播放下一句前停顿
myUtterance.voice=myLanguage;
[mySpeech speakUtterance:myUtterance];
//非ARC记得释放资源
/*
[myUtterance release];
[mySpeech release];
*/