前言
之前我的簡書中寫到了【iOS實現(xiàn)語音播報】里面有介紹到兩種語音播報方式:AVSpeechSynthesis和百度TTS,這篇文章我們就簡單的了解一下百度TTS到底該如何使用?
正文
1.百度TTS集成
TTS的集成我就不多說了,大家可以通過百度TTS集成進(jìn)行查看和學(xué)習(xí)!
2.使用
不管是【極光推送】還是【TTS】第一步少不了申請APPKey,然后在我們的工程中注冊
// 注冊
-(void)configureTTSSDK{
NSLog(@"TTS version info: %@", [BDSSpeechSynthesizer version]);
[BDSSpeechSynthesizer setLogLevel:BDS_PUBLIC_LOG_VERBOSE];
[[BDSSpeechSynthesizer sharedInstance] setSynthesizerDelegate:self];
// [self configureOnlineTTS];
[self configureOfflineTTS];
}
// 在線注冊
-(void)configureOnlineTTS{
[[BDSSpeechSynthesizer sharedInstance] setApiKey:BaiDu_API_Key withSecretKey:BaiDu_Secret_Key];
[[AVAudioSession sharedInstance]setCategory:AVAudioSessionCategoryPlayback error:nil];
}
//離線注冊
-(void)configureOfflineTTS{
NSError *err = nil;
// 在這里選擇不同的離線音庫(請在XCode中Add相應(yīng)的資源文件),同一時間只能load一個離線音庫。根據(jù)網(wǎng)絡(luò)狀況和配置,SDK可能會自動切換到離線合成。
NSString* offlineEngineSpeechData = [[NSBundle mainBundle] pathForResource:@"Chinese_And_English_Speech_Female" ofType:@"dat"];
NSString* offlineChineseAndEnglishTextData = [[NSBundle mainBundle] pathForResource:@"Chinese_And_English_Text" ofType:@"dat"];
err = [[BDSSpeechSynthesizer sharedInstance] loadOfflineEngine:offlineChineseAndEnglishTextData speechDataPath:offlineEngineSpeechData licenseFilePath:nil withAppCode:BaiDu_APP_ID];
if(err){
NSLog(@"offLineTTS configure error : %@",err.localizedDescription);
}else{
NSLog(@"offLineTTS success");
}
}
- (void)ttsReadContent:(NSString *) message{
[[BDSSpeechSynthesizer sharedInstance] setPlayerVolume:5];
[[BDSSpeechSynthesizer sharedInstance] setSynthParam:[NSNumber numberWithInteger:7] forKey:BDS_SYNTHESIZER_PARAM_SPEED];
NSInteger flag = [[BDSSpeechSynthesizer sharedInstance] speakSentence:message withError:nil];
NSLog(@"TTSFlage -------%ld",flag);
}
不難吧!是不是很簡單!也就這樣。。。。