AVFoundation視聽技術(shù)學(xué)習(xí)

最近趕上京東618活動, 感覺終于可以買書了, 一口氣買了8本,發(fā)現(xiàn)桌子腿再也不會不平了. ????

第一本AVFoundation 視聽技術(shù)學(xué)習(xí). 與時俱進的同時一定要注意基礎(chǔ)的東西哦.
第一章的有些東西太理論, 必須有一定大學(xué)知識基礎(chǔ)或者波形基礎(chǔ)的人才能看的懂, 這里我也是講的不太明白,所以需要大家去慢慢學(xué)習(xí)慢慢看了.

代碼第一部分.
初識AVFoundation ->NSSpeechSynthesizer 主要功能就是 控制著文本轉(zhuǎn)語音單元的播放, 綜合解析器吧

為什么我把稱之為綜合語音播放解析器, 看里面的API 應(yīng)該就會有一定的理解了吧.

@interface AVSpeechSynthesizer : NSObject
// 代理 主要是對 語音單元播放狀態(tài)的 監(jiān)聽 
@property(nonatomic, assign, nullable) 
id<AVSpeechSynthesizerDelegate> delegate;

// 這里 我們能得到 播放的狀態(tài).  為什么是只讀屬性呢,  我覺得他是 以 音頻單元 為點進行播放, 中途不可以采用 這些 . 后面的屬性有進行說明
@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;
// 繼續(xù)播放 語音單元
- (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didContinueSpeechUtterance:(AVSpeechUtterance *)utterance;
//取消播放 語音單元
- (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didCancelSpeechUtterance:(AVSpeechUtterance *)utterance;
// 這里 指的是  又來監(jiān)聽 播放 字符范圍
- (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer willSpeakRangeOfSpeechString:(NSRange)characterRange utterance:(AVSpeechUtterance *)utterance;
@end

好了API 基本解讀完畢, 這里看到了, 我想要進行 文本轉(zhuǎn)換語音, 必須要有 綜合語音解析器, 語音單元的添加. 才可以進行這一項工作.
那么什么是語音單元 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è)置 語音解析器 所要用的 語言, en-us  en-gb  zh-cn等等 , 英語, 美式英語, 中文. 當(dāng)然我喜歡中文
@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. */
// 以下的設(shè)置都是 對語音的單項設(shè)置

// 語速
@property(nonatomic) float rate;             // Values are pinned between AVSpeechUtteranceMinimumSpeechRate and AVSpeechUtteranceMaximumSpeechRate.
// 語調(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)過這么一個分析, 就會發(fā)現(xiàn) 文本轉(zhuǎn)語音是如此的簡單

//
//  THSpeechController.m
//  文本到語音的轉(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]){
        // 初始化話 語音綜合器
        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
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容