您的位置:首页 > 其它

AVFoundation框架理论+实战一(文本语音转换)

2017-12-06 17:43 429 查看
前言:本专栏主要是AVFoundation开发秘籍一书的总结和学习。

下面是这本书的扫描版:链接: https://pan.baidu.com/s/1miy0K7A 密码: ateq  (仅供学习使用)


AVFoundation 相关知识

涉及类:

AVSpeechSynthesizer:

   这是语音播放的关键API类,相当于一个发声器,他可以播放一条一条AVSpeechUtterance对象。他还有一个AVSpeechSynthesizerDelegate可以监听播放的状态。

AVSpeechUtterance:

   这个类主要是一条一条话语,这些话语对象可以填充文本,语言,语速,音高等等,

AVSpeechSynthesisVoice:

   语言设置,如中文,英文等等

   

具体的API点进类中去看。下面开始实战。


文本转语音实战代码

目标:我想做一个在线读漫画的小例子



Demo地址:https://github.com/RainManGO/speechSynthesizerDemo.git

部分代码:

//
// ZYSpeechController.m
// AVSpeechDemo
//
// Created by apple on 2017/12/6.
// Copyright © 2017年 ZY. All rights reserved.
//

#import "ZYSpeechController.h"

@interface ZYSpeechController ()

@end

@implementation ZYSpeechController

+(instancetype)speechManager{
static ZYSpeechController * speechController;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
speechController = [[self alloc]init];
});

return speechController;
}

-(void)bulidSettings{
_speechSynthesizer = [AVSpeechSynthesizer new];
_speechVoices = @[[AVSpeechSynthesisVoice voiceWithLanguage:@"zh-CN"],[AVSpeechSynthesisVoice voiceWithLanguage:@"zh-TW"]];
_UtteranceTextArray = [self bulidStringsArray];
}

-(NSArray *)bulidStringsArray{
return @[@[@"干哈呀",@"王师傅,你家亲戚来找你了。",@"老板让你回去。",@"王胖子叔叔,",@"我爸爸是王瘸子,他让我来帝京找你当 学徒",@"/n"],@[@"咋!不认识我了?",@"我是王小二呀,咱们都不是王八屯的吗。",@"王胖子也是你叫的吗?",@"我知道你是个小混混,要不是你爸求我,我才不管你呢,",@"谢谢王叔收我为徒。",@"谢我没有用,收不收你要老板娘同意才行。",@"老板?",@"/n"],@[@"啥同不同意的,王师傅的小老乡哪能不要啊。",@"后厨正缺帮手呢。",@"哟,小伙张的还挺精神",@"给王师傅打下手,包吃包住,学徒期间一个月八百,行不?",@"行",@"/n"],@[@"难道老板娘又想老牛吃嫩草了?",@"我还没有受宠
dc5f
过呢。",@"你爸没有让你带什么东西吗?",@"啊,有,我差点忘了。",@"给,我爸说一次只能泡一片,不能多放。",@"/n"]];
}

-(void)begainSpeakWitnIndex:(NSUInteger)page{
[_speechSynthesizer stopSpeakingAtBoundary:AVSpeechBoundaryImmediate];
NSArray * pageStrings = _UtteranceTextArray[page-1];
for (NSUInteger i = 0; i < pageStrings.count; i++) {
AVSpeechUtterance *utterance =
[[AVSpeechUtterance alloc] initWithString:pageStrings[i]];
utterance.voice = _speechVoices[i % 2];
utterance.rate = 0.5f;
utterance.pitchMultiplier = 0.8f;
utterance.postUtteranceDelay = 0.1f;
[self.speechSynthesizer speakUtterance:utterance];
}
}

- (void)viewDidLoad {
[super viewDidLoad];
}

@end


//
// ViewController.m
// AVSpeechDemo
//
// Created by apple on 2017/12/6.
// Copyright © 2017年 ZY. All rights reserved.
//

#import "ViewController.h"
#import "ZYSpeechController.h"
#import <AVFoundation/AVFoundation.h>
@interface ViewController ()<AVSpeechSynthesizerDelegate>
@property (strong, nonatomic) ZYSpeechController * speechController;
@property (weak, nonatomic) IBOutlet UIImageView *imageView;
@property (assign, nonatomic)NSUInteger page;

@end

@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];
self.page = 1;
[self speakImageSetting];
}

-(void)speakImageSetting{
_speechController = [ZYSpeechController speechManager];
[_speechController bulidSettings];
[_speechController begainSpeakWitnIndex:self.page];
_speechController.speechSynthesizer.delegate = self;
}

- (IBAction)tapNext:(UITapGestureRecognizer *)sender {

[UIView transitionWithView:self.imageView duration:1.0
options:UIViewAnimationOptionTransitionCurlUp
animations:^{
//cycle to next image
[self pageCount];
}
completion:NULL];
}

/**
切换照片
*/
-(void)pageCount{
if (self.page<4) {
self.page++;
}else{
self.page =1;
}
self.imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"p%lu",(unsigned long)self.page]];
[_speechController begainSpeakWitnIndex:self.page];
}

-(void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didStartSpeechUtterance:(AVSpeechUtterance *)utterance{
if ([utterance.speechString isEqualToString:@"/n"]) {
[self tapNext:nil];
}
}

@end
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: