2022-09-10 19:46:05 +02:00
|
|
|
//
|
|
|
|
// SpeechSynthesis.swift
|
|
|
|
// LiTranslate
|
|
|
|
//
|
|
|
|
// Created by Viktor Kalyniuk on 13.08.2022.
|
|
|
|
//
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
import AVFoundation
|
|
|
|
|
2022-10-24 19:52:38 +02:00
|
|
|
class SpeechSynthesis {
|
|
|
|
private let synthesize = AVSpeechSynthesizer()
|
|
|
|
|
|
|
|
func play(_ string: String, language: Languages) {
|
2022-09-10 19:46:05 +02:00
|
|
|
let utterance = AVSpeechUtterance(string: string)
|
2022-10-24 19:52:38 +02:00
|
|
|
|
|
|
|
do {
|
|
|
|
try AVAudioSession.sharedInstance().setCategory(.playback,mode: .default)
|
|
|
|
} catch let error {
|
|
|
|
print("\(error.localizedDescription)")
|
|
|
|
}
|
|
|
|
|
2022-09-10 19:46:05 +02:00
|
|
|
utterance.voice = AVSpeechSynthesisVoice(language: language.rawValue)
|
2022-10-24 19:52:38 +02:00
|
|
|
synthesize.speak(utterance)
|
2022-09-10 19:46:05 +02:00
|
|
|
}
|
|
|
|
|
2022-10-24 19:52:38 +02:00
|
|
|
func canSynthesis(language: Languages) -> Bool {
|
2022-09-10 19:46:05 +02:00
|
|
|
let speechVoices = AVSpeechSynthesisVoice.speechVoices()
|
|
|
|
for speechVoice in speechVoices {
|
|
|
|
if speechVoice.language.prefix(2) == language.rawValue {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|