首先使用的是科大讯飞的sdk

1.语音识别部分

AppDelegate.m

#import "AppDelegate.h"

#import <iflyMSC/iflyMSC.h>

@interface AppDelegate ()

@end

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

NSString *initString = [[NSString alloc] initWithFormat:@"appid=%@",@"你的app ID"];

[IFlySpeechUtility createUtility:initString];

return YES;

}

.h文件

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController

@end

.m实现文件

#import "ViewController.h"

#import <iflyMSC/iflyMSC.h>

@interface ViewController ()<IFlySpeechRecognizerDelegate>

{

//不带界面的识别对象

IFlySpeechRecognizer *iFlySpeechRecognizer;

}

@property(nonatomic,strong)UILabel * showLabel;

@end

@implementation ViewController

-(void)viewDidLoad{

[super viewDidLoad];

self.showLabel = [[UILabel alloc]initWithFrame:CGRectMake(8,200,300,200)];

self.showLabel.textColor = [UIColor redColor];

[self.view addSubview:self.showLabel];

//1.创建语音听写对象

iFlySpeechRecognizer = [IFlySpeechRecognizer sharedInstance]; //设置听写模式

iFlySpeechRecognizer.delegate = self;

[iFlySpeechRecognizer setParameter:@"iat" forKey:[IFlySpeechConstant IFLY_DOMAIN]];

//2.设置听写参数

[iFlySpeechRecognizer setParameter: @"iat" forKey: [IFlySpeechConstant IFLY_DOMAIN]];

//asr_audio_path是录音文件名,设置value为nil或者为空取消保存,默认保存目录在 Library/cache下。

[iFlySpeechRecognizer setParameter:@"asrview.pcm" forKey:[IFlySpeechConstant ASR_AUDIO_PATH]];

UIButton * startButton = [[UIButton alloc]initWithFrame:CGRectMake(20,100,100,50)];

[startButton addTarget:self action:@selector(startButtonDidClick) forControlEvents:UIControlEventTouchUpInside];

startButton.backgroundColor = [UIColor orangeColor];

[startButton setTitle:@"开始录音" forState:UIControlStateNormal];

[self.view addSubview:startButton];

UIButton * endButton = [[UIButton alloc]initWithFrame:CGRectMake(200,100,100,50)];

[endButton addTarget:self action:@selector(endButtonDidClick) forControlEvents:UIControlEventTouchUpInside];

endButton.backgroundColor = [UIColor orangeColor];

[endButton setTitle:@"结束录音" forState:UIControlStateNormal];

[self.view addSubview:endButton];

}

-(void)startButtonDidClick{

//3.启动识别服务

[iFlySpeechRecognizer startListening];

}

-(void)endButtonDidClick{

//识别服务

[iFlySpeechRecognizer stopListening];

}

- (void) onResults:(NSArray *) results isLast:(BOOL)isLast{

NSMutableString * resultString = [[NSMutableString alloc]init];

if (!isLast) {

NSDictionary *dic = results[0];

NSArray * keys = [dic allKeys];

for (NSString *key in keys) {

NSData * resData = [key dataUsingEncoding:NSUTF8StringEncoding];

NSDictionary * resultFromJson =  [NSJSONSerialization JSONObjectWithData:resData options:NSJSONReadingAllowFragments error:nil];

NSArray * tempArray = resultFromJson[@"ws"];

for (NSDictionary * tempDic in tempArray) {

NSArray * cwArray = tempDic[@"cw"];

for (NSDictionary * resultDic in cwArray) {

NSString * str = [NSString stringWithFormat:@"%@",resultDic[@"w"]];

[resultString appendString:str];

}

}

}

self.showLabel.text = [NSString stringWithFormat:@"%@",resultString];

}

}

/*识别会话结束返回代理

@ param error 错误码,error.errorCode=0表示正常结束,非0表示发生错误。 */

- (void)onError: (IFlySpeechError *) error{

NSLog(@"%@",error.errorDesc);

}

/**

停止录音回调

****/

- (void) onEndOfSpeech {

}

/**

开始识别回调

****/

- (void) onBeginOfSpeech {

}

/**

音量回调函数 volume 0-30

****/

- (void) onVolumeChanged: (int)volume {

}

@end

2  文字变语音 语音合成播报部分

