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

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)
@@ -66,22 +68,14 @@ import Capacitor
66
68
 
67
69
  // Adjust rate for a closer match to other platform.
68
70
  @objc private func adjustRate(_ rate: Float) -> Float {
69
- let baseRate = AVSpeechUtteranceDefaultSpeechRate
70
- if rate == 1 {
71
- return baseRate
72
- }
73
- if rate > baseRate {
74
- return baseRate + (rate * 0.025)
75
- }
76
- return rate / 2
71
+ let baseRate: Float = AVSpeechUtteranceDefaultSpeechRate
72
+ if (rate >= 1.0 ) {
73
+ return (0.1 * rate) + (baseRate - 0.1)
74
+ }
75
+ return rate * baseRate
77
76
  }
78
77
 
79
78
  @objc private func resolveCurrentCall() {
80
- do {
81
- try AVAudioSession.sharedInstance().setActive(false)
82
- } catch {
83
- CAPLog.print(error.localizedDescription)
84
- }
85
79
  guard let call = calls.first else {
86
80
  return
87
81
  }
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": "5.0.0",
4
4
  "description": "Capacitor plugin for synthesizing speech from text.",
5
5
  "main": "dist/plugin.cjs.js",
6
6
  "module": "dist/esm/index.js",