capacitor-community/text-to-speech

feat: iOS improve performance and compatibility with other capacitor plugins

jh-cm opened this issue · 0 comments

Is your feature request related to a problem? Please describe:

On iOS upon completion of TextToSpeech.speak() call, TextToSpeech plugin currently deactivates shared instance of AVAudioSession everytime. Issue here is two-fold:

  1. Depending on hardware, there will be measurable system hang. For example with iPad 10th generation with iPadOS 17.2 I have measured roughly 500ms hangs when hang detection is enabled on iPad's developer settings. Since these hangs affect main UI thread, different animations or scrolling functionality will suffer from obvious stutter. Commenting out try AVAudioSession.sharedInstance().setActive(false) hangs are no longer observed.
  2. Since shared instance of AVAudioSession is deactivated everytime speak() has completed, other capacitor plugins using AVAudioSession will also get deactivated since same instance is shared between plugins. For example capacitor-community speech-recognition plugin cannot be used in parallel with TextToSpeech plugin since completion of speak() method turns off speech recognition as well.

Describe the solution you'd like:

Perhaps retaining current behaviour as default one is wise so that usage of this plugin remains easy and avoids breaking changes. In order to address the issue presented above, perhaps there should be a way to manually deactivate the session with certain method call e.g. TextToSpeech.deactivateSession()? This way hang caused by deactivation will be observed only once at the time that is most suitable for a given app.

Additionally, it might be needed to allow passing extra options e.g. to current TTSOptions.category in order to satisfy compatibility with other capacitor plugins (admittedly I am not 100% sure about this).

Describe alternatives you've considered:

There are probably better ways to address this issue so take the solution presented above with a grain of salt.