I have an intermittent (aargh!) Problem playing Text-to-Speech in the background caused by Apple Watch. I correctly configured the background mode, the AVSession category, and the WatchKitExtensionRequest handler. (See below.) I have had this before, and I cannot understand what has changed. (Maybe iOS 9 has problems? Do means, among other things, iOS 8.)
The problem is this: when the application receives a request from Watch, and the application is either in the background or the phone is asleep (locked), speech sometimes plays immediately, and in other cases it does not play until the application is brought to the forefront. It seems that the OS sometimes queues sound, and sometimes not. I cannot find a common flow between successes and failures. I can verify with logging that the speakUtterance () call is made in all situations. But his behavior seems to be changing randomly. The only clue is that it may be that the longer the application is in the background, the less likely it will start talking immediately.
It makes me pull my hair out. Suggestions are welcome.
In info.plist:
Necessary background modes: the application plays audio or streams of audio / video using AirPlay
In the AppDelegate.application: didFinishLaunching: withOptions () application:
do {
try AVAudioSession.sharedInstance().setCategory(
AVAudioSessionCategoryPlayback,
withOptions:.DuckOthers
)
try AVAudioSession.sharedInstance().setActive(true)
} catch let error as NSError {
}
In the AppDelegate.application application: handleWatchKitExtensionRequest ... ():
var bgTaskId:UIBackgroundTaskIdentifier = 0
bgTaskId = application.beginBackgroundTaskWithName(
"Prose WKE handler",
expirationHandler: {
application.endBackgroundTask(bgTaskId)
}
)
application.endBackgroundTask(bgTaskId)
source
share