Playing audio in a WatchKit workout app when backgrounded and screen is off

1.2k views Asked by At

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.

  1. Start workout + keep app on screen = sound plays ok.
  2. Start workout + move app to background by tapping digital crown + keep screen on = sound plays ok.
  3. Start workout + lower wrist = screen switches off and sound no longer plays.
  4. 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?

2

There are 2 answers

2
John Martin On BEST ANSWER

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!

1
Valeo T On

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.