Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(iOS): Prevent UI hangs by optimizing AVAudioSession management #135

Merged
merged 2 commits into from
Sep 21, 2024

Conversation

physxP
Copy link
Contributor

@physxP physxP commented Sep 19, 2024

Pull Request Checklist

Please ensure your PR fulfills the following requirements:

  • The changes have been tested successfully.

Description

This PR addresses a critical issue in the iOS implementation of the Text-to-Speech plugin where starting or stopping speech causes UI hangs and stutters. This problem is especially noticeable when using TTS alongside resource-intensive features like the camera, leading to a degraded user experience.

Problem

  • UI Hangs and Stutters: The current implementation activates and deactivates the AVAudioSession every time speech is started or stopped. These operations are expensive and block the main thread, causing UI responsiveness issues.
  • Pronounced with Camera Usage: In applications where TTS is used concurrently with the camera, the stutters become highly pronounced due to the combined resource demands.

Solution

  • Activate AVAudioSession Once During Initialization:
    • Moved the activation of the AVAudioSession to the init method of the plugin.
    • This change ensures that the audio session is activated only once when the plugin is initialized.
  • Maintain Active Audio Session:
    • By keeping the audio session active, we eliminate the overhead of repeated activation and deactivation.
    • This reduces blocking operations on the main thread, preventing UI hangs and stutters.

Benefits

  • Improved Performance: The UI remains responsive, and the stutters are eliminated when using TTS, even alongside the camera.
  • Smooth User Experience: Users experience seamless speech output without interruptions or delays.

Testing

  • The changes have been tested extensively in our application.
  • We observed that the UI stutters are resolved, and the TTS functionality works as expected without any adverse effects.

Considerations

  • Resource Management: While keeping the AVAudioSession active may slightly increase resource usage, our testing indicates that the impact is minimal.
  • Audio Session Behavior: Developers should be aware that maintaining an active audio session might affect how the app interacts with other audio sessions (e.g., background music from other apps).

Related Issue

Close #111: feat: iOS improve performance and compatibility with other capacitor plugins


Note: A review of this fix would be greatly appreciated. We believe it significantly improves the plugin's performance on iOS devices, especially for apps that use TTS in combination with other resource-intensive features.

@robingenz
Copy link
Member

Please merge master into your branch.

@physxP
Copy link
Contributor Author

physxP commented Sep 20, 2024

@robingenz it's merged.

Copy link
Member

@robingenz robingenz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

@robingenz
Copy link
Member

Audio Session Behavior: Developers should be aware that maintaining an active audio session might affect how the app interacts with other audio sessions (e.g., background music from other apps).

@physxP We will give this a try. If there are any complaints about this, I will revert the changes and we will have to look for another solution.

@robingenz robingenz merged commit aaa4ace into capacitor-community:master Sep 21, 2024
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

feat: iOS improve performance and compatibility with other capacitor plugins
2 participants