前言
在IOS7之前一般語(yǔ)音識(shí)別是靠一些第三方庫(kù),iOS7之后,蘋(píng)果提供了文字轉(zhuǎn)語(yǔ)音的API可以使用。
導(dǎo)入框架
導(dǎo)入AVFoundation框架:
#import ?<AVFoundation/AVFoundation.h>
用法詳解
所用的類(lèi),主要有三個(gè)AVSpeechSynthesizer(聲音合成器,控制播放,暫定,繼續(xù),停止等),AVSpeechUtterance(表達(dá)器,控制內(nèi)容,音調(diào),速率等),AVSpeechSynthesisVoice(語(yǔ)言)。
1:AVSpeechSynthesizer
//開(kāi)始,傳入AVSpeechUtterance參數(shù)
- (void)speakUtterance:(AVSpeechUtterance *)utterance;
//停止
- (BOOL)stopSpeakingAtBoundary:(AVSpeechBoundary)boundary;
typedef NS_ENUM(NSInteger, AVSpeechBoundary) {
AVSpeechBoundaryImmediate,//立即停止
AVSpeechBoundaryWord ? ? ? ? //讀完最后一個(gè)詞停止
}
//暫停
- (BOOL)pauseSpeakingAtBoundary:(AVSpeechBoundary)boundary;
//繼續(xù)
- (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;//內(nèi)容字符串
@property(nonatomic, readonly) NSAttributedString *attributedSpeechString NS_AVAILABLE_IOS(10_0);//內(nèi)容富文本
@property(nonatomic) float rate;? ? ? ? ? ? // 速率AVSpeechUtteranceMinimumSpeechRate 之間 AVSpeechUtteranceMaximumSpeechRate.
@property(nonatomic) float pitchMultiplier;? // 音調(diào)0.5-2
@property(nonatomic) float volume;? ? ? ? ? // 音量0-1
@property(nonatomic) NSTimeInterval preUtteranceDelay;? ? //每句前延遲時(shí)間 默認(rèn)0
@property(nonatomic) NSTimeInterval postUtteranceDelay;? // 美劇后延遲時(shí)間 默認(rèn)0
3:AVSpeechSynthesisVoice
+ (NSArray*)speechVoices;//所有語(yǔ)言
+ (NSString *)currentLanguageCode;//當(dāng)前語(yǔ)言
+ (nullable AVSpeechSynthesisVoice *)voiceWithLanguage:(nullable NSString *)languageCode;//通過(guò)languageCode初始化
支持語(yǔ)言:(可通過(guò)speechVoices獲取所有,currentLanguageCode獲取當(dāng)前)
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);//通過(guò)identifier初始化
4:AVSpeechSynthesizerDelegate(控制狀態(tài)完成操作)
//開(kāi)始執(zhí)行
- (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didStartSpeechUtterance:(AVSpeechUtterance *)utterance;
//結(jié)束執(zhí)行
- (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didFinishSpeechUtterance:(AVSpeechUtterance *)utterance;
//暫停執(zhí)行
- (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didPauseSpeechUtterance:(AVSpeechUtterance *)utterance;
//繼續(xù)執(zhí)行
- (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didContinueSpeechUtterance:(AVSpeechUtterance *)utterance;
//取消時(shí)執(zhí)行
- (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didCancelSpeechUtterance:(AVSpeechUtterance *)utterance;
//朗讀某一段執(zhí)行
- (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer willSpeakRangeOfSpeechString:(NSRange)characterRange utterance:(AVSpeechUtterance *)utterance;
demo接口設(shè)計(jì)與地址
github.com/wxcGit/TTS(demo地址)