#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. php write_ini_file

    php读ini文件有很方便的pares_ini_file,但是写回去却没有,这里写一个: function write_ini_file($assoc_arr, $path, $has_section ...

  2. IntelliJ IDEA的配置优化

    IntelliJ IDEA的配置优化 我们安装完IntelliJ IDEA之后,在弹出的欢迎页面下方点击Configure,选择Setting,打开以下界面,我们在这个界面中进行配置. Appeara ...

  3. rand()产生随机数 及其和clock()的不同

    rand()使用 首先我们要对rand&srand有个总体的看法:srand初始化随机种子,rand产生随机数. 定义函数 : int rand(void) 函数说明 :因为rand的内部实现 ...

  4. 20款有用的JavaScript和CSS库

    Web开发与设计已经发展达到了新的高峰.创建一个网站并不是那么简单了,因为它使用的是几年前.今天是时间创造有吸引力的网站,不仅吸引了游客的关注也让他们订婚.另外,和功能的有吸引力的功能添加到该网站不应 ...

  5. 解决Redis之MISCONF Redis is configured to save RDB snapshots, but is currently not able to persist o...

    解决Redis之MISCONF Redis is configured to save RDB snapshots, but is currently not able to persist o... ...

  6. Git 推送操作

    Jerry 修改了他的最后一次提交的修改操作,他已经准备好将更改.推操作的数据永久存储的 Git 仓库.推操作成功后,其他开发人员可以看到Jerry 的变化. 他执行的git日志命令来查看提交的细节. ...

  7. Java Utils工具类大全(转)

    源码和jar见:https://github.com/evil0ps/utils #Java Utils --- 封装了一些常用Java操作方法,便于重复开发利用. 另外希望身为Java牛牛的你们一起 ...

  8. 解决pymongo里操作IOSDate类型的问题

    pymongo是Python对MongoDB的操作库.但是由于python没有IOSDate类型,所以对Mongo的时间类型是个很麻烦的操作.整理一个把python能识别的date类型转化为IOSDa ...

  9. 错误代码CS0051可访问性不一致_解决方案

    一.问题的出现 用C#在写多线程时报错 二.解决方案 1,分析思路 本来对BaseStruct设置为私有访问,但调用时又想公开化,从而造成了编译错误. 2,解决 将红色部分改为公有 3,总结 注意pu ...

  10. json转 PostMan bulk Edit application/x-www-form-urlencoded 需要的格式

    {?\n\s*"([^"]+)"\s*:\s*"?([^,"]+)"?\s*,?}? $1:$2\n PostMan需要的格式. json转 ...