AVFoundation视听技术学习

最近赶上京东618活动, 感觉终于可以买书了, 一口气买了8本,发现桌子腿再也不会不平了. 😄😄

第一本AVFoundation 视听技术学习. 与时俱进的同时一定要注意基础的东西哦.
第一章的有些东西太理论, 必须有一定大学知识基础或者波形基础的人才能看的懂, 这里我也是讲的不太明白,所以需要大家去慢慢学习慢慢看了.

代码第一部分.
初识AVFoundation ->NSSpeechSynthesizer 主要功能就是 控制着文本转语音单元的播放, 综合解析器吧

为什么我把称之为综合语音播放解析器, 看里面的API 应该就会有一定的理解了吧.

@interface AVSpeechSynthesizer : NSObject
// 代理 主要是对 语音单元播放状态的 监听 
@property(nonatomic, assign, nullable) 
id<AVSpeechSynthesizerDelegate> delegate;

// 这里 我们能得到 播放的状态.  为什么是只读属性呢,  我觉得他是 以 音频单元 为点进行播放, 中途不可以采用 这些 . 后面的属性有进行说明
@property(nonatomic, readonly, getter=isSpeaking) BOOL speaking;
@property(nonatomic, readonly, getter=isPaused) BOOL paused;

/* AVSpeechUtterances are queued by default. 
   Enqueing the same AVSpeechUtterance that is already enqueued or is speaking will raise an exception. */
// 读取 单元语音,  默认添加  就读取
- (void)speakUtterance:(AVSpeechUtterance *)utterance;

/* These methods will operate on the speech utterance that is speaking. Returns YES if it succeeds, NO for failure. */

/* Call stopSpeakingAtBoundary: to interrupt current speech and clear the queue. */
//  对于 语音单元的操作
- (BOOL)stopSpeakingAtBoundary:(AVSpeechBoundary)boundary;
- (BOOL)pauseSpeakingAtBoundary:(AVSpeechBoundary)boundary;
- (BOOL)continueSpeaking;

@end

AVSpeechSynthesizerDelegate 代理方法

@protocol AVSpeechSynthesizerDelegate <NSObject>
// 代理方法
@optional
// 开始播放 语音单元
- (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didStartSpeechUtterance:(AVSpeechUtterance *)utterance;
// 完成播放 语音单元
- (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didFinishSpeechUtterance:(AVSpeechUtterance *)utterance;
// 暂停播放 语音单元
- (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didPauseSpeechUtterance:(AVSpeechUtterance *)utterance;
// 继续播放 语音单元
- (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didContinueSpeechUtterance:(AVSpeechUtterance *)utterance;
//取消播放 语音单元
- (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didCancelSpeechUtterance:(AVSpeechUtterance *)utterance;
// 这里 指的是  又来监听 播放 字符范围
- (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer willSpeakRangeOfSpeechString:(NSRange)characterRange utterance:(AVSpeechUtterance *)utterance;
@end

好了API 基本解读完毕, 这里看到了, 我想要进行 文本转换语音, 必须要有 综合语音解析器, 语音单元的添加. 才可以进行这一项工作.
那么什么是语音单元 AVSpeechUtterance API是这样利用的:

@interface AVSpeechUtterance : NSObject<NSCopying, NSSecureCoding>
// 类方法, 工厂构造器 , 利用 String字符串  创建音频单元
+ (instancetype)speechUtteranceWithString:(NSString *)string;
// 初始化方法
- (instancetype)initWithString:(NSString *)string;

/* If no voice is specified, the system's default will be used. */
// 设置 语音解析器 所要用的 语言, en-us  en-gb  zh-cn等等 , 英语, 美式英语, 中文. 当然我喜欢中文
@property(nonatomic, retain, nullable) AVSpeechSynthesisVoice *voice;
//  获取 语音单元的 String,  这里就能看出来 , 此类不允许单独给String
@property(nonatomic, readonly) NSString *speechString;

/* Setting these values after a speech utterance has been enqueued will have no effect. */
// 以下的设置都是 对语音的单项设置

// 语速
@property(nonatomic) float rate;             // Values are pinned between AVSpeechUtteranceMinimumSpeechRate and AVSpeechUtteranceMaximumSpeechRate.
// 语调
@property(nonatomic) float pitchMultiplier;  // [0.5 - 2] Default = 1
// 声音
@property(nonatomic) float volume;           // [0-1] Default = 1
// 前置延迟
@property(nonatomic) NSTimeInterval preUtteranceDelay;    // Default is 0.0
// 后置延迟
@property(nonatomic) NSTimeInterval postUtteranceDelay;   // Default is 0.0

@end

经过这么一个分析, 就会发现 文本转语音是如此的简单

//
//  THSpeechController.m
//  文本到语音的转换
//
//  Created by 博兴 on 16/6/22.
//  Copyright © 2016年 bx. All rights reserved.
//

#import "THSpeechController.h"
@interface THSpeechController()
// 外部提供接口, 内部重写属性的话, 是的内外用法不同
@property(nonatomic, strong) AVSpeechSynthesizer *synthesizer;

@property(strong, nonatomic) NSArray *speechStrings;
@end

@implementation THSpeechController

- (instancetype)init{
    
    if([super init]){
        // 初始化话 语音综合器
        self.synthesizer = [[AVSpeechSynthesizer alloc]init];
        self.speechStrings = @[@"我爱你",@"中国",@"中国"];
        
    }
    return self;
}
+ (instancetype)speechController
{
    return [[self alloc]init];
}
- (void)beginConversation
{
    for (NSString *str in self.speechStrings) {
        AVSpeechUtterance *utterance = [AVSpeechUtterance speechUtteranceWithString:str];
        utterance.voice = [AVSpeechSynthesisVoice voiceWithLanguage:@"zh-CN"];
//        utterance.postUtteranceDelay = 1.0f;
        [self.synthesizer speakUtterance:utterance];
    }

}
@end
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,290评论 4 363
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,399评论 1 294
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 109,021评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,034评论 0 207
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,412评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,651评论 1 219
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,902评论 2 313
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,605评论 0 199
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,339评论 1 246
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,586评论 2 246
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,076评论 1 261
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,400评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,060评论 3 236
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,083评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,851评论 0 195
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,685评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,595评论 2 270

推荐阅读更多精彩内容