Skip to content

Instantly share code, notes, and snippets.

View myrickchow32's full-sized avatar

Myrick Chow myrickchow32

  • RedSo
  • Hong Kong
View GitHub Profile
func transitionAnimation(view: UIView, animationOptions: UIView.AnimationOptions, isReset: Bool) {
// Combine transition annimation option with other animation options
let combinedAnimationOptions: UIView.AnimationOptions = [animationOptions, .allowUserInteraction, .autoreverse, .repeat]
UIView.transition(with: view, duration: durationOfAnimationInSecond, options: combinedAnimationOptions, animations: {
view.backgroundColor = UIColor.init(named: isReset ? "darkGreen" : "darkRed")
}, completion: nil)
}
import Foundation
import AVFoundation
class VoiceUtils {
static var avSpeechSynthesizer = AVSpeechSynthesizer()
static func textToSpeech(text: String, delegate: AVSpeechSynthesizerDelegate?) {
/*
Support langauges:
import AVFoundation
let avSpeechSynthesizer = AVSpeechSynthesizer()
let utterance = AVSpeechUtterance(string: "Hello world")
let speechVoices = AVSpeechSynthesisVoice.speechVoices()
speechVoices.forEach { (voice) in
print("**********************************")
print("Voice identifier: \(voice.identifier)")
print("Voice language: \(voice.language)")
print("Voice name: \(voice.name)")
print("Voice quality: \(voice.quality.rawValue)") // Compact: 1 ; Enhanced: 2
}
/*
let utterance = AVSpeechUtterance(string: "Hello world")
// Method 1: Directly by identifier
utterance.voice = AVSpeechSynthesisVoice(identifier: "com.apple.ttsbundle.Karen-compact")
// Method 2: By langauge code
utterance.voice = AVSpeechSynthesisVoice(language: "en-AU")
let utterance = AVSpeechUtterance(string: "Hello world")
utterance.volume = 0.5 // Range: 0.0 - 1.0 ; A relative volume to the device volume
/*
Range: 0.0 - 1.0
AVSpeechUtteranceMinimumSpeechRate: 0.0
AVSpeechUtteranceMaximumSpeechRate: 1.0
AVSpeechUtteranceDefaultSpeechRate: 0.5
*/
let utterance = AVSpeechUtterance(string: "Hello world")
utterance.rate = 0.5
/*
Pitch multiplier range: 0.5 to 2.0
Default value: 1.0
*/
let utterance = AVSpeechUtterance(string: "Hello world")
utterance.pitchMultiplier = 1.0
import AVFoundation
let avSpeechSynthesizer = AVSpeechSynthesizer()
avSpeechSynthesizer.delegate = self
// Session 1 --- AVSpeechUtterance: 7 Parameters of synthesized speech
let utterance = AVSpeechUtterance(string: "Hello world")
utterance.voice = ...
utterance.rate = ...
utterance.pitchMultiplier = ...