Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS text-to-speech in background

I am having an intermittent (aargh!) problem playing Text-to-Speech in the background, triggered from Apple Watch. I have properly set up the background mode, the AVSession category, and the WatchKitExtensionRequest handler. (See below.) I had this working before, and can't figure out what changed. (Could it be iOS 9 has issues? "Before" means, among other things, iOS 8.)

The problem is this: when the app gets the request from the Watch and the app is either in the background or the phone is sleeping (locked), the speech sometimes plays right away, and other times doesn't play until the app is brought to the foreground. The OS seems to be sometimes queuing the audio, and sometimes not. I can't find any common thread between success and failure cases. I can verify with logging that the call to speakUtterance() is being made in all situations. But its behavior varies, apparently randomly. The only clue is that it might be the case that the longer the app is in the background, the less likely it is to speak right away.

This is making me pull my hair out. Suggestions welcome.

In info.plist:

Required background modes: App plays audio or streams audio/video using AirPlay

In AppDelegate.application:didFinishLaunching:withOptions():

do {
    try AVAudioSession.sharedInstance().setCategory(
        AVAudioSessionCategoryPlayback, 
        withOptions:.DuckOthers
    )
    try AVAudioSession.sharedInstance().setActive(true)
} catch let error as NSError {
    // etc...
}

In AppDelegate.application:handleWatchKitExtensionRequest...():

var bgTaskId:UIBackgroundTaskIdentifier = 0
bgTaskId = application.beginBackgroundTaskWithName(
    "Prose WKE handler",
    expirationHandler: {
        application.endBackgroundTask(bgTaskId)
    }
)
//... Post notification to call Text-to-Speech
application.endBackgroundTask(bgTaskId)
like image 303
Andrew Duncan Avatar asked Nov 29 '15 01:11

Andrew Duncan


People also ask

Have you enabled the audio background mode in your iOS app?

Under the Capabilities tab, set the Background Modes switch to ON and select the “Audio, AirPlay, and Picture in Picture” option under the list of available modes. With this mode enabled and your audio session configured, your app is ready to play background audio.

Does iPhone have a text to speech feature?

You can have your iPhone speak your screen out loud. By tapping into the Speech setting on your iPhone, you can hear the entire screen read aloud from top to bottom or just selected text. You can listen to text as you type it, word by word or each character.


1 Answers

Here's a workaround: play a second snippet of sound (I used a half-second of silence), using AVAudioPlayer, right after the call to speakUtterance(), This seems to "jog the pipeline".

like image 145
Andrew Duncan Avatar answered Oct 10 '22 13:10

Andrew Duncan