iOS中语音合成技术

这里写图片描述

#一 摘要
今天讲啥呢,今天给你们介绍一个你们平时不常用的系统自带的语音合成控件,具体是什么控件呢,是AVSpeechSynthesizer.web

这里写图片描述
#二 发展史
首先咱们来看看它的发展史,要说它的来历,出现的时间比较早,从IOS5开始,IOS系统已经在siri上集成了语音合成的功能,可是那时候所有是私有API。可是在IOS7,就新增了一个简单的API.能够实现无网络语音功能,支持多种语言.
你们看到这里必定很是想看看它的具体使用是什么,不要着急,先让你们看看它最终效果,这样你们会更有兴趣.
这里写图片描述微信

这里是有声音的,gif没办法把声音也保存起来,你们只看到了文字语言的切换,你们若是是感兴趣,你们能够把代码下载下来本身运行一下看一下顺便也听一下效果.好了,如今咱们来看看具体的实现.网络

#三 具体实现框架

1.首先咱们须要准备一个plist文件,里面包含语言类型,内容,国家.如图所示
这里写图片描述async

2.plist文件的读取svg

NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"Language" ofType:@"plist"];
    NSDictionary *dictionary = [[NSDictionary alloc] initWithContentsOfFile:plistPath];

3.导入AVFoundation/AVFoundation.h框架atom

注意:语音合成相关的API都是在AVFoundation/AVFoundation.h这个框架下面的.
#import <AVFoundation/AVFoundation.h>

4.定义一个成员变量,记录语音合成器 AVSpeechSynthesizerspa

AVSpeechSynthesizer *av = [[AVSpeechSynthesizer alloc]init];

5.定义语音对象 AVSpeechSynthesisVoice
其中这里的语言类型,咱们是从plist文件里面获取的..net

AVSpeechSynthesisVoice *voice = [AVSpeechSynthesisVoice voiceWithLanguage:[dic objectForKey:@"local"]];

6.实例化发声对象 AVSpeechUtterance,指定要朗读的内容code

AVSpeechUtterance *utterance = [[AVSpeechUtterance alloc]initWithString:[dic objectForKey:@"content"]];

7.指定语音和朗诵速度 
               
中文朗诵速度:0.1还可以接受
英文朗诵速度:0.3还能够

utterance.voice = voice;
            utterance.rate *= 0.7;

8.启动

[av speakUtterance:utterance];

你们看到这里 ,用法是否是很是简单,好了你们看一下完整的代码.

//
//  ViewController.m
//  ReadDiffrentLanguages
//
//  Created by Apple on 16/5/22.
//  Copyright © 2016年 liuYuGang. All rights reserved.
//

#import "ViewController.h"
#import <AVFoundation/AVFoundation.h>
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UILabel *labL;
@property (weak, nonatomic) IBOutlet UILabel *textL;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"f66129f9-6.jpg"]];
    // Do any additional setup after loading the view, typically from a nib.
}
- (IBAction)speak:(id)sender {

    NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"Language" ofType:@"plist"];
    NSDictionary *dictionary = [[NSDictionary alloc] initWithContentsOfFile:plistPath];
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        for (int i=0; i<[dictionary count]; i++) {
            NSDictionary *dic = [dictionary objectForKey:[NSString stringWithFormat:@"%d",i]];
            AVSpeechSynthesisVoice *voice = [AVSpeechSynthesisVoice voiceWithLanguage:[dic objectForKey:@"local"]];
            AVSpeechSynthesizer *av = [[AVSpeechSynthesizer alloc]init];
            AVSpeechUtterance *utterance = [[AVSpeechUtterance alloc]initWithString:[dic objectForKey:@"content"]];
            utterance.voice = voice;
            utterance.rate *= 0.7;
            [av speakUtterance:utterance];
            dispatch_async(dispatch_get_main_queue(), ^{
                _labL.text = [dic objectForKey:@"name"];
                _textL.text = [dic objectForKey:@"content"];
            });
            [NSThread sleepForTimeInterval:3.5];
        }
    });

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#四 效果
这里写图片描述

#五 结论

是否是很是简单,你们能够本身下下来本身运行一下看看效果.
源码:
http://download.csdn.net/detail/baihuaxiu123/9528396
你们技术交流能够关注我我的微信公众号:
这里写图片描述