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: #98

Open
dtrigo opened this issue Mar 31, 2023 · 2 comments
Open

bug: #98

dtrigo opened this issue Mar 31, 2023 · 2 comments

Comments

@dtrigo
Copy link

dtrigo commented Mar 31, 2023

Plugin version:
2.1.0

Platform(s):
iOS

Current behavior:
Error when trying to read text when in background mode

Expected behavior:
Read text

Steps to reproduce:
I'm trying to read a remote notification as soon as received, this works flawlessly in active mode, but when the app is in foreground it rejects the speak method with the message "session activation failed"

BTW category is set to 'playback'

I'm assuming, as the app receives the remote notification, it turns the app into active mode and the notification has a higher AV priority, so the request to setActive fails. With the snippet provided it works as expected, but since I'm not an expert, I would like you to address it (if you can)

Related code:
This is my workaround, added some tweaks...

@objc public func speak(_ text: String, _ lang: String, _ rate: Float, _ pitch: Float, _ category: String, _ volume: Float, _ voice : Int, _ call: CAPPluginCall) throws {
        self.synthesizer.stopSpeaking(at: .immediate)

        var avAudioSessionCategory = AVAudioSession.Category.ambient
        if category != "ambient" {
            avAudioSessionCategory = AVAudioSession.Category.playback
        }
        
        if #available(iOS 13.0, *) {
            self.synthesizer.usesApplicationAudioSession = false
        }

        do {
            try AVAudioSession.sharedInstance().setCategory(avAudioSessionCategory, mode: .default, options: AVAudioSession.CategoryOptions.duckOthers)
            try AVAudioSession.sharedInstance().setActive(true)
        } catch {
            // print(error)
        }

        self.calls.append(call)

        let utterance = AVSpeechUtterance(string: text)
        utterance.voice = AVSpeechSynthesisVoice(language: lang)
        utterance.rate = adjustRate(rate)
        utterance.pitchMultiplier = pitch
        utterance.volume = volume

        //Find the voice associated with the voice parameter if a voice specified. 
        //If the specified voice is not available we will fall back to default voice rather than raising an error. 
        if (voice >= 0) {
            let allVoices = AVSpeechSynthesisVoice.speechVoices()
            if (voice < allVoices.count) {
                utterance.voice = allVoices[voice]
            }
        }

        synthesizer.speak(utterance)
        try AVAudioSession.sharedInstance().setActive(false)
    }

Other information:

Capacitor doctor:

insert the output from `npx cap doctor` here
@nemoneph
Copy link

Thx for the info @dtrigo .
I have exactly the same issue (same context, error) for a GPS app with vocal navigation when phone is locked.

unfortunately I don't have the skills to say if your fix is good or / not.
How can I test your code / override the plugin code ?

@dtrigo
Copy link
Author

dtrigo commented Sep 11, 2023

@nemoneph I just updated the plug-in fn code to the ode I posted. To test it, just send the app to foreground and send a notification to read it aloud (I think you can simulate it)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants