ios 语音识别 android,iOS 10 语音识别

完整Demo代码:

#import "ViewController.h"

#import // 导入类库

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {

[super viewDidLoad];

// 0. 请求权限 && 配置Info.plist

/** 若不配置info.plist会Crash报错:

This app has crashed because it attempted to access privacy-sensitive data without a usage description.

The app's Info.plist must contain an NSSpeechRecognitionUsageDescription key with a string value explaining to the user how the app uses this data.

*/

[SFSpeechRecognizer requestAuthorization:^(SFSpeechRecognizerAuthorizationStatus status) {

switch (status) {

case SFSpeechRecognizerAuthorizationStatusNotDetermined:

NSLog(@"SFSpeechRecognizerAuthorizationStatusNotDetermined");

break;

case SFSpeechRecognizerAuthorizationStatusDenied:

NSLog(@"SFSpeechRecognizerAuthorizationStatusDenied");

break;

case SFSpeechRecognizerAuthorizationStatusRestricted:

NSLog(@"SFSpeechRecognizerAuthorizationStatusRestricted");

break;

case SFSpeechRecognizerAuthorizationStatusAuthorized:

NSLog(@"SFSpeechRecognizerAuthorizationStatusAuthorized");

break;

default:

break;

}

}];

}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

// 1. 初始化一个识别器

SFSpeechRecognizer *recognizer = [[SFSpeechRecognizer alloc] initWithLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"]];

// 2. 初始化mp3的url (我在Bundle中导入了一个本地的MP3文件进行演示)

NSURL *fileUrl = [[NSBundle mainBundle] URLForResource:@"buyao.mp3" withExtension:nil];

// 3. 根据Url创建请求

SFSpeechURLRecognitionRequest *request = [[SFSpeechURLRecognitionRequest alloc] initWithURL:fileUrl];

// 4. 发起请求

[recognizer recognitionTaskWithRequest:request resultHandler:^(SFSpeechRecognitionResult * _Nullable result, NSError * _Nullable error) {

if (error != nil) {

NSLog(@"识别错误:%@",error);

return ;

}

NSLog(@"%@",result.bestTranscription.formattedString);

}];

}

@end

控制台打印结果:(实际是斗地主的要不起,辨识度有待提高)

2017-01-13 11:36:58.774398 SpeechDemo[964:603117] SFSpeechRecognizerAuthorizationStatusAuthorized

2017-01-13 11:37:01.365989 SpeechDemo[964:603076] 识别错误:Error Domain=kAFAssistantErrorDomain Code=209 "(null)"

2017-01-13 11:37:01.503930 SpeechDemo[964:603076] 识别错误:Error Domain=kAFAssistantErrorDomain Code=209 "(null)"

2017-01-13 11:37:02.338849 SpeechDemo[964:603076] 要不钱

2017-01-13 11:37:02.339523 SpeechDemo[964:603076] 要不钱

2017-01-13 11:37:02.347165 SpeechDemo[964:603076] 要不钱

需要注意的地方就是需请求授权,并配置Info.plist,否则Crash.

NSSpeechRecognitionUsageDescription对应配置的string value便是授权时的提示信息

e.g.我这里这是的是语音识别授权

2f0d7f79de73

IMG_0037.PNG

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值