iOS 在自帶語音播報功能(但是只能是普通話),代碼如下:
#import "SpeechTool.h"#import@interface SpeechTool ()@end
@implementation SpeechTool
+ (void)creatSpeechWithSpeechText:(NSString*)string{
//1.初始化一個語音合成器肛走,控制播放、暫停
AVSpeechSynthesizer *av = [[AVSpeechSynthesizer alloc]init];
//2.設(shè)置語音合成器的代理
//av.delegate = self;
//3. 實例化發(fā)聲對象 AVSpeechUtterance录别,實例化發(fā)聲的對象羹与,指定要朗讀的內(nèi)容故硅,可以控制說話的語速等
AVSpeechUtterance*utterance = [[AVSpeechUtterance alloc]initWithString:string];
utterance.rate=0.5;
//設(shè)置發(fā)音,這是中文普通話 zh_CN 中文 en-US 英文
AVSpeechSynthesisVoice*voice = [AVSpeechSynthesisVoice voiceWithLanguage:@"zh-CN"];utterance.voice= voice;
[av speakUtterance:utterance];//開始
}
在需要調(diào)用的地方初始化SpeechTool類纵搁,調(diào)用+ (void)creatSpeechWithSpeechText:(NSString*)string 這個方法就行了