废话不多说直接亮码
自己写了个Model 名字叫SoundReadModel
SoundReadModel.h
#import @interface SoundReadModel : NSObject
- (void)read:(NSString *)str;
- (void)stopRead;
@end
SoundReadModel.m
#import "SoundReadModel.h"
#import
@interface SoundReadModel ()
@property(nonatomic,strong)AVSpeechSynthesizer*aVSpeechSynthesizer;
@end
@implementation SoundReadModel
- (instancetype)init{
if(self= [superinit]){
_aVSpeechSynthesizer.delegate = self;
}
return self;
}
- (void)read:(NSString*)str{
//AVSpeechUtterance: 可以假想成要说的一段话
AVSpeechUtterance* aVSpeechUtterance = [[AVSpeechUtterancealloc]initWithString:str];
aVSpeechUtterance.rate = AVSpeechUtteranceDefaultSpeechRate;
//AVSpeechSynthesisVoice: 可以假想成人的声音
aVSpeechUtterance.voice=[AVSpeechSynthesisVoicevoiceWithLanguage:@"zh-CN"];
//发音
[self.aVSpeechSynthesizer speakUtterance:aVSpeechUtterance];
}
- (void)stopRead{
[self.aVSpeechSynthesizer stopSpeakingAtBoundary:AVSpeechBoundaryImmediate];
}
- (void)speechSynthesizer:(AVSpeechSynthesizer*)synthesizer didFinishSpeechUtterance:(AVSpeechUtterance*)utterance;{
NSLog(@"阅读完毕");
}
//AVSpeechSynthesizer: 语音合成器, 可以假想成一个可以说话的人
- (AVSpeechSynthesizer*)aVSpeechSynthesizer{
if (!_aVSpeechSynthesizer) {
_aVSpeechSynthesizer = [[AVSpeechSynthesizer alloc] init];
}
return _aVSpeechSynthesizer;
}
ViewController中调用
import "ViewController.h"
#import "SoundReadModel.h"
@interface ViewController ()
@property (nonatomic,strong)UITextField *inputField;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
self.edgesForExtendedLayout = UIRectEdgeNone;
[selfinitUI];
}
- (void)initUI{
_inputField = [[UITextField alloc]init];
_inputField.borderStyle = UITextBorderStyleRoundedRect;
_inputField.font = [UIFont systemFontOfSize:13];
_inputField.frame = CGRectMake(30, 100, self.view.frame.size.width-30*2-60, 30);
[self.view addSubview:_inputField];
UIButton*readBtn = [[UIButtonalloc]init];
[readBtnsetTitle:@"报读" forState:UIControlStateNormal];
[readBtnsetTitleColor:[UIColor redColor] forState:UIControlStateNormal];
[readBtnaddTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside];
readBtn.backgroundColor = [UIColor lightGrayColor];
readBtn.frame = CGRectMake(self.view.frame.size.width-30*2-10, 100, 50, 30);
[self.viewaddSubview:readBtn];
//获取iPhone颜色 iPhone壳颜色 这个与当前内容无关
UIDevice*device = [UIDevicecurrentDevice];
SEL selector = NSSelectorFromString(@"deviceInfoForKey:");
if(![devicerespondsToSelector:selector]){
selector =NSSelectorFromString(@"_deviceInfoForKey:");
}
if([devicerespondsToSelector:selector]){
IMPimp = [devicemethodForSelector:selector];
NSString*(*func)(id,SEL,NSString*) = (void*)imp;
NSString*deviceColor = func(device,selector,@"DeviceColor");
NSString*deviceEnclosureColor = func(device,selector,@"DeviceEnclosureColor");
NSLog(@"color = %@ %@",deviceColor,deviceEnclosureColor);
}
}
- (void)btnClick{
SoundReadModel *model = [[SoundReadModel alloc]init];
[modelread:_inputField.text];
}
Demo下载地址:https://download.csdn.net/download/lixianyue1991/10574201