首先仙畦,文字轉(zhuǎn)語音最簡單的方案就是使用系統(tǒng)提供的接口
一、AVSpeechSynthesizer
AVSpeechSynthesizer開放的接口數(shù)量少廉邑,很難滿足我們開發(fā)的各種需求哥蔚,這個時候第三方的語音就是我們比較好的選擇。蛛蒙。
文本轉(zhuǎn)語音技術(shù), 也叫TTS, 是Text To Speech的縮寫. iOS如果想做有聲書等功能的時候, 會用到這門技術(shù).
iOS7之后才有該功能
需要 AVFoundation 庫
AVSpeechSynthesizer: 語音合成器, 可以假想成一個可以說話的人, 是最主要的接口
AVSpeechSynthesisVoice: 可以假想成人的聲音
AVSpeechUtterance: 可以假想成要說的一段話
二糙箍、百度語音包
百度語音,我選擇的是百度語音包GK睢(盡管百度一直狠招黑)
2.1下載離線合成語音包
2.2記住這3個key(說多了都是淚深夯,后來才知道AppId就是AppCode,殺千刀的文檔)
2.3給出的demo還是十分打的,我在自己精簡了很多主要是實現(xiàn)播放和設(shè)置聲音
-(void)configureSDK{
NSLog(@"TTS version info: %@", [BDSSpeechSynthesizer version]);
[BDSSpeechSynthesizer setLogLevel:BDS_PUBLIC_LOG_VERBOSE];
[[BDSSpeechSynthesizer sharedInstance] setSynthesizerDelegate:self];
[self configureOnlineTTS];
[self configureOfflineTTS];
}
// 配置在線
-(void)configureOnlineTTS{
//#error "Set api key and secret key"
[[BDSSpeechSynthesizer sharedInstance] setApiKey:@"e7QA3FWob8EbzLDP7I6fCtcY" withSecretKey:@"17d90e1974d0bcb31725245f96718e73"];
}
// 配置離線
-(void)configureOfflineTTS{
NSString* offlineEngineSpeechData = [[NSBundle mainBundle] pathForResource:@"Chinese_Speech_Female" ofType:@"dat"];
NSString* offlineEngineTextData = [[NSBundle mainBundle] pathForResource:@"Chinese_Text" ofType:@"dat"];
NSString* offlineEngineEnglishSpeechData = [[NSBundle mainBundle] pathForResource:@"English_Speech_Female" ofType:@"dat"];
NSString* offlineEngineEnglishTextData = [[NSBundle mainBundle] pathForResource:@"English_Text" ofType:@"dat"];
NSString* offlineEngineLicenseFile = [[NSBundle mainBundle] pathForResource:@"offline_engine_tmp_license" ofType:@"dat"];
//#error "set offline engine license"
NSError* err = [[BDSSpeechSynthesizer sharedInstance] loadOfflineEngine:offlineEngineTextData speechDataPath:offlineEngineSpeechData licenseFilePath:offlineEngineLicenseFile withAppCode:@"9353239"]; //
if (err) {
return;
}
err = [[BDSSpeechSynthesizer sharedInstance] loadEnglishDataForOfflineEngine:offlineEngineEnglishTextData speechData:offlineEngineEnglishSpeechData];
if (err) {
return;
}
}
// 播放失敗
-(void)synthesizerErrorOccurred:(NSError *)error speaking:(NSInteger)SpeakSentence synthesizing:(NSInteger)SynthesizeSentence{
[[BDSSpeechSynthesizer sharedInstance] cancel];
}
// 合成參數(shù)設(shè)置
// 聲音
[[BDSSpeechSynthesizer sharedInstance] setSynthesizerParam:[NSNumber numberWithInt:BDS_SYNTHESIZER_SPEAKER_FEMALE] forKey:BDS_SYNTHESIZER_PARAM_SPEAKER ];
// 音量
[[BDSSpeechSynthesizer sharedInstance] setSynthesizerParam:[NSNumber numberWithInt:5] forKey:BDS_SYNTHESIZER_PARAM_VOLUME];
// 音速
[[BDSSpeechSynthesizer sharedInstance] setSynthesizerParam:[NSNumber numberWithInt:5] forKey:BDS_SYNTHESIZER_PARAM_SPEED];
// 音調(diào)
[[BDSSpeechSynthesizer sharedInstance] setSynthesizerParam:[NSNumber numberWithInt:5] forKey:BDS_SYNTHESIZER_PARAM_PITCH];
// 壓縮
[[BDSSpeechSynthesizer sharedInstance] setSynthesizerParam:[NSNumber numberWithInt: BDS_SYNTHESIZER_AUDIO_ENCODE_MP3_16K] forKey:BDS_SYNTHESIZER_PARAM_AUDIO_ENCODING ];