Merge commit '7621e2f8dec938cf48181c8b10afc9b01f444e68' into beta

This commit is contained in:
Ilya Laktyushin
2025-12-06 02:17:48 +04:00
commit 8344b97e03
28070 changed files with 7995182 additions and 0 deletions
+59
View File
@@ -0,0 +1,59 @@
import Foundation
import AVFoundation
import AccountContext
// Incuding at least one Objective-C class in a swift file ensures that it doesn't get stripped by the linker
private final class LinkHelperClass: NSObject {
}
public class SpeechSynthesizerHolder: NSObject, AVSpeechSynthesizerDelegate {
private var speechSynthesizer: AVSpeechSynthesizer
public var completion: () -> Void = {}
init(speechSynthesizer: AVSpeechSynthesizer) {
self.speechSynthesizer = speechSynthesizer
super.init()
self.speechSynthesizer.delegate = self
}
deinit {
self.stop()
}
public func stop() {
self.speechSynthesizer.stopSpeaking(at: .immediate)
}
public func speechSynthesizer(_ synthesizer: AVSpeechSynthesizer, didFinish utterance: AVSpeechUtterance) {
self.completion()
}
}
public func supportedSpeakLanguages() -> Set<String> {
var languages: [String] = []
for voice in AVSpeechSynthesisVoice.speechVoices() {
let components = voice.language.components(separatedBy: "-")
if let language = components.first {
languages.append(language)
}
}
return Set(languages)
}
public func speakText(context: AccountContext, text: String) -> SpeechSynthesizerHolder? {
guard !text.isEmpty else {
return nil
}
let speechSynthesizer = AVSpeechSynthesizer()
let utterance = AVSpeechUtterance(string: text)
if #available(iOS 11.0, *), let language = NSLinguisticTagger.dominantLanguage(for: text) {
utterance.voice = AVSpeechSynthesisVoice(language: language)
}
speechSynthesizer.speak(utterance)
return SpeechSynthesizerHolder(speechSynthesizer: speechSynthesizer)
}