#import <Foundation/Foundation.h>
#import <Speech/Speech.h> @interface SpeechListener : NSObject
<SFSpeechRecognitionTaskDelegate,SFSpeechRecognizerDelegate> @property(nonatomic,strong)SFSpeechRecognizer *listener;
@property(nonatomic,strong)SFSpeechRecognitionTask *currentTask;
@property(nonatomic,assign)SFSpeechRecognizerAuthorizationStatus authorizationStatus;
@property(nonatomic,strong)SFSpeechAudioBufferRecognitionRequest *currentRequest;
@property(nonatomic,assign)BOOL inRecognizer;
@property(nonatomic,copy)void (^haveRecognizerSpeakTextBlock)(NSString *text,NSTimeInterval startLocation,NSTimeInterval length); -(void)startRecognizer;
-(void)giveBuffer:(CMSampleBufferRef)buffer;
-(void)endRecognizer; @end
#import "SpeechListener.h"

@interface SpeechListener()

@property(nonatomic,copy)NSString *currentText;
@property(nonatomic,assign)NSTimeInterval beginListenLocation; @end @implementation SpeechListener -(instancetype)init
{
self = [super init];
if (self) {
self.listener = [[SFSpeechRecognizer alloc] initWithLocale:[NSLocale localeWithLocaleIdentifier:@"zh-CN"]];
[self.listener setDelegate:self];
[self requestAuthorizationStatus];
}
return self;
} -(void)requestAuthorizationStatus
{
typeof(self) __weak weakself = self;
[SFSpeechRecognizer requestAuthorization:^(SFSpeechRecognizerAuthorizationStatus status) {
typeof(weakself) __strong strongself = weakself;
strongself.authorizationStatus = status;
}];
} -(void)startRecognizer
{
self.beginListenLocation = CFAbsoluteTimeGetCurrent();
self.currentRequest = [[SFSpeechAudioBufferRecognitionRequest alloc] init];
[self.currentRequest setShouldReportPartialResults:YES];
typeof(self) __weak weakself = self;
self.currentTask = [self.listener recognitionTaskWithRequest:self.currentRequest resultHandler:^(SFSpeechRecognitionResult *result, NSError *error) {
typeof(weakself) __strong strongself = weakself;
if (strongself.inRecognizer) {
if (error != nil) {
[strongself endRecognizer];
}
else
if(result != nil) {
if (strongself.inRecognizer) {
strongself.currentText = [[result bestTranscription] formattedString];
}
if ([result isFinal]) {
[strongself endRecognizer];
}
}
}
if (error) { }
}];
self.inRecognizer = YES;
} -(void)endRecognizer
{
if (self.currentText.length > ) {
if (self.haveRecognizerSpeakTextBlock) {
self.haveRecognizerSpeakTextBlock(self.currentText, self.beginListenLocation, CFAbsoluteTimeGetCurrent() - self.beginListenLocation);
}
} self.inRecognizer = NO;
if (self.currentTask) {
[self.currentTask cancel];
self.currentTask = nil;
}
if (self.currentRequest) {
[self.currentRequest endAudio];
self.currentRequest = nil;
}
self.currentText = @"";
} -(void)speechRecognizer:(SFSpeechRecognizer *)speechRecognizer availabilityDidChange:(BOOL)available
{
self.inRecognizer = !available;
} -(void)giveBuffer:(CMSampleBufferRef)buffer
{
if (self.currentRequest) {
[self.currentRequest appendAudioSampleBuffer:buffer];
}
if (self.inRecognizer && (CFAbsoluteTimeGetCurrent() - self.beginListenLocation) > ) {
[self endRecognizer];
[self startRecognizer];
}
} @end

https://www.politepix.com/openears/   一款免费的识别工具

最新文章

  1. JDBC_part1_Oracle数据库连接JDBC以及查询语句
  2. ASP.NET Aries 开源开发框架:开发指南(一)
  3. Linux服务器常用操作
  4. [XAF] How to improve the application&#39;s performance
  5. nginx 日志切割
  6. MVC公开课 &ndash; 1.基础 (2013-3-15广州传智MVC公开课)
  7. BZOJ 3224: Tyvj 1728 普通平衡树 treap
  8. SQL笔记(1)索引/触发器
  9. 一模 (3) day2
  10. SoapUI-x64(app:url请求参数)
  11. React Native通信机制详解
  12. 如何去除CISCO交换机中的口令??
  13. ES6学习笔记之Promise
  14. [React] React Fundamentals: Integrating Components with D3 and AngularJS
  15. cocos2dx内存管理的一些看法
  16. .net自定义控件
  17. MVC中的模型注解
  18. 组队赛第二场:字符串哈希+DP
  19. freemarker中的substring取子串(十四)
  20. 数据定义: CREATE、DROP、ALTER

热门文章

  1. Effective Java 第三版——50. 必要时进行防御性拷贝
  2. goland激活码
  3. 严苛模式 strictmode
  4. python开发-与其他语言的比较
  5. Linux SD/MMC/SDIO驱动分析_转
  6. 如何在Excel中提取小数点后面的数字?
  7. hdoj:2043
  8. 数据中心内负载均衡-ECMP的使用分析
  9. OpenResty 最佳实践 1
  10. (原)android系统下绑定Server的时候报MainActivity has leaked ServiceConnection的错误