Make writing a habit together! This is my first day to participate in the “Gold Digging Day New Plan · April More text challenge”, click to see the details of the activity.

AVFoundation is an advanced framework for processing time-based media data in Apple iOS and OS X. It provides a powerful feature set by developing the necessary tools to enable developers to create state-of-the-art media applications based on Apple platforms. Designed for 64-bit processors, AVFoundation takes full advantage of multi-core hardware. It will automatically provide hardware acceleration operation to ensure that most devices can run with the best performance. It is one of the necessary frameworks for iOS development and audio and video development.

Participate in the gold digging Day new plan, continue to record AVFoundation learning, Demo learning address

AVSpeechSynthesizer

  • Text to speech function can be easily realized

For example, “Hello AV Foundation”

Let speechsynthesizer: AVSpeechSynthesizer = AVSpeechSynthesizer() // Synthesizer = AVSpeechUtterance(string: "Hello AV Foundation") // Speechsynthesizer.speak (utterance)Copy the code

AVSpeechUtterance other related Settings

  • If you want to change the sound, set utterance.voice
  • Avspeech Synthesized Voice can be created using either a language encoding or an identifier
utterance.voice = AVSpeechSynthesisVoice(language: "En - GB") / / British English utterance. Voice = AVSpeechSynthesisVoice (identifier: "com. Apple. Ttsbundle. Daniel - compact")Copy the code
  • View the properties for the AVSpeechSynthesisVoice instance; the following properties are read-only
CQLog(utterance. Voice? .identifier) // CQLog(utterance. Voice? .language) // Gender, return enumeration 0- unknown, 1- male, 2 female CQLog(utterance. Voice? .gender) // Name CQLog(utterance.voice? .name) // Voice quality, 1 default quality, 2 enhanced quality CQLog(utterance.voice?.quality)Copy the code
  • View the current language encoding selected by the user
CQLog(AVSpeechSynthesisVoice.currentLanguageCode())
Copy the code
  • View all voice playback sounds
CQLog(AVSpeechSynthesisVoice.speechVoices())
Copy the code
  • Adjust the voice playback rate. The range is 0-1
  • AVSpeechUtteranceMaximumSpeechRate maximum rate
  • AVSpeechUtteranceMinimumSpeechRate minimum rate
  • The default rate AVSpeechUtteranceDefaultSpeechRate
Utterance. Rate = AVSpeechUtteranceMinimumSpeechRate utterance. Rate = 0.4Copy the code
  • Adjust the pitch of speech, ranging from 0.5(low pitch) to 2.0(high pitch)
Utterance. PitchMultiplier = 0.8Copy the code
  • Pausing before the next sentence makes the language seem less unnatural
/ / play with the 0.1 seconds pause before utterance. PostUtteranceDelay = 0.1Copy the code
  • Pause after the last sentence,
Utterance. PreUtteranceDelay = 0.1Copy the code
  • Adjust speech volume range 0-1
utterance.volume = 1
Copy the code
  • View the current discourse content
CQLog(utterance.speechString)
Copy the code

Other uses AVSpeechSynthesizer

  • Write calls buffer in turn, triggering proxy functions that cannot be called simultaneously with speak
Synthesizer. Write (utterance) {audioBuffer in CQLog(audioBuffer)}Copy the code
  • On TV
/ play/pause, trigger suspending agent callback, can continue to play the synthesizer. PauseSpeaking (ats: immediate)Copy the code
  • Continue to play
synthesizer.continueSpeaking()
Copy the code
  • Stop playing
// Immediatel should stop immediately, and the current segment should stop synthesizer.stopspeaking (at:.word)Copy the code

AVSpeechSynthesizer agent

SpeechSynthesizer (_ synthesizer: AVSpeechSynthesizer, didStart utterance: AVSpeechUtterance) {CQLog(speechString + "speechString ")} // Synthesizer AVSpeechSynthesizer, didFinish utterance: AVSpeechUtterance) {CQLog(speechString + "speechString ")} // Pause the speechSynthesizer AVSpeechSynthesizer, didPause utterance: AVSpeechUtterance) {CQLog(speechString + "pause ")} // Synthesizer AVSpeechSynthesizer, didContinue utterance: AVSpeechUtterance) {CQLog(speechString + "speechString ")} // Unsynthesizer AVSpeechSynthesizer, didCancel utterance: AVSpeechUtterance) {CQLog(utterance. SpeechString + "Unwrap ")} // Synthesizer AVSpeechSynthesizer, willSpeakRangeOfSpeechString characterRange: NSRange, utterance: AVSpeechUtterance) {CQLog(speechString + "speechString \(Characterrange.location)")}Copy the code