导入AVFoundation框架,并引用#import <AVfoundation/AVSpeechSynthesis.h>
代码部分如下:
AVSpeechUtterance *utterance = [AVSpeechUtterancespeechUtteranceWithString:@"你想要合成的语音内容"];
AVSpeechSynthesisVoice *voice = [AVSpeechSynthesisVoicevoiceWithLanguage:@"zh-CN"];//发音类型
utterance.voice = voice;
NSLog(@"%@",[AVSpeechSynthesisVoicespeechVoices]);//打印所有的发音类型
utterance.rate *=0.1;//语速
AVSpeechSynthesizer *synth = [[AVSpeechSynthesizeralloc]init];
[synthspeakUtterance:utterance];
//关闭播报语音
BOOL speakStop = [synth stopSpeakingAtBoundary:AVSpeechBoundaryImmediate];
if (!speakStop) {
[synth stopSpeakingAtBoundary:AVSpeechBoundaryWord];
}
//检测是否关闭
int isStop;
if ([synth isSpeaking]==YES) {
isStop = 1;
}else{
isStop = 0;
}