iphone之使用讯飞语音sdk实现语音识别功能

Wesley13
• 阅读 606

1、首先下载讯飞sdk及文档:http://open.voicecloud.cn/

2、学习里面的demo简单实现了一个小的语音识别功能

先做一个简单demo,看看识别效果。注:语音识别必须联网。

所有接口必需在联网状态下才能正常使用。

效果图:

iphone之使用讯飞语音sdk实现语音识别功能

#import <UIKit/UIKit.h>
#import "iflyMSC/IFlySpeechRecognizer.h"
#import "iflyMSC/IFlyDataUploader.h"

@protocol SpeechAlertViewDelegate <NSObject>
@optional
- (void)getResultText:(NSString *)text;
@end

@interface SpeechAlertView : UIAlertView<IFlySpeechRecognizerDelegate>
{
    UIImageView *speechImage;//声音图片
    
    IFlySpeechRecognizer * _iFlySpeechRecognizer;//语音识别对象
    UIView *backgroundView;
}
@property (assign, nonatomic)id<SpeechAlertViewDelegate> speechDelegate;
@end



#import "SpeechAlertView.h"
#define APPID @"51de5743"
#define TIMEOUT @"20000"
// timeout      连接超时的时间,以ms为单位,毫秒,符号ms ,1000 毫秒 = 1秒,30000=30秒
//timeout:网络超时时间,单位:ms,默认为20000,范围0-30000
@implementation SpeechAlertView

-(id)init
{
    self = [super initWithFrame:CGRectMake(0, 0, 300, 220)];
    if (self) {
        // Initialization code
    }
    return self;
}
- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}

