最近趕上京東618活動(dòng), 感覺(jué)終于可以買書了, 一口氣買了8本,發(fā)現(xiàn)桌子腿再也不會(huì)不平了. ????
第一本AVFoundation 視聽技術(shù)學(xué)習(xí). 與時(shí)俱進(jìn)的同時(shí)一定要注意基礎(chǔ)的東西哦.
第一章的有些東西太理論, 必須有一定大學(xué)知識(shí)基礎(chǔ)或者波形基礎(chǔ)的人才能看的懂, 這里我也是講的不太明白,所以需要大家去慢慢學(xué)習(xí)慢慢看了.
代碼第一部分.
初識(shí)AVFoundation ->NSSpeechSynthesizer 主要功能就是 控制著文本轉(zhuǎn)語(yǔ)音單元的播放, 綜合解析器吧
為什么我把稱之為綜合語(yǔ)音播放解析器, 看里面的API 應(yīng)該就會(huì)有一定的理解了吧.
@interface AVSpeechSynthesizer : NSObject
// 代理 主要是對(duì) 語(yǔ)音單元播放狀態(tài)的 監(jiān)聽
@property(nonatomic, assign, nullable)
id<AVSpeechSynthesizerDelegate> delegate;
// 這里 我們能得到 播放的狀態(tài). 為什么是只讀屬性呢, 我覺(jué)得他是 以 音頻單元 為點(diǎn)進(jìn)行播放, 中途不可以采用 這些 . 后面的屬性有進(jìn)行說(shuō)明
@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. */
// 讀取 單元語(yǔ)音, 默認(rèn)添加 就讀取
- (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. */
// 對(duì)于 語(yǔ)音單元的操作
- (BOOL)stopSpeakingAtBoundary:(AVSpeechBoundary)boundary;
- (BOOL)pauseSpeakingAtBoundary:(AVSpeechBoundary)boundary;
- (BOOL)continueSpeaking;
@end
AVSpeechSynthesizerDelegate 代理方法
@protocol AVSpeechSynthesizerDelegate <NSObject>
// 代理方法
@optional
// 開始播放 語(yǔ)音單元
- (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didStartSpeechUtterance:(AVSpeechUtterance *)utterance;
// 完成播放 語(yǔ)音單元
- (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didFinishSpeechUtterance:(AVSpeechUtterance *)utterance;
// 暫停播放 語(yǔ)音單元
- (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didPauseSpeechUtterance:(AVSpeechUtterance *)utterance;
// 繼續(xù)播放 語(yǔ)音單元
- (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didContinueSpeechUtterance:(AVSpeechUtterance *)utterance;
//取消播放 語(yǔ)音單元
- (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didCancelSpeechUtterance:(AVSpeechUtterance *)utterance;
// 這里 指的是 又來(lái)監(jiān)聽 播放 字符范圍
- (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer willSpeakRangeOfSpeechString:(NSRange)characterRange utterance:(AVSpeechUtterance *)utterance;
@end
好了API 基本解讀完畢, 這里看到了, 我想要進(jìn)行 文本轉(zhuǎn)換語(yǔ)音
, 必須要有 綜合語(yǔ)音解析器, 語(yǔ)音單元的添加. 才可以進(jìn)行這一項(xiàng)工作.
那么什么是語(yǔ)音單元 AVSpeechUtterance
API是這樣利用的:
@interface AVSpeechUtterance : NSObject<NSCopying, NSSecureCoding>
// 類方法, 工廠構(gòu)造器 , 利用 String字符串 創(chuàng)建音頻單元
+ (instancetype)speechUtteranceWithString:(NSString *)string;
// 初始化方法
- (instancetype)initWithString:(NSString *)string;
/* If no voice is specified, the system's default will be used. */
// 設(shè)置 語(yǔ)音解析器 所要用的 語(yǔ)言, en-us en-gb zh-cn等等 , 英語(yǔ), 美式英語(yǔ), 中文. 當(dāng)然我喜歡中文
@property(nonatomic, retain, nullable) AVSpeechSynthesisVoice *voice;
// 獲取 語(yǔ)音單元的 String, 這里就能看出來(lái) , 此類不允許單獨(dú)給String
@property(nonatomic, readonly) NSString *speechString;
/* Setting these values after a speech utterance has been enqueued will have no effect. */
// 以下的設(shè)置都是 對(duì)語(yǔ)音的單項(xiàng)設(shè)置
// 語(yǔ)速
@property(nonatomic) float rate; // Values are pinned between AVSpeechUtteranceMinimumSpeechRate and AVSpeechUtteranceMaximumSpeechRate.
// 語(yǔ)調(diào)
@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
經(jīng)過(guò)這么一個(gè)分析, 就會(huì)發(fā)現(xiàn) 文本轉(zhuǎn)語(yǔ)音是如此的簡(jiǎn)單
//
// THSpeechController.m
// 文本到語(yǔ)音的轉(zhuǎn)換
//
// Created by 博興 on 16/6/22.
// Copyright ? 2016年 bx. All rights reserved.
//
#import "THSpeechController.h"
@interface THSpeechController()
// 外部提供接口, 內(nèi)部重寫屬性的話, 是的內(nèi)外用法不同
@property(nonatomic, strong) AVSpeechSynthesizer *synthesizer;
@property(strong, nonatomic) NSArray *speechStrings;
@end
@implementation THSpeechController
- (instancetype)init{
if([super init]){
// 初始化話 語(yǔ)音綜合器
self.synthesizer = [[AVSpeechSynthesizer alloc]init];
self.speechStrings = @[@"我愛(ài)你",@"中國(guó)",@"中國(guó)"];
}
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