AV Foundation

主要框架

  • CoreAudio

    • 音频处理框架
    • 扩展学习:《Learning CoreAudio》
  • CoreVideo
    • 视频处理的管道模式,逐帧访问
  • CoreMedia
    • 提供音频和视频处理的低级数据类型和接口,如CMTime
  • CoreAnimation
    • 动画框架

AV Foundation解析

  • 音频播放和记录

    • AVAudioPlayer
    • AVAudioRecorder
  • 媒体文件检查
    • 媒体文件的信息,媒体长度,创建时间等
    • 艺术家
  • 视频播放
    • AVPlayer
    • AVPlayerItem
  • 媒体捕捉
    • 摄像头 AVCaputureSession
  • 媒体编辑
    • 音视频整合,编辑,修改,场景动画,如IMovie
  • 媒体处理
    • AVAssetReader
    • AVAssetWriter

文字转语音

  • 文字转语音主要是用到AVSpeechSynthesizer

  • 里面封装了一些语音的常见操作,包括常见的播放、暂停、停止等

  • 使用AV Foundation需要包含头文件 #import <AVFoundation/AVFoundation.h>

  • 声明一个AVSpeechSynthesizer实例

#import "ViewController.h"
#import <AVFoundation/AVFoundation.h> @interface ViewController ()
/**语音播放对象*/
@property (nonatomic, strong) AVSpeechSynthesizer *synthesizer;
/**语音支持类型数组*/
@property (nonatomic, copy) NSArray *voices;
/// 播放的文字数组
@property (nonatomic, copy) NSArray *speechStrings;
/// 当前播放
@property (nonatomic, assign) NSInteger *currentIndex;
@end
  • 初始化实例对象

//1 创建AVSpeechSynthesizer对象 _synthesizer = [[AVSpeechSynthesizer alloc] init];

  • 设置支持的语模式,可以通过方法speechVoices获得系统支持的语音
// 查看支持的语音体系
NSLog(@"%@",[AVSpeechSynthesisVoice speechVoices]); // 这里只用中文演示
_voices =
@[
[AVSpeechSynthesisVoice voiceWithLanguage:@"zh-CN"]
];
  • 然后从本地读取一个文件,以换行符分隔
// 从本地读取文件
- (void)read {
_speechStrings = [[NSString stringWithContentsOfFile:[[NSBundle mainBundle]pathForResource:@"test" ofType:@"txt"] encoding:NSUTF8StringEncoding error:nil] componentsSeparatedByString:@"\n"];
}
  • 最后点击屏幕即可播放
// 开始播放
- (void)beginConversation {
for (NSInteger i = 0; i < self.speechStrings.count; i ++) {
AVSpeechUtterance *utterance = [[AVSpeechUtterance alloc] initWithString:self.speechStrings[i]];
// 播放语音类型
utterance.voice = self.voices[0];
// 播放速率
utterance.rate = 0.4f;
// 音调改变
utterance.pitchMultiplier= 0.8f;
// 播放下一条暂停一下
utterance.postUtteranceDelay = 0.1f;
[_synthesizer speakUtterance:utterance];
}
}

播放控制

