开发者论坛

 找回密码
 注册 (请使用非IE浏览器)
查看: 4987|回复: 4

绝对原创,代码分享!科大讯飞集成代码分享

[复制链接]

0

精华

0

贡献

0

赞扬

帖子
6
软币
158
在线时间
10 小时
注册时间
2014-6-4
发表于 2014-7-23 17:22:16 | 显示全部楼层 |阅读模式
这里有更多源码: 13986644126538096n.jpg

1)新建工程
0.png
2)添加静态库
将开发工具包中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 @"你是我的小呀小苹果儿,怎么爱你都不嫌多。\
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

精华

8

贡献

10

赞扬

帖子
36
软币
189
在线时间
9 小时
注册时间
2014-10-1
发表于 2014-10-2 21:07:22 | 显示全部楼层
讯飞很牛的牌子,不知道资料怎么样
回复

使用道具 举报

0

精华

0

贡献

0

赞扬

帖子
11
软币
101
在线时间
6 小时
注册时间
2014-11-24
发表于 2014-11-24 15:48:09 | 显示全部楼层
非常棒、、、、、、、、、
回复

使用道具 举报

0

精华

97

贡献

28

赞扬

帖子
191
软币
566
在线时间
70 小时
注册时间
2014-9-15
发表于 2015-1-13 14:58:00 | 显示全部楼层
科大讯飞 看看
回复

使用道具 举报

Archiver|手机版|小黑屋|开发者网 ( 苏ICP备08004430号-2 )
版权所有:南京韵文教育信息咨询有限公司

GMT+8, 2024-4-19 22:21

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表