一、简单说明
(1)
在iOS7之前,想要实现语音播报文字内容,可能需要第三方资源库来实现。现在在iOS7上,系统为我们提供了语音播报文字的功能,我们不仅可以播报英语内容,也可以播报汉语文字
实现TTS主要依赖AVSpeechSynthesizer,AVSpeechUtterance,AVSpeechSynthesisVoice,要使用这些类必须先加入
AVFoundation框架:
AVSpeechSynthesisVoice:用来配置发音,支持的发音非常多.个人感觉台湾发音最好听~通过调用
[AVSpeechSynthesisVoicespeechVoices]类方法可用看到支持的发音种类;
AVSpeechUtterance:这个类就是用来将字符串合成为语音对象提供给AVSpeechSynthesizer来播放,这个类还有一些
实例方法用来控制语速,音调等等。。
实现代码:
TXSoundPlayer.h文件代码:
1 // Created by 鑫 on 14/12/23. 2 // Copyright (c) 2014年 梁镋鑫. All rights reserved. 3 // 4 5 #import <Foundation/Foundation.h> 6 #import <AVFoundation/AVFoundation.h> 7 8 @interface TXSoundPlayer : NSObject 9 { 10 NSMutableDictionary* soundSet; //声音设置 11 NSString* path; //配置文件路径 12 } 13 14 @property(nonatomic,assign)float rate; //语速 15 @property(nonatomic,assign)float volume; //音量 16 @property(nonatomic,assign)float pitchMultiplier; //音调 17 @property(nonatomic,assign)BOOL autoPlay; //自动播放 18 19 20 +(TXSoundPlayer*)soundPlayerInstance; 21 22 -(void)play:(NSString*)text; 23 24 -(void)setDefault; 25 26 -(void)writeSoundSet; 27 28 @end
TXSoundPlayer.m文件代码:
1 // 2 // Created by 鑫 on 14/12/23. 3 // Copyright (c) 2014年 梁镋鑫. All rights reserved. 4 // 5 6 #import "TXSoundPlayer.h" 7 8 static TXSoundPlayer* soundplayer=nil; 9 10 @implementation TXSoundPlayer 11 12 +(TXSoundPlayer*)soundPlayerInstance 13 { 14 if(soundplayer==nil) 15 { 16 soundplayer=[[TXSoundPlayer alloc]init]; 17 [soundplayer initSoundSet]; 18 } 19 return soundplayer; 20 } 21 22 //播放声音 23 -(void)play:(NSString*)text 24 { 25 if(![text isEqualToString:NULL]) 26 { 27 AVSpeechSynthesizer* player=[[AVSpeechSynthesizer alloc]init]; 28 AVSpeechUtterance* u=[[AVSpeechUtterance alloc]initWithString:text];//设置要朗读的字符串 29 u.voice=[AVSpeechSynthesisVoice voiceWithLanguage:@"zh-CN"];//设置语言 30 u.volume=self.volume; //设置音量(0.0~1.0)默认为1.0 31 u.rate=self.rate; //设置语速 32 u.pitchMultiplier=self.pitchMultiplier; //设置语调 33 [player speakUtterance:u]; 34 } 35 } 36 37 //初始化配置 38 -(void)initSoundSet 39 { 40 path=[[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"SoundSet.plist"]; 41 soundSet=[NSMutableDictionary dictionaryWithContentsOfFile:path]; 42 if(soundSet==nil) 43 { 44 soundSet=[NSMutableDictionary dictionary]; 45 [soundplayer setDefault]; 46 [soundplayer writeSoundSet]; 47 } 48 else 49 { 50 self.autoPlay=[[soundSet valueForKeyPath:@"autoPlay"] boolValue]; 51 self.volume=[[soundSet valueForKeyPath:@"volume"] floatValue]; 52 self.rate=[[soundSet valueForKeyPath:@"rate"] floatValue]; 53 self.pitchMultiplier=[[soundSet valueForKeyPath:@"pitchMultiplier"] floatValue]; 54 } 55 } 56 57 //恢复默认设置 58 -(void)setDefault 59 { 60 self.volume=0.7; 61 self.rate=0.166; 62 self.pitchMultiplier=1.0; 63 } 64 65 //将设置写入配置文件 66 -(void)writeSoundSet 67 { 68 [soundSet setValue:[NSNumber numberWithBool:self.autoPlay] forKey:@"autoPlay"]; 69 [soundSet setValue:[NSNumber numberWithFloat:self.volume] forKey:@"volume"]; 70 [soundSet setValue:[NSNumber numberWithFloat:self.rate] forKey:@"rate"]; 71 [soundSet setValue:[NSNumber numberWithFloat:self.pitchMultiplier] forKey:@"pitchMultiplier"]; 72 [soundSet writeToFile:path atomically:YES]; 73 } 74 75 @end
(2)
上面的代码节选自我项目中的部分代码,功能已经封装好工具类了,所以想要实现该功能,例如warnmsg = @"今天天气真好,工作加油";来测试一下。
NSString * dayweather =@"据警方了解经济,复苏减肥了就散了就是垃圾手机发了睡觉了时间就是犯贱手机发了就是就是,哦就哦时间就是就是凤凰山风景时间飞逝减肥接送"; TXSoundPlayer* sound=[TXSoundPlayer soundPlayerInstance]; [sound play:dayweather];
注意:在iOS7上运行能正常播放声音,而在iOS8上由于模拟器问题不能整成播放,在真机上才能正常播报,坑爹的模拟器,害得我看了那没文档。
来源:https://www.cnblogs.com/asd5551680/p/4180556.html