- (IBAction)play:(id)sender {
if (_currentIndex == 0) {
[self beginConversation:_currentIndex];
}else {
[_synthesizer continueSpeaking];
}
} - (IBAction)stop:(id)sender {
_currentIndex = 0; [_synthesizer stopSpeakingAtBoundary:AVSpeechBoundaryImmediate];
} - (IBAction)pause:(id)sender { [_synthesizer pauseSpeakingAtBoundary:AVSpeechBoundaryImmediate];
} - (IBAction)previous:(id)sender {
_currentIndex -= 2;
if (_currentIndex <= 0) {
_currentIndex = 0;
}else if(_currentIndex >= _speechStrings.count) {
_currentIndex = _speechStrings.count;
}
[_synthesizer stopSpeakingAtBoundary:AVSpeechBoundaryImmediate];
[self beginConversation:_currentIndex];
} - (IBAction)next:(id)sender {
if (_currentIndex <= 0) {
_currentIndex = 0;
}else if(_currentIndex >= _speechStrings.count) {
_currentIndex = _speechStrings.count;
} [_synthesizer stopSpeakingAtBoundary:AVSpeechBoundaryImmediate];
[self beginConversation:_currentIndex];
} // 代理方法
- (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didStartSpeechUtterance:(AVSpeechUtterance *)utterance {
_currentIndex ++; if (_currentIndex <= 0) {
_currentIndex = 0;
}else if(_currentIndex >= _speechStrings.count) {
_currentIndex = _speechStrings.count;
}
self.currentLabel.text = [NSString stringWithFormat:@"%zd",_currentIndex];
}
- (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didFinishSpeechUtterance:(AVSpeechUtterance *)utterance { }

AV Foundation 实现文字转语音的更多相关文章

  1. IOS开发之学习《AV Foundation 开发秘籍》

    敲了这么久的代码,查阅了很多资料,都是网络电子版的,而且时间久了眼睛也累了,还不如看一下纸质的书籍,让眼睛休息休息. 本篇开始学习<AV Foundation 开发秘籍>,并记录对自己本人 ...

  2. C# 语音识别(文字to语音、语音to文字)

    最近打算研究一下语音识别,但是发现网上很少有C#的完整代码,就把自己的学习心得放上来,和大家分享一下. 下载API: 1)SpeechSDK51.exe                   (67.0 ...

  3. Android文字转语音

    虽然视觉上的反馈通常是给用户提供信息最快的方式,但这要求用户把注意力设备上.当用户不能查看设备时,则需要一些其他通信的方法.Android提供了强大的文字转语音Text-to-Speech,TTS A ...

  4. Android实例-调用GOOGLE的TTS实现文字转语音(XE7+小米2)(无图)

    注意:在手机上必须选安装文字转语音引擎“google Text To Speech”地址:http://www.shouji56.com/soft/GoogleWenZiZhuanYuYinYinQi ...

  5. iOS语音识别,语音播报,文字变语音播报,语音变文字

    首先使用的是科大讯飞的sdk 1.语音识别部分 AppDelegate.m #import "AppDelegate.h" #import <iflyMSC/iflyMSC. ...

  6. 简单C#文字转语音

    跟着微软走妥妥的,C#文字转语音有很多参数我就不说了,毕竟我也是初学者.跟大家分享最简单的方法,要好的效果得自己琢磨喽: 先添加引用System.Speech程序集: using System; us ...

  7. Android技术分享-文字转语音并朗读

    Android技术分享-文字转语音并朗读 最近在做一个项目,其中有一个功能是需要将文本转换成语音并播放出来.下面我将我的做法分享一下. 非常令人开心的是,Android系统目前已经集成了TTS,提供了 ...

  8. TTS 文字转语音 ekho

    1.源码下载 使用svn客户端,执行如下命令下载 svn co https://svn.code.sf.net/p/e-guidedog/code/ 2.官方网站查看说明 http://www.egu ...

  9. Android文字转语音引擎(TTS)使用

    百度网盘下载地址 密码:3si0资源来源:https://blog.csdn.net/Sqq_yj/article/details/82460580?utm_source=blogxgwz4 简单比较 ...

随机推荐

  1. Codeforces Round #521 (Div. 3) D. Cutting Out 【二分+排序】

    任意门:http://codeforces.com/contest/1077/problem/D D. Cutting Out time limit per test 3 seconds memory ...

  2. Hinge Loss

    http://blog.csdn.net/luo123n/article/details/48878759 https://en.wikipedia.org/wiki/Hinge_loss       ...

  3. java线程池系列(1)-ThreadPoolExecutor实现原理

    前言 做java开发的,一般都避免不了要面对java线程池技术,像tomcat之类的容器天然就支持多线程. 即使是做偏后端技术,如处理一些消息,执行一些计算任务,也经常需要用到线程池技术. 鉴于线程池 ...

  4. PL/SQL知识点

    1.ROW_NUMBER() OVER(PARTITION BY XXX ORDER BY XXX) SELECT STP.FLOW_INST_ID, BUU.USER_NAME, ORGG.NAME ...

  5. Spring8中lambda表达式的学习(Function接口、BiFunction接口、Consumer接口)

    代码重构,为了确保功能的等效性,梳理代码时,发现如下代码: public SingleRespTTO fundI(SingleReqTTO request) throws Exception { re ...

  6. Unity让带有Rigidbody组件的游戏对象停止运动

    Rigidbody rigidbody = transform.GetComponent<Rigidbody>(); rigidbody.velocity = Vector3.zero; ...

  7. mybatis自动生成@Table、@Column、@Id注解

    在pom.xml中添加如下插件以及插件相关的依赖 <build> <plugins> <plugin> <groupId>org.springframe ...

  8. 为何企业钟爱H5响应式网站? html5响应式网站的优势与特点

    随着移动互联网时代的到来,H5响应式网站应运而生,并成功获得了商家.访客.搜索引擎等的青睐!越来越多的企业也选择了H5响应式建站,可为何企业钟爱H5响应式网站呢?难道传统网站不好吗?这个不能妄下结论, ...

  9. 九、IIC驱动原理分析

    学习目标:学习IIC驱动原理: 一.IIC总线协议 IIC串行总线包括一条数据线(SDA)和一条时钟线(SCL),支持“一主多从”和“多主机”模式:每个从机设备都有唯一的地址来识别. 图 1 IIC ...

  10. unity独立游戏开发日记2018/09/27

    今天优化了下昨天的代码,并且添加了树木和其他资源的生成.还修复了接近石头后,挖掘图标不出现的bug.目前可以在unity中稳定60-70fps. 详看文章:https://www.cnblogs.co ...