iphone之使用讯飞语音sdk实现语音识别功能
2、学习里面的demo简单实现了一个小的语音识别功能
先做一个简单demo,看看识别效果。注:语音识别必须联网。
所有接口必需在联网状态下才能正常使用。
效果图:
#import
#import "iflyMSC/IFlySpeechRecognizer.h"
#import "iflyMSC/IFlyDataUploader.h"
@protocol SpeechAlertViewDelegate
@optional
- (void)getResultText:(NSString *)text;
@end
@interface SpeechAlertView : UIAlertView
{
UIImageView *speechImage;//声音图片
IFlySpeechRecognizer * _iFlySpeechRecognizer;//语音识别对象
UIView *backgroundView;
}
@property (assign, nonatomic)id 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
源码下载地址:
iphone之使用讯飞语音sdk实现语音识别功能相关推荐
- Android基于讯飞语音SDK实现语音识别
一.准备工作 1.你需要android手机应用开发基础 2.科大讯飞语音识别SDK android版 3.科大讯飞语音识别开发API文档 4.android手机 关于科大讯飞SDK及API文档,请到科 ...
- 关于讯飞语音SDK开发学习
前奏,浑浑噩噩已经工作一年多,这一年多收获还是挺多的.逛园子应该有两年多了,工作后基本上是天天都会来园子逛逛,园子 里还是有很多牛人写了一些不错的博客,帮我解决很多问题.但是一直没写过博客,归根到底一 ...
- Android 文字转语音使用讯飞语音SDK(eclipse版 无UI)
Android 文字转语音使用讯飞语音SDK(eclipse版) 1.下载SDK(地址:http://www.xfyun.cn/sdk/dispatcher)下载前会让你先创建应用,创建应用后会得到一 ...
- (原创)用讯飞语音实现人机交互的功能
目前在做一款车载的项目,其中有一个需求是在开车的时候实现人与手机的对话,全过程不需要用手,只用语音操控. 这个就类似于人与机器人的对话,机器人在后台一直待命,用户说话 机器人做出对应的反映. 但由于用 ...
- iOS开发(第三方使用)——讯飞语音SDK接入
去到讯飞开放平台创建应用并添加服务 下载SDK,下载时需要选上项目的,必须选上相应的项目,不能用项目1下载的SDK和项目2的app ID结合使用(估计是讯飞绑定了,所以步骤1和步骤2也不能颠倒) 拷贝 ...
- 讯飞语音SDK集成调试常见问题
问题一:启动录音失败 20006 这个问题是由于应用程序未获取到录音,语音等权限导致的. 可以将demo中manifest文件里的权限复制到自己的app程序里,一般都可以解决. 问题二:创建失败,请确 ...
- Android语音识别开发详解(基于讯飞语音SDK)
一.准备工作 1.你需要android手机应用开发基础 2.科大讯飞语音识别SDK android版 3.科大讯飞语音识别开发API文档 4.android手机 关于科大讯飞SDK及API文档,请到科 ...
- SDK(3)讯飞语音
讯飞语音SDK 先去官网下载配置好 as中新建工程,新建module 基于安卓23,不管这个 改为 新建工程, 导入类库 又见addasliabrary 可以查看依 ...
- iOS: 讯飞语音的使用
一.介绍: 讯飞语音做的相当不错,容错率达到90%多,如果需要做语音方面的功能,它绝对是一个不错的选择.讯飞语音的功能很多:语音听写.语音识别.语音合成等,但我们最常用的还是语音听写.讯飞语音中包含界 ...
最新文章
- 设计模式--Builder
- win10+Tensorflow2 + cuda +RTX 3080 +cudnn 安装
- python的clock函数,Python3 time clock()方法
- 异常处理机制——panic 和 recover
- 直播画面抖动_罗永浩直播带货1.1个亿,企业想玩网络直播,三大专业设备你必须知道...
- Spring管理的交易说明-第2部分(JPA)
- scott登录查询常用语句
- edusoho linux 500错误,EduSoho网校系统如何处理500 Internal Server Error?
-
EduSoho官网
- vb中picturebox透明时看到下面的picturebox中图片
- 修复老相片_将老相片数码化
- Redis服务安全加固
- BroadcastReceiver的学习和使用实例
- 关于uuid与自增列的选择
- 微信小程序免费教学视频
- 《树莓派实战秘籍》——1.15 技巧15连接GPIO脚到面包板上
- 【人工智能时代——Notion AI vs ChatGPT】
- leg_detector包随机森林训练(修改自leg_detector中的train_leg_detector.cpp)
- 成立三年多的即刻搜索看起来在消失
- 太空避难所修改数据(船员和货币)
- 不懂时间管理的本质,你只会越来越忙