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

bug: Audio playing from other App does not return to full volume after finishing speaking #138

Open
Solarus8 opened this issue Oct 20, 2024 · 8 comments
Labels
bug/fix Something isn't working needs: triage platform: ios iOS platform

Comments

@Solarus8
Copy link

Plugin version:
"@capacitor-community/text-to-speech": "^5.0.0"

Platform(s):
iPhone 15 Pro Max, iOS 18.0.1

Current behavior:
When playing other audio (music app - like Pandora) the volume of that audio does not return to full volume after it is attenuated during the text-2-speech content is finished playing

Expected behavior:
The audio of any other app will return to full volume after the text-2-speech content is finished playing.

Steps to reproduce:
Open Pandora (or other audio app), open capacitor app with text-2-speech, play text, notice volume is about halved when text is read (this is good) and then the volume of Pandora stays at halved volume until the app is sent to the background or closed.

@robingenz robingenz added platform: ios iOS platform bug/fix Something isn't working labels Oct 20, 2024
@robingenz
Copy link
Member

Would you be willing to create a PR?

@flexiblefactory
Copy link

flexiblefactory commented Oct 20, 2024

I think something like this could be the fix:

// Reset the audio session category to normal after TTS finishes

    public func speechSynthesizer(_ synthesizer: AVSpeechSynthesizer, didCancel utterance: AVSpeechUtterance) {
        // Reset session when speech cancelled
         try AVAudioSession.sharedInstance().setActive(false)
         self.resolveCurrentCall()
    }

    public func speechSynthesizer(_ synthesizer: AVSpeechSynthesizer, didFinish utterance: AVSpeechUtterance) {
        // Reset session when speech finishes
        try AVAudioSession.sharedInstance().setActive(false)
        self.resolveCurrentCall()
    }

@flexiblefactory
Copy link

I have tested calling AVAudioSession.sharedInstance().setActive(false)) after the speech and confirmed that this is the fix to restore normal audio volume to other applications

@Solarus8
Copy link
Author

Solarus8 commented Oct 21, 2024 via email

@flexiblefactory
Copy link

It is a plugin currently and works as a workaround. I do think it would make sense for the changes above to be integrated into the TTS plugin.

@physxP
Copy link
Contributor

physxP commented Oct 23, 2024

I think something like this could be the fix:

// Reset the audio session category to normal after TTS finishes

    public func speechSynthesizer(_ synthesizer: AVSpeechSynthesizer, didCancel utterance: AVSpeechUtterance) {
        // Reset session when speech cancelled
         try AVAudioSession.sharedInstance().setActive(false)
         self.resolveCurrentCall()
    }

    public func speechSynthesizer(_ synthesizer: AVSpeechSynthesizer, didFinish utterance: AVSpeechUtterance) {
        // Reset session when speech finishes
        try AVAudioSession.sharedInstance().setActive(false)
        self.resolveCurrentCall()
    }

I believe this approach may cause UI stuttering if setActive is called during a UI animation or, for instance, when a Camera Preview is active. Handling AVAudioSession on a background queue introduces a lag of approximately 300-500ms, varying based on the device. A more appropriate solution might be to provide explicit start or stop methods, allowing users to control when the audio session is activated or deactivated, avoiding these performance issues. This would give developers finer control over managing the audio session in relation to their app's lifecycle and UI elements.

cc: @robingenz @Solarus8

@flexiblefactory
Copy link

The other aspect to this is that the plugin also sets active true at startup time, causing other audio to "duck" as soon as the plugin loads (and then ducking persists until the app quits).

@physxP
Copy link
Contributor

physxP commented Oct 28, 2024

The other aspect to this is that the plugin also sets active true at startup time, causing other audio to "duck" as soon as the plugin loads (and then ducking persists until the app quits).

That's true. So, if setActive method is configurable from TS side then think it could fix all of these issues at once?
This could be an API breaking change if implemented in the most efficient manner. I am thinking of creating a PR for this to keep both worlds happy.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug/fix Something isn't working needs: triage platform: ios iOS platform
Projects
None yet
Development

No branches or pull requests

4 participants