At the moment, I am engaged on a undertaking to implement text-to-speech performance when the app receives a push notification. The notification is saved regionally and proven utilizing an area notification with UNMutableNotificationContent() for the content material. It has to make use of the ringer audio and should run within the foreground, background, and even when the app is killed, much like the .sound property in UNMutableNotificationCenter().
I’m particularly not utilizing the playback class in AVAudioSession as a result of it makes use of media audio as an alternative of ringer audio. Once I use the ambient class in AVAudioSession, it can’t run within the background or when the app is killed. I anticipate it to run within the foreground, background, and even when the app is killed upon receiving a push notification.
This code snippet is the logic I utilized in AppDelegate didReceiveRemoteNotification.
if let tts = userInfo["text"] as? String {
do {
attempt audioSession.setActive(true)
attempt audioSession.setCategory(AVAudioSession.Class.ambient,
choices: [.duckOthers])
} catch {
print("Unable to set audio session class: %@", error)
}
let utterance = AVSpeechUtterance(string: userInfo["text"] as! String)
utterance.voice = AVSpeechSynthesisVoice(language: "id-ID")
synthesizer.communicate(utterance)
}