@capacitor-community/text-to-speech 4.1.0 → 4.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -4,10 +4,21 @@ import Capacitor
4
4
  @objc public class TextToSpeech: NSObject, AVSpeechSynthesizerDelegate {
5
5
  let synthesizer = AVSpeechSynthesizer()
6
6
  var calls: [CAPPluginCall] = []
7
+ let queue = DispatchQueue(label: "backgroundAudioSetup", qos: .userInitiated, attributes: [], autoreleaseFrequency: .inherit, target: nil)
7
8
 
8
9
  override init() {
9
10
  super.init()
10
11
  self.synthesizer.delegate = self
12
+ // set session in background to avoid UI hangs.
13
+ queue.async {
14
+ do {
15
+ let avAudioSessionCategory: AVAudioSession.Category = .playback
16
+ try AVAudioSession.sharedInstance().setCategory(avAudioSessionCategory, mode: .default, options: .duckOthers)
17
+ try AVAudioSession.sharedInstance().setActive(true)
18
+ } catch {
19
+ print("Error setting up AVAudioSession: \(error)")
20
+ }
21
+ }
11
22
  }
12
23
 
13
24
  public func speechSynthesizer(_ synthesizer: AVSpeechSynthesizer, didCancel utterance: AVSpeechUtterance) {
@@ -20,15 +31,6 @@ import Capacitor
20
31
 
21
32
  @objc public func speak(_ text: String, _ lang: String, _ rate: Float, _ pitch: Float, _ category: String, _ volume: Float, _ voice: Int, _ call: CAPPluginCall) throws {
22
33
  self.synthesizer.stopSpeaking(at: .immediate)
23
-
24
- var avAudioSessionCategory = AVAudioSession.Category.ambient
25
- if category != "ambient" {
26
- avAudioSessionCategory = AVAudioSession.Category.playback
27
- }
28
-
29
- try AVAudioSession.sharedInstance().setCategory(avAudioSessionCategory, mode: .default, options: AVAudioSession.CategoryOptions.duckOthers)
30
- try AVAudioSession.sharedInstance().setActive(true)
31
-
32
34
  self.calls.append(call)
33
35
 
34
36
  let utterance = AVSpeechUtterance(string: text)
@@ -77,11 +79,6 @@ import Capacitor
77
79
  }
78
80
 
79
81
  @objc private func resolveCurrentCall() {
80
- do {
81
- try AVAudioSession.sharedInstance().setActive(false)
82
- } catch {
83
- CAPLog.print(error.localizedDescription)
84
- }
85
82
  guard let call = calls.first else {
86
83
  return
87
84
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@capacitor-community/text-to-speech",
3
- "version": "4.1.0",
3
+ "version": "4.1.1",
4
4
  "description": "Capacitor plugin for synthesizing speech from text.",
5
5
  "main": "dist/plugin.cjs.js",
6
6
  "module": "dist/esm/index.js",