In IOS7, Apple has introduced TTS technology that enables speech synthesis without the need for a network environment.
The main APIs for iOS7 speech synthesis are as follows:
1, Avspeechutterance, is the basic unit of speech synthesis, which encapsulates some parameters that affect the need for speech synthesis: voice, intonation, speed, and delay.
2, Avspeechsynthesisvoice, is the voice object in speech synthesis, it mainly includes voice and region two aspects.
3, Avspeechsynthesizer, voice synthesizer management class, through the Speakutterance: Method Management Avspeechsynthesizer.
4, Avspeechsynthesizerdelegate, is the avspeechsynthesizer of the Commission agreement.
The code is as follows:
#import "DemoVC36.h"
#import
@interface DemoVC36 () @property (weak, nonatomic) Iboutlet Uitextview *textview;
@property (Weak, nonatomic) Iboutlet UISlider *slider;
@property (nonatomic, strong) Avspeechsynthesizer *speechsynthesizer;
@end
@implementation DemoVC36
-(void) Viewdidload {
[Super Viewdidload];
To TextView
[Self.textView.layer setborderwidth:0.5f];
[Self.textView.layer Setbordercolor:[uicolor Graycolor]. Cgcolor];
[Self.textview setdelegate:self];
Self.speechsynthesizer = [[Avspeechsynthesizer alloc] init];
Self.speechSynthesizer.delegate = self;
}
-(BOOL) TextView: (Uitextview *) TextView Shouldchangetextinrange: (nsrange) Range Replacementtext: (NSString *) text{
BOOL retval = TRUE;
if ([text isequaltostring:@ "\ n"]) {
[Self.textview Resignfirstresponder];
retval = FALSE;
}
return retval;
}
-(Ibaction) speakbuttonwaspressed: (UIButton *) Sender {
NSString *str = @ "Wei Fang is a handsome man";
Avspeechutterance *utt = [Avspeechutterance speechutterancewithstring:str];
Utt.rate = [Self.slider value];
[Self.speechsynthesizer Speakutterance:utt];
}
-(Ibaction) Speechspeedshouldchange: (ID) Sender {
UISlider *slider = (UISlider *) sender;
Nsinteger val = round (slider.value);
NSLog (@ "%@", [NSString stringwithformat:@ "%ld", Val]);
}
#pragma mark--avspeechsynthesizerdelegate
-(void) SpeechSynthesizer: (Avspeechsynthesizer *) synthesizer didstartspeechutterance: (Avspeechutterance *) utterance{
NSLog (@ "speech synthesis start");
}
-(void) SpeechSynthesizer: (Avspeechsynthesizer *) synthesizer didfinishspeechutterance: (Avspeechutterance *) utterance{
NSLog (@ "speech synthesis complete");
}
@end
iOS speech synthesis