//uialertview的大小位置
-(void)setFrame:(CGRect)frame{
    //重新设置弹出框的大小和位置    
    UIWindow *window =  [UIApplication sharedApplication].keyWindow;

    [super setFrame:CGRectMake((window.frame.size.width-self.frame.size.width)/2, (window.frame.size.height-self.frame.size.height)/2, self.frame.size.width, self.frame.size.height)];
}
//重新写界面内容
- (void) layoutSubviews {
    //屏蔽系统的ImageView 和 UIButton
    for (UIView *v in [self subviews]) {
        if ([v class] == [UIImageView class]){
            [v setHidden:YES];
        }
        
        
        if ([v isKindOfClass:[UIButton class]] ||
            [v isKindOfClass:NSClassFromString(@"UIThreePartButton")]) {
            [v setHidden:YES];
        }
    }
    
    //添加背影图
    UIView *backView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
    backView.backgroundColor = [UIColor colorWithRed:66/255.0 green:68/255.0 blue:70/255.0 alpha:1.0];
    [self addSubview:backView];
    
    //添加标题
    UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 0, backView.frame.size.width-20, 30)];
    titleLabel.backgroundColor = [UIColor clearColor];
    titleLabel.text = @"语音识别";
    titleLabel.font = [UIFont systemFontOfSize:16];
    titleLabel.textColor = [UIColor colorWithRed:218.0/255.0 green:217.0/255.0 blue:217.0/255.0 alpha:1];
    [backView addSubview:titleLabel];
    
    //添加关闭按钮huati_close
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setImage:[UIImage imageNamed:@"alert_close.png"] forState:UIControlStateNormal];
    [backView addSubview:button];
    button.tag = 1;
    button.frame = CGRectMake(backView.frame.size.width-25, 5, 20, 20);
    [button addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
    
    //添加黄线
    UIView *xianView = [[UIView alloc]initWithFrame:CGRectMake(0, 30, backView.frame.size.width, 1)];
    xianView.backgroundColor = [UIColor yellowColor];
    [backView addSubview:xianView];
    
    //添加内容
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 35, backView.frame.size.width, 40)];
    label.backgroundColor = [UIColor clearColor];
    label.text = @"默认不讲话5秒后自动关闭,间隔不讲话2秒后关闭,最多说20秒";
    label.font = [UIFont boldSystemFontOfSize:15];
    label.textAlignment = UITextAlignmentCenter;
    label.textColor = [UIColor yellowColor];
    [backView addSubview:label];
    label.numberOfLines = 0;
    
    //添加中间图片
    speechImage = [[UIImageView alloc]initWithFrame:CGRectMake((self.frame.size.width-50)/2, 80, 50, 85)];
    speechImage.image = [UIImage imageNamed:@"yuyin_01.png"];
    [backView addSubview:speechImage];
    
    //添加说完了按钮
    UIButton *submitButton = [UIButton buttonWithType:UIButtonTypeCustom];
    
    submitButton.frame = CGRectMake((backView.frame.size.width-170)/2, 170, 150, 35);
    submitButton.tag = 2;
    [submitButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [submitButton setTitle:@"说完了" forState:UIControlStateNormal];
    [submitButton setBackgroundImage:[UIImage imageNamed:@"alert_tButton.png"] forState:UIControlStateNormal];
    [submitButton addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
    [backView addSubview:submitButton];
    //想添加什么由此添加
    
    //创建对象
    NSString *initString = [[NSString alloc] initWithFormat:@"appid=%@,timeout=%@",APPID,TIMEOUT];
    //语音识别对象创建
    _iFlySpeechRecognizer = nil;
    _iFlySpeechRecognizer = [IFlySpeechRecognizer createRecognizer:initString delegate:self];
//    _iFlySpeechRecognizer.delegate = self;
    /*
     2.vad_bos:静音超时时间,即用户多长时间不说话则当做超 时处理,单位:ms,engine 指定 sms 识别默认值为 5000,其他 情况默认值为 4000,范围 0-10000;
     3.vad_eos:后端点静音检测时间,即用户停止说话多长时间 内即认为不再输入,自动停止录音,单位:ms,sms 识别默认 值为 1800,其他默认值为 700,范围 0-10000;
     */
    [_iFlySpeechRecognizer setParameter:@"domain" value:@"sms"];
    [_iFlySpeechRecognizer setParameter:@"sample_rate" value:@"16000"];
    [_iFlySpeechRecognizer setParameter:@"plain_result" value:@"0"];
    initString = nil;
    
    //开始识别
    [_iFlySpeechRecognizer startListening];
    
}
//按钮处理方法
-(void) buttonClicked:(id)sender
{
    [self dismissWithClickedButtonIndex:0 animated:YES];
    
}

//显示
-(void)show
{
//    [super show];
    UIWindow *window =  [UIApplication sharedApplication].keyWindow;
    backgroundView = [[UIView alloc]initWithFrame:window.frame];
    backgroundView.backgroundColor = [UIColor clearColor];
    [backgroundView addSubview:self];
    [window addSubview:backgroundView];
}
//弹出框消失
-(void)dismissWithClickedButtonIndex:(NSInteger)buttonIndex animated:(BOOL)animated
{
    [_iFlySpeechRecognizer stopListening];
    [_iFlySpeechRecognizer cancel];
    [_iFlySpeechRecognizer setDelegate:nil];
    _iFlySpeechRecognizer = nil;
    speechImage = nil;
    [backgroundView removeFromSuperview];
    backgroundView = nil;
}


#pragma mark - IFlySpeechRecognizerDelegate
- (void) onVolumeChanged: (int)volume
{
    NSLog(@"%d",volume);
    //录音的音量,音量范围1~100
    if (volume>=0 &&volume<=5) {
        speechImage.image = [UIImage imageNamed:@"yuyin_01.png"];
    }else if(volume>5 && volume<=30){
        speechImage.image = [UIImage imageNamed:@"yuyin_02.png"];
    }else{
        speechImage.image = [UIImage imageNamed:@"yuyin_03.png"];
    }
}

- (void) onBeginOfSpeech
{
    NSLog(@"正在录音");
}

- (void) onEndOfSpeech
{
    NSLog(@"停止录音");
}

- (void) onError:(IFlySpeechError *) error
{
    NSLog(@"停止录音%@,%@",error,[error errorDesc]);
    [self dismissWithClickedButtonIndex:0 animated:YES];
}

//结果
- (void) onResults:(NSArray *) results
{
    NSMutableString *result = [[NSMutableString alloc] init];
    NSDictionary *dic = [results objectAtIndex:0];
    for (NSString *key in dic) {
        [result appendFormat:@"%@",key];
    }
    NSLog(@"转写结果:%@--results:%@",result,results);
    
    //返回结果
    [_speechDelegate getResultText:result];
}

@end

源码下载地址:

http://download.csdn.net/detail/rhljiayou/5889565

点赞
收藏
评论区
推荐文章
blmius blmius
3年前
MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1
文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s
Easter79 Easter79
3年前
swap空间的增减方法
(1)增大swap空间去激活swap交换区:swapoff v /dev/vg00/lvswap扩展交换lv:lvextend L 10G /dev/vg00/lvswap重新生成swap交换区:mkswap /dev/vg00/lvswap激活新生成的交换区:swapon v /dev/vg00/lvswap
皕杰报表之UUID
​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为
Wesley13 Wesley13
3年前
PPDB:今晚老齐直播
【今晚老齐直播】今晚(本周三晚)20:0021:00小白开始“用”飞桨(https://www.oschina.net/action/visit/ad?id1185)由PPDE(飞桨(https://www.oschina.net/action/visit/ad?id1185)开发者专家计划)成员老齐,为深度学习小白指点迷津。
Jacquelyn38 Jacquelyn38
3年前
2020年前端实用代码段,为你的工作保驾护航
有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )
Stella981 Stella981
3年前
ROS(indigo) 语音工具 科大讯飞 百度 pocketsphinx julius rospeex 16.11.22更新 ROS中文语音
ROS语音工具汇总,目前先给出链接,只用过一些简单的命令。中文语音:参考链接:使用科大讯飞库1 http://www.ncnynl.com/archives/201611/1069.html(https://www.oschina.net/action/GoToLink?urlhttp%3A%2F%2Fwww.ncnynl.com%
Wesley13 Wesley13
3年前
00:Java简单了解
浅谈Java之概述Java是SUN(StanfordUniversityNetwork),斯坦福大学网络公司)1995年推出的一门高级编程语言。Java是一种面向Internet的编程语言。随着Java技术在web方面的不断成熟,已经成为Web应用程序的首选开发语言。Java是简单易学,完全面向对象,安全可靠,与平台无关的编程语言。
Wesley13 Wesley13
3年前
MySQL部分从库上面因为大量的临时表tmp_table造成慢查询
背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_
为什么mysql不推荐使用雪花ID作为主键
作者:毛辰飞背景在mysql中设计表的时候,mysql官方推荐不要使用uuid或者不连续不重复的雪花id(long形且唯一),而是推荐连续自增的主键id,官方的推荐是auto_increment,那么为什么不建议采用uuid,使用uuid究
Python进阶者 Python进阶者
9个月前
Excel中这日期老是出来00:00:00,怎么用Pandas把这个去除
大家好,我是皮皮。一、前言前几天在Python白银交流群【上海新年人】问了一个Pandas数据筛选的问题。问题如下:这日期老是出来00:00:00,怎么把这个去除。二、实现过程后来【论草莓如何成为冻干莓】给了一个思路和代码如下:pd.toexcel之前把这