#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/   一款免费的识别工具

iOS10原生的语音转文字功能的更多相关文章

  1. iOS 10中如何搭建一个语音转文字框架

    在2016WWDC大会上,Apple公司介绍了一个很好的语音识别的API,那就是Speech framework.事实上,这个Speech Kit就是Siri用来做语音识别的框架.如今已经有一些可用的 ...

  2. 语音识别系统:有免费实用的"语音到文字"的软件么?

    自从看了<李开复自传>,就对"语音识别系统"产生了非常深刻的印象. 根据自己的判断,语音识别系统还是非常有用的. 以自己的实际需求来看: 1.中国象棋中的应用. 中国象 ...

  3. 原生JS实现购物车结算功能代码+zepto版

    html <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3 ...

  4. android中自定义view---实现竖直方向的文字功能,文字方向朝上,同时提供接口,判断当前touch的是哪个字符,并改变颜色

    android自定义view,实现竖直方向的文字功能,文字方向朝上,同时提供接口,判断当前touch的是哪个字符,并改变颜色. 由于时间比较仓促,因此没有对代码进行过多的优化,功能远远不如androi ...

  5. C# 语音识别(文字to语音、语音to文字)

    最近打算研究一下语音识别,但是发现网上很少有C#的完整代码,就把自己的学习心得放上来,和大家分享一下. 下载API: 1)SpeechSDK51.exe                   (67.0 ...

  6. 机器人之路的第一小步:录音+语音识别(语音转文字),大小600K(免费下载)!

    机器人之路的第一小步:录音+语音识别(语音转文字),大小600K,本人出品! 机器人之路的第一小步:录音+语音识别,准确率还不是特别高,不过普通话标准的话,识别准确率还是不错的,大家可以体验一下,请下 ...

  7. iOS语音播报文字

    记得大学的时候学微软Window Phone时,有语音识别类似苹果的嘿,Siri.今天无聊百度搜了一下,搜到苹果语音播报文字.自己试了下还挺好玩. 1.引入框架#import <AVFounda ...

  8. Python使用websocket调用语音识别,语音转文字

    @ 目录 0. 太长不看系列,直接使用 1. Python调用标贝科技语音识别websocket接口,实现语音转文字 1.1 环境准备: 1.2 获取权限 1.2.1 登录 1.2.2 创建新应用 1 ...

  9. 手把手使用Python语音识别,进行语音转文字

    0. 太长不看系列,直接使用 在1.2官网注册后拿到APISecret和APIKey,直接复制文章2.4demo代码,确定音频为wav格式,采样率为16K,在命令行执行 python single_s ...

随机推荐

  1. AaronYang WCF教程目录

    ============================原创,讲究实践===================== 1. 那天有个小孩教我WCF[一][1/3]     基本搭建      阅读    ...

  2. shell函数【参数传递及输入输出】&内置函数

    Linux——shell脚本基础3:shell函数[参数传递及输入输出]&内置函数 函数定义 1 退出状态 1 参数传递 2 标准IO 2 脚本调试 2 AND&OR 3 内置命令补充 ...

  3. Atitit 数据库view视图使用推荐规范与最佳实践与方法

    Atitit 数据库view视图使用推荐规范与最佳实践与方法 1. 视图的优点:1 1.1. **提升可读性  定制用户数据,聚焦特定的数据1 1.2. 使用视图,可以简化数据操作.       1 ...

  4. 一篇文章掌握RequireJS常用知识

    本文采取循序渐进的方式,从理论到实践,从RequireJS官方API文档中,总结出在使用RequireJS过程中最常用的一些用法,并对文档中不够清晰具体的内容,加以例证和分析,分享给大家供大家参考,具 ...

  5. 读取Excel二进制写入DB,并从DB中读取生成Excel文件

    namespace SendMailSMSService { class Program { static void Main(string[] args) { var connString = Sq ...

  6. 在webpack中使用postcss-px2rem的

    经过一番折腾重要搞定了. 首先需要安装postcss-plugin-px2rem. npm install --save-dev postcss-plugin-px2rem 我的webpack工程中没 ...

  7. Ubuntu18.04启用中文输入法

    Ubuntu18.04发布了,但是搜狗输入法目前尚未支持. 而18.04自带的中文输入法选择sunpinyin为预设中文输入引擎,但是并没有将sunpinyin放入iso镜像中.详情参考https:/ ...

  8. Python 的 Magic Methods 指南(转)

    介绍 本指南是数月博客的总结.主题是魔术方法. 什么是魔术方法呢?它们是面向对象Python语言中的一切.它们是你可以自定义并添加“魔法”到类中的特殊方法.它们被双下划线环绕(比如__init__或_ ...

  9. CMMI的敏捷开发

  10. 第四百节,Django+Xadmin打造上线标准的在线教育平台—生产环境部署CentOS6.5安装python3.5.1

    第四百节,Django+Xadmin打造上线标准的在线教育平台—生产环境部署CentOS6.5安装python3.5.1 1.检查系统是否安装了python [root@192 ~]# rpm -qa ...