實現(xiàn)效果
[MCBaiduSpeechManage SpakeString:@"你好"];
配置注意事項 主要針對離線語音合成
- 在百度語音開發(fā)者平臺 如果你不是通過直接創(chuàng)建應用,而是通過應用管理開通語音合成功能,拿到的App ID/API Key/Secret Key name 那么你需要在 應用管理>>>管理包名 重新設置包名,否則這將影響 離線語音的使用.
- 導入datfile時不要導入 offline_engine_tmp_license.dat 這個是百度demo的離線語音證書,配置完成后會自動下載所需要的離線證書.
- 在使用離線語音功能的時候,要確保離線語音證書已下載成功.換句話說第一次要聯(lián)網,之后才可使用離線語音功能.
- iOS9及后 在plist文件中配置http支持.
代碼說明
按官方說明配置導入SDK,請看上述的注意事項
導入頭文件
#import "BDSSpeechSynthesizer.h"
配置云在線語音合成
//顯示語音日志
[BDSSpeechSynthesizer setLogLevel:BDS_PUBLIC_LOG_VERBOSE];
[[BDSSpeechSynthesizer sharedInstance] setSynthesizerDelegate: self];
/// 設置線程,默認使用主線程
[[BDSSpeechSynthesizer sharedInstance] setSDKCallbackQueue:dispatch_queue_create("baiduVoiceQueue", DISPATCH_QUEUE_SERIAL)];
/// 在線相關設置
[[BDSSpeechSynthesizer sharedInstance] setApiKey:@"yourAK" withSecretKey:@"yourSK"];
//在線語音請求超時時間
[[BDSSpeechSynthesizer sharedInstance] setSynthParam:[NSNumber numberWithFloat:10.0] forKey:BDS_SYNTHESIZER_PARAM_ONLINE_REQUEST_TIMEOUT];
語音合成參數(shù)設置
// 合成參數(shù)設置 可根據(jù)需求自行修改
///女聲
[[BDSSpeechSynthesizer sharedInstance] setSynthParam:
[NSNumber numberWithInt:BDS_SYNTHESIZER_SPEAKER_FEMALE]
forKey:BDS_SYNTHESIZER_PARAM_SPEAKER];
///音量 0 ~9
[[BDSSpeechSynthesizer sharedInstance] setSynthParam:[NSNumber numberWithInt:9] forKey:BDS_SYNTHESIZER_PARAM_VOLUME];
///語速 0 ~9
[[BDSSpeechSynthesizer sharedInstance] setSynthParam:[NSNumber numberWithInt:6] forKey:BDS_SYNTHESIZER_PARAM_SPEED];
///語調 0 ~9
[[BDSSpeechSynthesizer sharedInstance] setSynthParam: [NSNumber numberWithInt:5] forKey:BDS_SYNTHESIZER_PARAM_PITCH];
///mp3音質 壓縮的16K
[[BDSSpeechSynthesizer sharedInstance] setSynthParam:
[NSNumber numberWithInt: BDS_SYNTHESIZER_AUDIO_ENCODE_MP3_16K]
forKey:BDS_SYNTHESIZER_PARAM_AUDIO_ENCODING ];
配置離線語音合成
/// 離線相關設置
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"];
///無法實現(xiàn)離線語音請檢查以下部分
NSError* err = [[BDSSpeechSynthesizer sharedInstance] loadOfflineEngine:offlineEngineTextData speechDataPath:offlineEngineSpeechData licenseFilePath:nil withAppCode:@"8553284"];
if(err)
{
/// Get offline TTS license from server failed with error -101 請檢查包名設置
NSLog(@"_________________%@",[err localizedDescription]);
}
err = [[BDSSpeechSynthesizer sharedInstance] loadEnglishDataForOfflineEngine:offlineEngineEnglishTextData speechData:offlineEngineEnglishSpeechData];
if(err)
{
NSLog(@"_________________%@",err);
}