6

I've read many many articles, read many StackOverflow posts and tried different solutions, but I can't get it to work.

For an app that communicates with bluetooth headsets, the user must be able to start/stop a stopwatch when the user presses a button on the headset. When this button is pressed I receive a bluetooth event and start the stopwatch (receiving this bluetooth event in the background is not a problem). When the stopwatch is started I use the AVSpeechSynthesizer to speak the text 'Stopwatch started'. Now comes my problem: I want to have a text to speech every minute, speaking the amount of minutes that elapsed. But also when the app is in the background...

I've read about solutions like this:

var bgTask = UIBackgroundTaskIdentifier()
bgTask = UIApplication.shared.beginBackgroundTask(expirationHandler: {
   UIApplication.shared.endBackgroundTask(bgTask)
})
let timer = Timer.scheduledTimer(timeInterval: 60.0, target: self, selector: #selector(notificationReceived), userInfo: nil, repeats: true)
RunLoop.current.add(timer, forMode: RunLoopMode.defaultRunLoopMode)

It did work, but I can't find anything about how stable this code is on production (when the app is downloaded from the App Store). Also scheduled local notifications can not trigger any code when the app is in the background. Even the property postUtteranceDelay of AVSpeechUtterance seems unstable. I set the property to 460 seconds, but the text was spoken after a random 56 seconds...

I turned on almost all the background modes for my app.

<key>UIBackgroundModes</key>
<array>
    <string>audio</string>
    <string>bluetooth-central</string>
    <string>fetch</string>
    <string>remote-notification</string>
</array>

Other people suggest to make a silent audio track. But it seems that apps using that kind of 'silent tracks' are being refused by the Apple Review team.

What depresses me the most, is that apps like Runkeeper and Seconds Interval Timer are able to do his. Even when I turn on flight mode and disable all settings (like push, motion sensing) from the apps. So, why can't I find a working solution...?

Does anyone have a suggestion where to start?

Jan Doornbos
  • 639
  • 6
  • 25
  • So is the solution above working for you or not...? – caseynolan Feb 13 '18 at 11:38
  • Which solution do you mean? The code sample I provided did work while developing. But I read posts that said that code would not work well on production apps (downloaded from the App Store). So I still don't have a working solution. – Jan Doornbos Feb 14 '18 at 12:32
  • Maybe we could help eachother... I have a question: (https://stackoverflow.com/questions/48851260/strange-sound-behavior-but-only-on-bluetooth) that is somewhat similar. Anyway, I would say in your case the good news is that it must be possible because this is how google maps directions work, correct? It interrupts other audio momentarily with the turn-by-turn directions. – Nerdy Bunz Feb 19 '18 at 11:50
  • Also, from my experience, the post utterance delay does not work unless it is immediately followed by another utterance in the queue... it does not delay its own completion handler. perhaps your 56 seconds relates to your "once-per-minute" schedule +/- 4 seconds rather than the post utterance delay setting (which is probably being ignored). – Nerdy Bunz Feb 19 '18 at 11:50
  • https://stackoverflow.com/questions/28317036/text-to-speech-functionality-when-app-is-in-background-mode – Nerdy Bunz Feb 19 '18 at 11:52

0 Answers0