最近公司在接入一个语音评测的项目,记录下调试封装腾讯口语评测的SDK过程
首先下载SDK,接入到项目,参照官方文档,不做多说
由于项目有需要暂停录音和获取录音音量的需求,所以必须要自己录音流式传输。
这边先记录用官方自带的录音调试
下面贴上封装工具类的代码
#import "TencentSeoUtil.h"
#import <AVFoundation/AVFoundation.h>
#import "PrivateInfo.h"
@interface TencentSeoUtil()<TAIOralEvaluationDelegate>
@property (nonatomic , copy) void(^errorBlock)(void);
@property (nonatomic , strong) TAIOralEvaluation *oralEvaluation;
@property (nonatomic , strong) NSString *fileName;
@property (nonatomic , assign) BOOL isRecording;
@property (nonatomic , strong) NSString *filePath;
@end
@implementation TencentSeoUtil
-(instancetype)init{
self = [super init];
if(self){
self.oralEvaluation = [[TAIOralEvaluation alloc] init];
self.oralEvaluation.delegate = self;
// 由于我前一个页面有音频播放,导致这边录音一直不调用代理方法,加上这两句代码,完美解决
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];
[[AVAudioSession sharedInstance] setActive:YES error:nil];
}
return self;
}
- (void)requestRecordingPermission: (void(^) (BOOL))callback {
AVAudioSession* audioSession = [AVAudioSession sharedInstance];
if ([audioSession respondsToSelector: @selector(requestRecordPermission:)]) {
[audioSession performSelector: @selector(requestRecordPermission:)
withObject: ^(BOOL granted) {
callback(granted);
}];
}
}
#pragma mark - oral evaluation delegate
- (void)oralEvaluation:(TAIOralEvaluation *)oralEvaluation onEvaluateData:(TAIOralEvaluationData *)data result:(TAIOralEvaluationRet *)result error:(TAIError *)error
{
if(error.code != TAIErrCode_Succ){
[SilenceHUDUtil showError:@"录制失败!"];
self.isRecording = NO;
}
// NSFileHandle *handle = [NSFileHandle fileHandleForWritingAtPath:self.filePath];
// [handle seekToEndOfFile];
// [handle writeData:data.audio];
NSLog(@"%s",__func__);
NSLog(@"oralEvaluation:datasize:%lu seq:%ld, end:%ld, error:%@, ret:%@",(unsigned long)data.audio.length,(long)data.seqId, (long)data.bEnd, error, result);
if(data.bEnd){
NSLog(@"结果%@",result);
}
}
-(void)beginRecordWithFileName:(NSString *)fileName andTestString:(NSString *)testString{
self.fileName = fileName;
TAIOralEvaluationParam *param = [[TAIOralEvaluationParam alloc] init];
param.sessionId = @((int)[[NSDate date] timeIntervalSince1970]).stringValue;
param.appId = [PrivateInfo shareInstance].appId;
param.soeAppId = [PrivateInfo shareInstance].soeAppId;
param.secretId = [PrivateInfo shareInstance].secretId;
param.secretKey = [PrivateInfo shareInstance].secretKey;
param.workMode = TAIOralEvaluationWorkMode_Stream;
param.evalMode = TAIOralEvaluationEvalMode_Paragraph;
param.serverType = TAIOralEvaluationServerType_English;
param.scoreCoeff = 3;
param.fileType = TAIOralEvaluationFileType_Mp3;
param.storageMode = TAIOralEvaluationStorageMode_Enable;
param.textMode = TAIOralEvaluationTextMode_Noraml;
param.retryTimes = 5;
param.refText = @"hello, how are you? how old are you?";
param.timeout = 30;
NSLog(@"测试文本:%@",testString);
NSString *path = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0];
path = [path stringByAppendingPathComponent:@"TencentSeo"];
self.filePath = [path stringByAppendingPathComponent:self.fileName];
if([[NSFileManager defaultManager] fileExistsAtPath:path] == false){
[[NSFileManager defaultManager] createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:nil];
}else if([[NSFileManager defaultManager] fileExistsAtPath:self.filePath] == false){
BOOL isCreatFileOK = [[NSFileManager defaultManager] createFileAtPath:self.filePath contents:nil attributes:nil];
NSLog(@"创建文件%@",isCreatFileOK?@"成功":@"失败");
}
[self.oralEvaluation setFragSize:1024];
[self.oralEvaluation startRecordAndEvaluation:param callback:^(TAIError *error) {
if(error.code == TAIErrCode_Succ){
self.isRecording = YES;
}
// [ws setResponse:[NSString stringWithFormat:@"startRecordAndEvaluation:%@", error]];
NSLog(@"startRecordAndEvaluation:%@",error);
}];
}
-(BOOL)isRecording{
return _isRecording;
}
-(void)pause{
}
-(void)pursue{
}
-(void)cancel{
}
-(void)complet{
self.isRecording = NO;
[self.oralEvaluation stopRecordAndEvaluation:^(TAIError *error) {
NSLog(@"stopRecordAndEvaluation:%@", error);
}];
}
@end
官方的DEMO在录音开始后返回上一个页面时,会奔溃在recordingCallback
解决方法:
-(void)dealloc{
[self.oralEvaluation stopRecordAndEvaluation:^(TAIError *error) {
}];
}