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.
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.
I'm trying to figure out what I've missed in order to support audio in a workout app when the screen is off...
HKWorkoutSession
.WKBackgroundModes
UIBackgroundModes
.I've tried:
AVAudioPlayer
AVAudioEngine
+AVAudioPlayerNode
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 to AVAudioSessionCategoryPlayback
(I had it set to ambient).
As soon as I changed the session category to AVAudioSessionCategoryPlayback it worked!
To play audio while in background you also need to set the UIBackgroundModes to "audio" in the extension plist apart from setting the "workout processing" background mode.
Refrence HKWorkoutSession
To play audio or provide haptic feedback from the background, you must also add the UIBackgroundModes key to your WatchKit extension’s Info.plist file. This key’s value is an array containing the audio value.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With