AV Foundation 实现文字转语音
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 {
}
本文来自博客园,作者:struggle_time,转载请注明原文链接:https://www.cnblogs.com/songliquan/p/5424583.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
2015-04-23 IOS开发学习笔记017-第一个IOS应用