AVFoundation框架理論+實戰一(文字語音轉換)

ZY_FlyWay發表於2017-12-06

前言:本專欄主要是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"],@[@"難道老闆娘又想老牛吃嫩草了?",@"我還沒有受寵過呢。",@"你爸沒有讓你帶什麼東西嗎?",@"啊,有,我差點忘了。",@"給,我爸說一次只能泡一片,不能多放。",@"/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



相關文章