毕竟是相对小众的插件,网络上的资源也并不多,留个笔记。
1.初始化,最好放在应用初始化的时候来做这件事情(否则容易产生10106BUG或者参数错误)
//初始化讯飞语音插件
NSString *initString = [NSString stringWithFormat:@"%@=%@", [IFlySpeechConstant APPID], APPID_VALUE]; //APPID_VALUE是在讯飞申请的唯一标识码
//所有服务启动前,需要确保执行createUtility
[IFlySpeechUtility createUtility:initString];
2.在使用前和使用后要注意AVAudioSession的设置(如果应用中有其他需要使用到音频的功能,否则必然会产生10106的BUG)
//使用语音前,设置成playAndRecord(通常是发生在点击语音按钮事件中)
NSError* sessionError = nil;
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
[audioSession setCategory:AVAudioSessionCategoryPlayAndRecord error:&sessionError];
//使用语音后,设置成playback(通常是在onResult和onError这两个讯飞代理方法中)
NSError* sessionError = nil;
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
[audioSession setCategory:AVAudioSessionCategoryPlayback error:&sessionError];
值得注意的是如果应用中有音频播放器的功能,需要在使用前暂停播放器
3.语音识别返回数据的基本处理,因为onResult中返回的是array
NSMutableString *result = [[NSMutableString alloc] init];
NSDictionary *dic = [array objectAtIndex:0];
for (NSString *key in dic) {
[result appendFormat:@"%@",key];
}
[self performSelectorOnMainThread:@selector(onUpdateTextView:) withObject:result waitUntilDone:YES];
- (void)onUpdateTextView:(NSString *)sentence { //主要是进行一些字符的处理
NSMutableString *keyStr = [NSMutableString stringWithString:sentence];
if ([keyStr hasSuffix:@"。"]) {
NSRange range = [keyStr rangeOfString:@"。" options:NSBackwardsSearch];
[keyStr deleteCharactersInRange:range];
}
if ([keyStr hasSuffix:@"?"]) {
NSRange range = [keyStr rangeOfString:@"?" options:NSBackwardsSearch];
[keyStr deleteCharactersInRange:range];
}
if ([keyStr hasSuffix:@"!"]) {
NSRange range = [keyStr rangeOfString:@"!" options:NSBackwardsSearch];
[keyStr deleteCharactersInRange:range];
}
if ([keyStr hasSuffix:@","]) {
NSRange range = [keyStr rangeOfString:@"," options:NSBackwardsSearch];
[keyStr deleteCharactersInRange:range];
}
//使用语音后,设置成playback
NSError* sessionError = nil;
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
[audioSession setCategory:AVAudioSessionCategoryPlayback error:&sessionError];
if (pausedPlayer) { //看需求是否需要恢复播放器的播放
[[AudioPlayer sharedInstance] startPlay];
pausedPlayer = NO;
}
[iflyRecognizerView cancel];
}