iOS Developer

入门 -[AV Foundation]

2017-02-22  本文已影响0人  玄裳

AV Foundation 的含义


用于处理基于时间的数据媒体数据的高级 Objective-C 框架。充分利用了多核硬件的优势并大量使用 blockGCD 机制将复杂的计算机进程党在后台线程进行

AV Foundation 的适用范围


``iOS`` 和 ``Mac OS X ``媒体环境.png

解析AV Foundation


AV Foundation 的核心功能:

了解数字媒体


数字媒体压缩


AV FoundationCore Audio 提供对 MP3 数据解码的支持,但是不支持对其进行编码。

容器格式


容器格式被认为元文件格式。包含一种或更多种媒体类型,比如:.mov(视频).m4v(视频).mpg(动态图像专家组,可以是视频也可以是音频).m4a(音频)等。

初识AV Foundation


iOS开发中,运行下面的代码可以听见默认设置的声音读出“hello lily”

AVSpeechSynthesizer *synthesizer = [[AVSpeechSynthesizer alloc] init];?//语音合成器
AVSpeechUtterance *utterance = [[AVSpeechUtterance alloc] initWithString:@"hello lily"];//将字符串合成为语音对象

[synthesizer speakUtterance:utterance];//生成音频

实现两个人的文本到语音的对话功能
Speaker.h文件demo:

#import <Foundation/Foundation.h>
#import <AVFoundation/AVFoundation.h>

@interface Speaker : NSObject

+ (instancetype)speaker;
- (void)begainConversation;

@end

Speaker.m文件demo:


#import "Speaker.h"

@interface Speaker ()

@property (nonatomic, strong) AVSpeechSynthesizer *synthesizer;
@property (nonatomic, strong) NSArray *voicesArray;
@property (nonatomic, strong) NSArray *speechStringArr;
@end

@implementation Speaker

+ (instancetype)speaker
{
    return [[self alloc] init];
}

- (id)init
{
    self = [super init];
    if (self) {

        _synthesizer = [[AVSpeechSynthesizer alloc] init];
      
        //设置语言类别
        _voicesArray= @[[AVSpeechSynthesisVoice voiceWithLanguage:@"en-GB"],[AVSpeechSynthesisVoice voiceWithLanguage:@"en-US"]];
        
        _speechStringArr = [self buildConversation];
        
    }
    
    return self;
}

- (NSArray *)buildConversation
{
    return @[@"hello i am Sam  what's your name?",
             @"i am lily",
             @"how old are u?",
             @"18 years old this year",
             @"Really so young!",
             @"Thank you, but I don't think so",
             @"why?",
             @"Because I haven't a boyfriend",
             ];
}

- (void)begainConversation
{
    for (NSUInteger i = 0; i<self.speechStringArr.count; i++) {
        AVSpeechUtterance *utterance = [[AVSpeechUtterance alloc] initWithString:self.speechStringArr[i]];
        utterance.voice = self.voicesArray[i%2];
        utterance.rate = 0.5f; //播放语音内容的速率([0.0-1.0]值区间)
        utterance.pitchMultiplier = 0.9f;[0.5低音调-2.0高音调]//播放特定语句时改变声音的音调
        utterance.postUtteranceDelay = 0.1f;//语音合成器在播放下一语句之间的暂停时间
        [self.synthesizer speakUtterance:utterance];
    }
}

ViewController中调用:

 Speaker *speaker = [[Speaker alloc] init];
 [speaker begainConversation];
上一篇 下一篇

猜你喜欢

热点阅读