前言:小白第一次接入科大訊飛語(yǔ)音聽寫,接入還是比較簡(jiǎn)單的,先看效果圖無(wú)UI界面

2017-10-25 16_22_20.gif
效果圖有兩部分,一是接入科大訊飛語(yǔ)音聽寫功能,可以實(shí)現(xiàn)將語(yǔ)音轉(zhuǎn)換成文字。 二是看到的語(yǔ)音音量動(dòng)畫效果,為了更加形象。
接入科大訊飛第一步需要自己去科大訊飛開發(fā)者中心申請(qǐng)應(yīng)用,只有應(yīng)用申請(qǐng)成功才能獲取到AppID才可以正常接入語(yǔ)音聽寫,接入部分大家可以去科大訊飛開發(fā)者中心去看開發(fā)文檔,這里主要講一下運(yùn)用到工程中
1.獲取APPID, 添加服務(wù),下載對(duì)應(yīng)的SDK,拿到SDK里邊的iflyMSC.framework,放入自己創(chuàng)建好的工程中

ED4F6CD0-00EC-442A-A820-9AAA6FE580EA.png
2.添加對(duì)應(yīng)的framework依賴

C7639B3F-A060-4997-9253-B3C21031FA0E.png
正常到這里,接入訊飛語(yǔ)音基本完成,下邊是代碼部分
AppDelegate里邊按著開發(fā)者文檔走
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
self.window.backgroundColor = [UIColor whiteColor];
ViewController * vc = [[ViewController alloc]init];
UINavigationController * nav = [[UINavigationController alloc]initWithRootViewController:vc];
self.window.rootViewController = nav;
[self.window makeKeyAndVisible];
[self createOtherFrameWork];
return YES;
}
// 初始化訊飛
- (void)createOtherFrameWork{
//設(shè)置sdk的log等級(jí),log保存在下面設(shè)置的工作路徑中
[IFlySetting setLogFile:LVL_ALL];
//打開輸出在console的log開關(guān)
[IFlySetting showLogcat:YES];
//設(shè)置sdk的工作路徑
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *cachePath = [paths objectAtIndex:0];
[IFlySetting setLogFilePath:cachePath];
NSString *initString = [[NSString alloc] initWithFormat:@"appid=%@", APPID];
[IFlySpeechUtility createUtility:initString];
}
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation{
[[IFlySpeechUtility getUtility] handleOpenURL:url];
return YES;
}
ViewController.m里邊寫入正常的無(wú)UI添加方法
@property (nonatomic, strong) IFlySpeechRecognizer *iFlySpeechRecognizer;
創(chuàng)建語(yǔ)音識(shí)別對(duì)象
- (void)MSCSpeech{
//創(chuàng)建語(yǔ)音識(shí)別對(duì)象
_iFlySpeechRecognizer = [IFlySpeechRecognizer sharedInstance];
//設(shè)置識(shí)別參數(shù)
//設(shè)置為聽寫模式
[_iFlySpeechRecognizer setParameter: @"iat" forKey: [IFlySpeechConstant IFLY_DOMAIN]];
//asr_audio_path 是錄音文件名,設(shè)置value為nil或者為空取消保存,默認(rèn)保存目錄在Library/cache下。
[_iFlySpeechRecognizer setParameter:@"iat.pcm" forKey:[IFlySpeechConstant ASR_AUDIO_PATH]];
//去掉逗號(hào)
[_iFlySpeechRecognizer setParameter:@"0" forKey:[IFlySpeechConstant ASR_PTT]];
[_iFlySpeechRecognizer setDelegate: self];
[self.view addSubview:self.speechButton];
[self.view addSubview:self.speechLabel];
//[self.view addSubview:self.audioStreamButton];
//啟動(dòng)識(shí)別服務(wù)
// [_iFlySpeechRecognizer startListening];
if (_speechView == nil) {
_speechView = [MSCVoiceRecordToastContentView new];
_speechView.center = CGPointMake(self.view.frame.size.width/2, self.speechButton.frame.origin.y-100);
_speechView.bounds = CGRectMake(0, 0, 120, 120);
_speechView.backgroundColor = [[UIColor blackColor]colorWithAlphaComponent:0.5];
_speechView.layer.cornerRadius = 6;
_speechView.hidden = YES;
[self.view addSubview:_speechView];
}
}
當(dāng)然聲明代理,同時(shí)科大訊飛語(yǔ)音delegate的幾個(gè)回調(diào)方法也要寫出來(lái)
//IFlySpeechRecognizerDelegate協(xié)議實(shí)現(xiàn)
//識(shí)別結(jié)果返回代理
- (void) onResults:(NSArray *) results isLast:(BOOL)isLast{
NSMutableString *resultString = [[NSMutableString alloc] init];
NSDictionary *dic = results[0];
for (NSString *key in dic) {
[resultString appendFormat:@"%@",key];
}
NSString * resultFromJson = [MSCHelper stringFromJson:resultString];
self.speechLabel.text = [NSString stringWithFormat:@"%@%@", self.speechLabel.text,resultFromJson];
// NSLog(@"當(dāng)前獲取的數(shù)據(jù) : %@",resultFromJson);
if ([_iFlySpeechRecognizer isListening]) {
NSLog(@"正在識(shí)別");
}
}
//識(shí)別會(huì)話結(jié)束返回代理
- (void)onError: (IFlySpeechError *) error{
NSLog(@"錯(cuò)誤原因 : %@",error.errorDesc);
if (self.speechButton.selected) {
[_iFlySpeechRecognizer startListening];
}else{
[_iFlySpeechRecognizer cancel];
}
}
//停止錄音回調(diào)
- (void) onEndOfSpeech{
NSLog(@"結(jié)束錄音");
}
//開始錄音回調(diào)
- (void) onBeginOfSpeech{
NSLog(@"開始錄音");
}
//音量回調(diào)函數(shù)
- (void) onVolumeChanged: (int)volume{
[self.speechView updateWithPower:volume];
}
//會(huì)話取消回調(diào)
- (void) onCancel{
NSLog(@"取消了當(dāng)前會(huì)話");
}