iosavfoundationwatchkitwatchoswatchos-4

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


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.

  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...

I've tried:

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?


Solution

  • 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!