讯飞语音

来源:互联网 发布:淘宝西瑞鞋质量怎么样 编辑:程序博客网 时间:2024/04/27 14:11
添加静态库
将开发工具包中lib目录下地iflyMSC.framework添加到工程中去。再添加如下图所示的库:
2.png
3)导入头文件

在.pch文件导入如下头文件:
01        #import <iflyMSC/IFlyRecognizerViewDelegate.h>
02        #import <iflyMSC/IFlyRecognizerView.h>
03          
04        #import <iflyMSC/IFlySpeechRecognizerDelegate.h>
05        #import <iflyMSC/IFlySpeechRecognizer.h>
06          
07        #import <iflyMSC/IFlySpeechUnderstander.h>
08          
09        #import <iflyMSC/IFlySpeechSynthesizerDelegate.h>
10        #import <iflyMSC/IFlySpeechSynthesizer.h>
11          
12        #import <iflyMSC/IFlyContact.h>
13        #import <iflyMSC/IFlyDataUploader.h>
14        #import <iflyMSC/IFlyUserWords.h>
15        #import <iflyMSC/IFlySpeechConstant.h>
16        #import <iflyMSC/IFlySpeechUtility.h>

    4)初始化SDK

在AppDelegate.m文件中的- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions里添加如下代码:
1        NSString *appid = @"53be3a59";
2            NSString *initString = [NSString stringWithFormat:@"appid=%@",appid];
3        [IFlySpeechUtility createUtility:initString];

请自行根据自己应用的APPID修改appid字符串内容。

    5)语音转写,带界面
01        //初始化语音识别控件
02            self.iflyRecognizerView = [[IFlyRecognizerView alloc]initWithCenter:self.view.center];
03            self.iflyRecognizerView.delegate = self;
04            [self.iflyRecognizerView setParameter: @"iat" forKey:[IFlySpeechConstant IFLY_DOMAIN]];
05            //asr_audio_path保存录音文件名,默认目录是documents
06            [self.iflyRecognizerView setParameter: @"asrview.pcm" forKey:[IFlySpeechConstant ASR_AUDIO_PATH]];
07                //设置返回的数据格式为默认plain
08            [self.iflyRecognizerView setParameter:@"plain" forKey:[IFlySpeechConstant RESULT_TYPE]];
09        - (void)startListenning:(id)sender{
10            [self.iflyRecognizerView start];
11        NSLog(@"开始识别");}
12        //返回数据处理
13        - (void)onResult:(NSArray *)resultArray isLast:(BOOL)isLast
14        {
15            NSMutableString *result = [NSMutableString new];
16            NSDictionary *dic = [resultArray objectAtIndex:0];
17            NSLog(@"DIC:%@",dic);
18            for (NSString *key in dic) {
19                [result appendFormat:@"%@",key];
20            }
21            self.textView.text = [NSString stringWithFormat:@"%@%@",self.textView.text,result];}
22          
23        - (void)onError:(IFlySpeechError *)error
24        {
25            NSLog(@"RecognizerView error : %@",error.errorDesc);}

    6)上传联系人
01        //创建上传实例
02        self.uploader = [IFlyDataUploader new];
03        //获取联系人
04        IFlyContact *iflyContact = [IFlyContact new];
05        NSString *contact = [iflyContact contact];
06          
07        [self.uploader setParameter:@"uup" forKey:@"subject"];
08        [self.uploader setParameter:@"contact" forKey:@"dtt"];
09        [self.uploader uploadDataWithCompletionHandler:^(NSString *result, IFlySpeechError *error) {
10                NSLog(@"uploader contact error: %d",[error errorCode]);
11                if (![error errorCode]) {
12                    [self.popUpView setText: @"上传成功"];
13                    [self.view addSubview:self.popUpView];
14                }
15                else {
16                    [self.popUpView setText: @"上传失败"];
17                    [self.view addSubview:self.popUpView];
18                }
19            } name:@"contact" data: contact];

    7)上传用户词表
