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
原创粉丝点击