I'm trying to build a WatchKit app that will give audio feedback during a workout. The audio is a series of short pre-recorded voice clips.
- The app starts a workout session
- Audio clips play fine when the app is active
- Audio also plays fine when the app is in the background whilst the screen is on (e.g. by tapping the digital crown, or opening another app).
However, when the screen turns off - e.g. by starting a workout and then lowering the wrist - the audio doesn't play until the app becomes active again.
i.e.
- Start workout + keep app on screen = sound plays ok.
- Start workout + move app to background by tapping digital crown + keep screen on = sound plays ok.
- Start workout + lower wrist = screen switches off and sound no longer plays.
- Start workout + move app to background + lower wrist = screen switches off and sound no longer plays.
I'm trying to figure out what I've missed in order to support audio in a workout app when the screen is off...
- The app has an active
HKWorkoutSession
. - The extension has the "workout-processing" background mode enabled under
WKBackgroundModes
- The extension also has the "audio" background mode enabled under
UIBackgroundModes
. - The extension has the HealthKit entitlement and all HealthKit features are working.
- On the iOS app, I've added the 'audio' background mode.
I've tried:
- playing the audio clips using
AVAudioPlayer
- playing audio using
AVAudioEngine
+AVAudioPlayerNode
- giving up on the clips entirely and using AVSpeechSynthesizer
The behaviour is the same - as soon as the screen switches off, the sound no longer plays. The sound does play if the app is in the background and the screen is still on.
The app is running on Watch OS 4. I've tested on a Series 2 and a Series 3 watch and it is the same on both.
What have I missed?
Ahhh, finally I found this post and realised I had the same problem:
The
AVAudioSession
category needs to be set toAVAudioSessionCategoryPlayback
(I had it set to ambient).As soon as I changed the session category to AVAudioSessionCategoryPlayback it worked!