iOS AVSpeechSynthesizer进行文字转语音
来源:互联网 发布:java jdk下载 列表 编辑:程序博客网 时间:2024/05/05 11:25
AVSpeechSynthesizer是iOS 7在AVFoundation中新增的语音合成API。
文字转语音的大致识别过程是 文字 —> siri —> 后台云 —> Foundation
在使用AVSpeechSynthesizer前需要导入头文件#import <AVFoundation/AVFoundation.h>
1、初始化语音合成器(相当于说话的嘴巴)
AVSpeechSynthesizer *synthesizer = [AVSpeechSynthesizer new];
2、设置语音合成器可识别的语言(相当于说话的语言)
//语音合成器可识别的语言_voices = @[[AVSpeechSynthesisVoice voiceWithLanguage:@"en-US"],[AVSpeechSynthesisVoice voiceWithLanguage:@"zh-CN"]];
3、设置语音发声器(相当于说话的舌头)
AVSpeechUtterance *utt = [[AVSpeechUtterance alloc]initWithString:speechVoices[i]]; //对舌头进行设置 //设置声音是播放中文还是英文(单数播放英文,双数播放中文) utt.voice = self.voices[i%2]; // 设置速率(0- 1) utt.rate = 0.4; // 设置音调(0-1) utt.pitchMultiplier = 0.8; // 设置延迟处理 utt.postUtteranceDelay = 0.2; //说话 [synthesizer speakUtterance:utt];
以上便是利用AVSpeechSynthesizer进行文字转语音的大致过程。
简单demo:
创建一个简单的语音识别类:
SpeechController.h
#import <Foundation/Foundation.h>#import <AVFoundation/AVFoundation.h>/** * 这是一个文本转语音的类 */@interface SpeechController : NSObject// 创建一个语音合成器(嘴巴)@property (nonatomic,strong,readonly) AVSpeechSynthesizer *synthesizer;// 创建一个类方法来管理语音合成器+ (instancetype) speechController;// 文本转语音的方法- (void)beginSpeechWithStrings:(NSArray<NSString *>*)speechVoices;@end
SpeechController.m
#import "SpeechController.h"@interface SpeechController ()//声音识别的语言@property (nonatomic,strong) NSArray *voices;@endstatic SpeechController * shareSelf = nil;@implementation SpeechController#pragma mark - 创建一个类方法来管理语音合成器+ (instancetype) speechController{ static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ shareSelf = [[self alloc]init]; }); return shareSelf;}- (instancetype)init{ self = [super init]; if (self) { //嘴巴 _synthesizer = [AVSpeechSynthesizer new]; //语音合成器可识别的语言 _voices = @[[AVSpeechSynthesisVoice voiceWithLanguage:@"en-US"],[AVSpeechSynthesisVoice voiceWithLanguage:@"zh-CN"]]; } return self;}#pragma mark - 文本转语音的方法- (void)beginSpeechWithStrings:(NSArray<NSString *>*)speechVoices{ //数组中有几句话就要说几次 for (int i = 0; i<speechVoices.count; i++) { //舌头 AVSpeechUtterance *utt = [[AVSpeechUtterance alloc]initWithString:speechVoices[i]]; //对舌头进行设置 //设置声音是播放中文还是英文(单数播放英文,双数播放中文) utt.voice = self.voices[i%2]; // 设置速率(0- 1) utt.rate = 0.4; // 设置音调(0-1) utt.pitchMultiplier = 0.8; // 设置延迟处理 utt.postUtteranceDelay = 0.2; //说话 [self.synthesizer speakUtterance:utt]; }}@end
调用过程:
#import "FirstTableViewController.h"#import "SpeechController.h"@interface FirstTableViewController ()<AVSpeechSynthesizerDelegate>@property(nonatomic,strong) NSMutableArray *speechStrings;@end@implementation FirstTableViewController- (void)viewDidLoad { [super viewDidLoad]; self.speechStrings = [NSMutableArray array]; SpeechController *spcontor = [SpeechController speechController]; spcontor.synthesizer.delegate = self; [spcontor beginSpeechWithStrings:@[@"hello",@"常老师",@"短老师"]];}- (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated.}#pragma mark - Table view data source- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return self.speechStrings.count;}- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *ID = @"cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID]; if (cell == nil) { cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID]; } cell.textLabel.text = self.speechStrings[indexPath.row]; return cell;}- (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didStartSpeechUtterance:(AVSpeechUtterance *)utterance{ // 把每一句要转化的文本放入数组中 [self.speechStrings addObject:utterance.speechString]; // 刷新数据 [self.tableView reloadData]; // 数据回滚 NSIndexPath *indexpath = [NSIndexPath indexPathForRow:self.speechStrings.count - 1 inSection:0]; [self.tableView scrollToRowAtIndexPath:indexpath atScrollPosition:UITableViewScrollPositionBottom animated:YES];}
1 0
- iOS AVSpeechSynthesizer进行文字转语音
- 文字转语音AVSpeechSynthesizer
- IOS开发笔记之文字转语音AVSpeechSynthesizer用法
- ios 文本转语音 播报 AVSpeechSynthesizer
- iOS-AVSpeechSynthesizer语音合成
- iOS 语音播放文字内容--制作简易听书软件(AVSpeechSynthesizer)
- AVSpeechSynthesizer文字转语音播放(需真机)详解
- iOS用AVSpeechSynthesizer合成语音
- iOS 语音合成 AVSpeechSynthesizer详解
- iOS7 文本转语音 AVSpeechSynthesizer
- iOS7 文本转语音 AVSpeechSynthesizer
- iOS- AVSpeechSynthesizer——iOS7语音合成器
- iOS- AVSpeechSynthesizer——iOS7语音合成器
- iOS- AVSpeechSynthesizer——iOS7语音合成器
- iOS 文字转语音
- iOS文字 转 语音
- iOS-文字转语音
- iOS文字转语音播放
- webclient上传数据到ashx服务
- 关于基于http的嵌入式linux与服务器的get,post解析理解
- tomcat server.xml中文版
- Linux使用jstat命令查看jvm的GC情况
- 块作用域来申明function防止污染全局变量
- iOS AVSpeechSynthesizer进行文字转语音
- 一步一步教你用优雅的Direct3D11代码画一个三角形
- Ubuntu下查看软件版本及安装位置
- 配置hadoop2.4.0记录
- jap页面静态化和伪静态化《转载》
- Lambda&Java多核编程-5-函数式接口与function包
- ubuntu16.04下cuda8.0+cudnn5.1+tensorflow安装
- 英文论文摘要写法心得
- centos上SVN安装与使用命令