iOS文字转语音(语音朗读)

一直觉得朗读是一个比较酷炫的功能,之前一直不知道怎么做,目前有一些三方的语音朗读SDK,但他们都会收费,因此对于普通个人开发者可能是个不必要的花费,所以我就看了看苹果自家的语音功能,缺陷就是声音可能不是你很满意的。
不扯了,来看看我们需要做点什么吧

1.添加AVFoundation框架
iOS文字转语音(语音朗读)_第1张图片
DAA8CB27-FD6B-4674-AA77-0B41F2D436EF.png
2.导入头文件、设置代理,申明一个AVSpeechSynthesizer全局变量
#import 

@interface ViewController ()
<
AVSpeechSynthesizerDelegate
>
{
AVSpeechSynthesizer *_avSpeaker;
}
3.初始化并开始朗读
//初始化语音合成器
_avSpeaker = [[AVSpeechSynthesizer alloc] init];
_avSpeaker.delegate = self;
//初始化要说出的内容
AVSpeechUtterance *utterance = [[AVSpeechUtterance alloc] initWithString:[@"爱我iOS"];
//设置语速,语速介于AVSpeechUtteranceMaximumSpeechRate和AVSpeechUtteranceMinimumSpeechRate之间
//AVSpeechUtteranceMaximumSpeechRate
//AVSpeechUtteranceMinimumSpeechRate
//AVSpeechUtteranceDefaultSpeechRate
utterance.rate = 0.5;
        
//设置音高,[0.5 - 2] 默认 = 1
//AVSpeechUtteranceMaximumSpeechRate
//AVSpeechUtteranceMinimumSpeechRate
//AVSpeechUtteranceDefaultSpeechRate
utterance.pitchMultiplier = 1;
        
//设置音量,[0-1] 默认 = 1
utterance.volume = 1;

//读一段前的停顿时间
utterance.preUtteranceDelay = 1;
//读完一段后的停顿时间
utterance.postUtteranceDelay = 1;
        
//设置声音,是AVSpeechSynthesisVoice对象
//AVSpeechSynthesisVoice定义了一系列的声音, 主要是不同的语言和地区.
//voiceWithLanguage: 根据制定的语言, 获得一个声音.
//speechVoices: 获得当前设备支持的声音
//currentLanguageCode: 获得当前声音的语言字符串, 比如”ZH-cn”
//language: 获得当前的语言
//通过特定的语言获得声音
AVSpeechSynthesisVoice *voice = [AVSpeechSynthesisVoice voiceWithLanguage:@"zh-CN"];
//通过voicce标示获得声音
//AVSpeechSynthesisVoice *voice = [AVSpeechSynthesisVoice voiceWithIdentifier:AVSpeechSynthesisVoiceIdentifierAlex];
utterance.voice = voice;
//开始朗读
[_avSpeaker speakUtterance:utterance];
4.暂停朗读
//暂停朗读
//AVSpeechBoundaryImmediate 立即停止
//AVSpeechBoundaryWord    当前词结束后停止
[_avSpeaker pauseSpeakingAtBoundary:AVSpeechBoundaryImmediate];
5.继续朗读
[_avSpeaker continueSpeaking];
6.停止朗读
//AVSpeechBoundaryImmediate 立即停止
//AVSpeechBoundaryWord    当前词结束后停止
[_avSpeaker stopSpeakingAtBoundary:AVSpeechBoundaryImmediate];
7.代理方法
#pragma mark -
#pragma mark - AVSpeechSynthesizerDelegate
//已经开始
- (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didStartSpeechUtterance:(AVSpeechUtterance *)utterance{


}
//已经说完
- (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didFinishSpeechUtterance:(AVSpeechUtterance *)utterance{

//如果朗读要循环朗读,可以在这里再次调用朗读方法
//[_avSpeaker speakUtterance:utterance];
}
//已经暂停
- (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didPauseSpeechUtterance:(AVSpeechUtterance *)utterance{

}
//已经继续说话
- (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didContinueSpeechUtterance:(AVSpeechUtterance *)utterance{

}
//已经取消说话
- (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didCancelSpeechUtterance:(AVSpeechUtterance *)utterance{

}
//将要说某段话
- (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer willSpeakRangeOfSpeechString:(NSRange)characterRange utterance:(AVSpeechUtterance *)utterance{

}
8.实现后台播放

(1)在配置里设置

iOS文字转语音(语音朗读)_第2张图片
98BB280C-A7EC-4B28-86BA-6CC417AC9F17.png

(2)在AppDelegate.m里添加代码

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

AVAudioSession *session = [AVAudioSession sharedInstance];
NSError *error = nil;
[session setCategory:AVAudioSessionCategoryPlayback error:&error];
[session setActive:YES error:&error];
[Bmob registerWithAppKey:BMOB_AppID];
return YES;
}

你可能感兴趣的:(iOS文字转语音(语音朗读))