前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >AVFoundation框架理论+实战一(文本语音转换)

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

作者头像
星宇大前端
发布2019-01-15 15:09:32
1.2K0
发布2019-01-15 15:09:32
举报
文章被收录于专栏:大宇笔记大宇笔记

前言:本专栏主要是AVFoundation开发秘籍一书的总结和学习。

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

AVFoundation 相关知识

涉及类:

AVSpeechSynthesizer:

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

AVSpeechUtterance:

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

AVSpeechSynthesisVoice:

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

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

文本转语音实战代码

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

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

部分代码:

代码语言:javascript
复制
//
//  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"],@[@"难道老板娘又想老牛吃嫩草了?",@"我还没有受宠过呢。",@"你爸没有让你带什么东西吗?",@"啊,有,我差点忘了。",@"给,我爸说一次只能泡一片,不能多放。",@"/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
代码语言:javascript
复制
//
//  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
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2017年12月06日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • AVFoundation 相关知识
  • 文本转语音实战代码
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档