首先写一个工具当然这个不是我写的,能用^_^

.h 文件如下

#import <UIKit/UIKit.h>

#import <AVFoundation/AVSpeechSynthesis.h>

@interface SpeechSynthesizer : NSObject

+ (instancetype)sharedSpeechSynthesizer;

- (void)speakString:(NSString *)string;

- (void)stopSpeak;

@end

.m 文件如下

#import "SpeechSynthesizer.h"

@interface SpeechSynthesizer () <AVSpeechSynthesizerDelegate>

@property (nonatomic, strong, readwrite) AVSpeechSynthesizer *speechSynthesizer;

@end

@implementation SpeechSynthesizer

+ (instancetype)sharedSpeechSynthesizer

{

static id sharedInstance = nil;

static dispatch_once_t onceToken;

dispatch_once(&onceToken, ^{

sharedInstance = [[SpeechSynthesizer alloc] init];

});

return sharedInstance;

}

- (instancetype)init

{

if (self = [super init])

{

[self buildSpeechSynthesizer];

}

return self;

}

- (void)buildSpeechSynthesizer

{

if ([[[UIDevice currentDevice] systemVersion] floatValue] < 7.0)

{

return;

}

self.speechSynthesizer = [[AVSpeechSynthesizer alloc] init];

[self.speechSynthesizer setDelegate:self];

}

- (void)speakString:(NSString *)string

{

if (self.speechSynthesizer)

{

AVSpeechUtterance *aUtterance = [AVSpeechUtterance speechUtteranceWithString:string];

[aUtterance setVoice:[AVSpeechSynthesisVoice voiceWithLanguage:@"zh-CN"]];

//iOS语音合成在iOS8及以下版本系统上语速异常

if ([[[UIDevice currentDevice] systemVersion] floatValue] < 8.0)

{

aUtterance.rate = 0.25;//iOS7设置为0.25

}

else if ([[[UIDevice currentDevice] systemVersion] floatValue] < 9.0)

{

aUtterance.rate = 0.15;//iOS8设置为0.15

}

if ([self.speechSynthesizer isSpeaking])

{

[self.speechSynthesizer stopSpeakingAtBoundary:AVSpeechBoundaryWord];

}

[self.speechSynthesizer speakUtterance:aUtterance];

}

}

- (void)stopSpeak

{

if (self.speechSynthesizer)

{

[self.speechSynthesizer stopSpeakingAtBoundary:AVSpeechBoundaryImmediate];

}

}

@end

使用方法如下

语音播报“世界那么大我想去看看”

[[SpeechSynthesizer sharedSpeechSynthesizer] speakString:@"世界那么大我想去看看"];

停止播报

[[SpeechSynthesizer sharedSpeechSynthesizer] stopSpeak];

iOS语音识别,语音播报,文字变语音播报,语音变文字的更多相关文章

  1. iOS后台唤醒实战:微信收款到账语音提醒技术总结

    1.前言 微信为了解决小商户老板们在频繁交易中不方便核对.确认到账的功能痛点,产品MM提出了新版本需要支持收款到账语音提醒功能.本文借此总结了iOS平台上的APP后台唤醒和语音合成.播放等一系列技术开 ...

  2. 当语音识别搭配AI之后,我的语音助手更懂我的心了

    欢迎大家前往腾讯云+社区,获取更多腾讯海量技术实践干货哦~ 本文由腾讯云AI中心发表于云+社区专栏 我今天演讲主要分四个部分,第一个是分享语音识别概述,然后是深度神经网络的基础:接下来就是深度学习在语 ...

  3. 实时语音趣味变声,大叔变声“妙音娘子”Get一下

    欢迎大家前往腾讯云+社区,获取更多腾讯海量技术实践干货哦~ 本文由腾讯游戏云 发表于云+社区专栏 游戏社交化是近年来游戏行业发展的重要趋势,如何提高游戏的社交属性已成为各大游戏厂商游戏策划的重要组成部 ...

  4. SLAM+语音机器人DIY系列:(七)语音交互与自然语言处理——1.语音交互相关技术

    摘要 这一章将进入机器人语音交互的学习,让机器人能跟人进行语音对话交流.这是一件很酷的事情,本章将涉及到语音识别.语音合成.自然语言处理方面的知识.本章内容: 1.语音交互相关技术 2.机器人语音交互 ...

  5. 离线语音Snowboy热词唤醒+ 树莓派语音交互实现开关灯

    离线语音Snowboy热词唤醒 语音识别现在有非常广泛的应用场景,如手机的语音助手,智能音响(小爱,叮咚,天猫精灵...)等. 语音识别一般包含三个阶段:热词唤醒,语音录入,识别和逻辑控制阶段. 热词 ...

  6. 论文笔记:语音情感识别(五)语音特征集之eGeMAPS,ComParE,09IS,BoAW

    一:LLDs特征和HSFs特征 (1)首先区分一下frame和utterance,frame就是一帧语音.utterance是一段语音,是比帧高一级的语音单位,通常指一句话,一个语音样本.uttera ...

  7. php如何清除html格式并去除文字中的空格然后截取文字

    PHP如何清除html格式并去除文字中的空格然后截取文字,详细分享一下处理方法(顺便对PHP清除HTML字符串的函数做了一个小结): htmlspecialchars 将特殊字元转成 HTML格式语法 ...

  8. C#+OpenGL+FreeType显示3D文字(2) - 用GLSL+VBO绘制文字

    C#+OpenGL+FreeType显示3D文字(2) - 用GLSL+VBO绘制文字 +BIT祝威+悄悄在此留下版了个权的信息说: 上一篇得到了字形贴图及其位置字典(可导出为XML).本篇就利用此贴 ...

  9. UIButton图片文字控件位置自定义(图片居右文字居左、图片居中文字居中、图片居左文字消失等)

    在开发中经常会碰到需要对按钮中的图片文字位置做调整的需求.第一种方式是通过设置按钮中图片文字的偏移量.通过方法setTitleEdgeInsets和setImageEdgeInsets实现 代码如下: ...

随机推荐

  1. Swift中面向协议的编程

    什么是面向协议的编程? 面向协议的编程,是一种编程范式. 编程范式,是一个计算机科学用语.维基百科中的解释是,计算机编程的基本风格或典型模式.通俗来说,就是解决某一个问题的方法不同方法和思路. 像大家 ...

  2. poj 3671 Dining Cows (Dp)

    /* 一开始并没有想出On的正解 后来发现题解的思路也是十分的巧妙的 还是没能把握住题目的 只有1 2这两个数的条件 dp还带练练啊 ... */ #include<iostream> # ...

  3. HTTPS 部署简要指南

    许多Web开发者都知道SSL,但常见的情况是SSL没有完整地部署或者没有部署在它应该部署的地方.这篇关于何时及如何部署SSL的简要指南,将帮助你避免大多数常见错误. 要点 如果你有任何机密信息,或者你 ...

  4. PHP 执行系统外部命令 system() exec() passthru()

    区别: system() 输出并返回最后一行shell结果. exec() 不输出结果,返回最后一行shell结果,所有结果可以保存到一个返回的数组里面. passthru() 只调用命令,把命令的运 ...

  5. nodejs+express 4.x笔记

    4.x与3.x变化比较大,包括安装以及api 一:安装express4.x 1. npm install express -g //express modules2. npm install expr ...

  6. log4j的配置及使用

    用日志的好处: 可以长久的保存日志信息. 日志可以保存到:网络.文件.数据库 设置日志的级别. OFF Fatal – System.exit(0); - JVM, ERROR – 错误,模块错误. ...

  7. C/C++安全编码-字符串

    1 字符串     1.1 字符串基础 字符串提供命令行参数.环境变量.控制台输入.文本文件及网络连 接,提供外部输入方法来影响程序的行为和输出,这也是程序容易出错的地方.字符串是一个概念,并不是C/ ...

  8. ajax提交表单序列化(serialize())数据

    知识点: $("#form").serialize();将表单数据序列化为标准URL编码文本字符串(key1=value1&key2=value2…). 以下用一个例子来演 ...

  9. linux笔记2.21

    命令dmesg显示本次内核启动信息 init是系统运行的第一个进程 Linux运行级别: 0   关机 1   单用户模式 2   不带网络的多用户模式 3   命令行多用户模式  4   未使用 5 ...

  10. ThinkPHP实现RBAC

    RBAC: role base access control   基于角色的用户访问权限控制 不同人员登录系统要显示不同的菜单项目 1.传统方式权限设置: 具体操作权限与用户直接联系: