今天发现了一个很好玩的功能,语音识别和文字识别,这一篇文章先记录下文字识别.
AV Foundation中提供了一个AVSpeechSynthesizer类来向app中添加朗诵文本的功能.这个类用来播放一个或者多个语音内容,每条语音内容都是一个AVSpeechUtterance对象.(快速记忆:一个语音合成器(SpeechSynthesizer)播放需要一段语音内容(SpeechUtterance))
//首先导入框架
#import <AVFoundation/AVFoundation.h>
//遵守协议
<AVSpeechSynthesizerDelegate>
//创建实例变量
@property (strong, nonatomic) AVSpeechSynthesizer *speech;
@property (strong, nonatomic) UIBarButtonItem *readItem;
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
//停止类型:迅速停止(也可以是念完整个单词后停止)
[self.speech stopSpeakingAtBoundary:AVSpeechBoundaryImmediate];
}
#pragma mark - <AVSpeechSynthesizerDelegate> 监听朗读状态
- (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didStartSpeechUtterance:(AVSpeechUtterance *)utterance {
//已经开始
}
- (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didFinishSpeechUtterance:(AVSpeechUtterance *)utterance {
//已经结束
}
- (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didCancelSpeechUtterance:(AVSpeechUtterance *)utterance {
//已经取消
}
- (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didPauseSpeechUtterance:(AVSpeechUtterance *)utterance{
//已经暂停
}
- (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didContinueSpeechUtterance:(AVSpeechUtterance *)utterance{
//已经继续
}
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"暂停" style:UIBarButtonItemStyleDone target:self action:@selector(readAction:)];;
//初始化一个语音合成器
_speech = [[AVSpeechSynthesizer alloc] init];
//设置代理
_speech.delegate = self;
////创建被播放对象 (有多少个播放语句,就有多少个被播放对象 )
AVSpeechUtterance *utt = [AVSpeechUtterance speechUtteranceWithString:@"静夜思,李白,床前明月光,疑是地上霜,举头望明月,低头思故乡--AVSpeechSynthesizer"];
//设置语言类型
utt.voice = [AVSpeechSynthesisVoice voiceWithLanguage:@"zh-CN"];
//指定播放语音时的速率
//最小值和最大值分别是 AVSpeechUtteranceMinimumSpeechRate和AVSpeechUtteranceMaximumSpeechRate,
utt.rate = 0.4;
//设置声调,属性值介于0.5(低音调)~2.0(高音调)之间
utt.pitchMultiplier = 0.8;
//告诉synthesizer本句朗读结束后要延迟多少秒再接着朗读下一秒
utt.postUtteranceDelay = 0.3;
[self.speech speakUtterance:utt];
/*
Arabic (ar-SA)
Chinese (zh-CN, zh-HK, zh-TW)
Czech (cs-CZ)
Danish (da-DK)
Dutch (nl-BE, nl-NL)
English (en-AU, en-GB, en-IE, en-US, en-ZA)
Finnish (fi-FI)
French (fr-CA, fr-FR)
German (de-DE)
Greek (el-GR)
Hebrew (he-IL)
Hindi (hi-IN)
Hungarian (hu-HU)
Indonesian (id-ID)
Italian (it-IT)
Japanese (ja-JP)
Korean (ko-KR)
Norwegian (no-NO)
Polish (pl-PL)
Portuguese (pt-BR, pt-PT)
Romanian (ro-RO)
Russian (ru-RU)
Slovak (sk-SK)
Spanish (es-ES, es-MX)
Swedish (sv-SE)
Thai (th-TH)
Turkish (tr-TR)
*/
}
-(void)readAction:(UIBarButtonItem *)sender{
if (self.speech.speaking) {
[self.speech pauseSpeakingAtBoundary:AVSpeechBoundaryWord];
sender.title = @"读笑话";
}
if(self.speech.paused){
[self.speech continueSpeaking];
sender.title = @"暂停";
}
}
如果想在后台播放, 需要在background modes配置里设置
AVAudioSession *se = [AVAudioSession sharedInstance];
[se setCategory:AVAudioSessionCategoryPlayback error:nil];
[se setActive:true error:nil];