01        [self.uploader setParameter:@"iat" forKey:@"subject"];
02        [self.uploader setParameter:@"userword" forKey:@"dtt"];}
03        IFlyUserWords *iflyUserWords = [[IFlyUserWords alloc] initWithJson:@"{\"userword\":[{\"name\":\"iflytek\",\"words\":[\"DevStore\",\"Steven\",\"清蒸鲈鱼\",\"挪威三文鱼\",\"黄埔军校\",\"横沙牌坊\",\"科大讯飞\"]}]}" ];
04        [self.uploader uploadDataWithCompletionHandler:^(NSString *result, IFlySpeechError *error) {
05                NSLog(@"upload userwords error: %d",[error errorCode]);
06                if (![error errorCode]) {
07                    [self.popUpView setText: @"上传成功"];
08                    [self.view addSubview:self.popUpView];
09                }
10                else {
11                    [self.popUpView setText: @"上传失败"];
12                    [self.view addSubview:self.popUpView];
13                }
14            } name:@"userwords" data:[iflyUserWords toString]];

    8)语义识别
01        self.iflySpeechUnderstander = [IFlySpeechUnderstander sharedInstance];
02        self.iflySpeechUnderstander.delegate = self;
03        [self.iflySpeechUnderstander startListening];
04        - (void)onResults:(NSArray *)results isLast:(BOOL)isLast{
05            NSMutableString *resultString = [[NSMutableString alloc] init];
06            NSDictionary *dic = results [0];
07            for (NSString *key in dic) {
08                [resultString appendFormat:@"%@",key];
09            }
10            NSLog(@"听写结果:%@",resultString);
11        }
12        - (void)onError:(IFlySpeechError *)errorCode{
13            NSString *text ;
14        if (errorCode.errorCode ==0 ) {
15                if (result.length==0) {
16                    text = @"无识别结果";
17                }
18                else
19                {
20                    text = @"识别成功";
21                }
22            }
23            else
24            {
25                text = [NSString stringWithFormat:@"发生错误:%d %@",errorCode.errorCode,errorCode.errorDesc];
26        }
27        NSLog(@"%@",text);
28        }

    9)语音合成
view source
print?
01        //头文件定义
02        @interface SpeechSynViewController : UIViewController <IFlySpeechSynthesizerDelegate>
03        @property (nonatomic,strong)IFlySpeechSynthesizer *iflySpeechSyn;   
04        //.m文件定义
05        #define SPEAKING @"DevStore全球首家开发者服务商店。\
06         
07         
08         "
09        - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
10        {
11            self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
12            if (self) {
13                //创建合成对象
14                self.iflySpeechSyn = [IFlySpeechSynthesizer sharedInstance];
15                self.iflySpeechSyn.delegate = self;
16                //设置语音合成的参数
17                //语速,取值范围 0~100
18                [self.iflySpeechSyn setParameter:@"50" forKey:[IFlySpeechConstant SPEED]];
19                //音量;取值范围 0~100
20                [self.iflySpeechSyn setParameter:@"50" forKey:[IFlySpeechConstant VOLUME]];
21                //发音人,默认为”xiaoyan”;可以设置个性化发音人列表
22                [self.iflySpeechSyn setParameter:@"xiaoyan" forKey:[IFlySpeechConstant VOICE_NAME]];
23                //音频采样率,目前支持的采样率有 16000 和 8000
24                [self.iflySpeechSyn setParameter:@"8000" forKey:[IFlySpeechConstant SAMPLE_RATE]];
25                //asr_audio_path保存录音文件路径,如不再需要,设置value为nil表示取消,默认目录是 documents
26                [self.iflySpeechSyn setParameter:@"tts.pcm" forKey:[IFlySpeechConstant TTS_AUDIO_PATH]];
27        }
28            return self;
29        }
30        //开始播放
31        - (void) onStart:(id) sender
32        {
33        [self.iflySpeechSyn startSpeaking:SPEAKING];
34        }
35        //合成结束,此代理必须要实现
36        - (void) onCompleted:(IFlySpeechError *)error
37        {
38            NSString *text;
39            if (self.isCanceled)
40            {
41                text = @"合成已取消";
42            }
43            else if(error.errorCode == 0)
44            {
45                text = @"合成结束";
46            }
47            else
48            {
49                text = [NSString stringWithFormat:@"发生错误:%d %@",error.errorCode,error.errorDesc];
50                NSLog(@"%@",text);
51            }
52        }
0 0
原创粉丝点击