Today bloggers have a text-to-speech (TTS) needs, encountered some difficulties, here and we share, hope to progress together.
Text-to-speech technology, also called TTS, is the abbreviation for text to speech. iOS uses this technology when it wants to do features like audio books.
This feature is only iOS7 after you import the Avfoundation library
When the iphone mute mode is turned on, the sound will not play and you need to turn on background playback
Avaudiosession *audiosession = [Avaudiosession sharedinstance];
[Audiosession Setcategory:avaudiosessioncategoryplayback Error:nil];
Speech synthesizer, is the most important interface
Avspeechsynthesizer *av = [[Avspeechsynthesizer alloc]init];
To say a word, the speech synthesizer's playback content interface
Avspeechutterance *utterance1 = [[Avspeechutterance alloc]initwithstring:[nsstring stringWithFormat:@ "%@", @ "real time"]]; Text that needs to be converted
Pause time after reading a paragraph
utterance1.postutterancedelay=0.1;
Rate of speech
utterance1.rate=0.4;
Start playing content
[AV Speakutterance:utterance1];
Avspeechsynthesisvoice defines a range of sounds, mainly in different languages and regions. Here are a few of his properties and methods
Voicewithlanguage: Get a voice based on the language you've developed.
Speechvoices: Get the sound supported by the current device
Currentlanguagecode: Gets the language string of the current sound, such as "ZH-CN"
Language: Getting the current language
The above is a simple TTS playback, if you want to do more complex operations, you can follow the Avspeechsynthesizerdelegate protocol, implement its methods
The delegate for the synthesizer, for some events, provides the interface for the response.
Didcancelspeechutterance: I've canceled my speech.
Didcontinuespeechutterance: has continued to speak
Didfinishspeechutterance: Already finished.
Didpausespeechutterance: Already paused
Didstartspeechutterance: Already started
Willspeakrangeofspeechstring: Going to say a word
iOS Development journal 50-text-to-speech (TTS)