0

When playing an audio file in watchOS (using SpriteKit):

run(SKAction.playSoundFileNamed("ready.wav", waitForCompletion:false))

It will works until you background the app (says face the watch outwards) and back, then the audio file cannot be played anymore.

I have tried many workarounds, including setting UIBackgroundModes to audio, but the sound will always stop working after the app is background and back. Any solution?

PS: I think I might have found the answer: use AVAudioPlayer instead of SKAction.

Lim Thye Chean
  • 8,704
  • 9
  • 49
  • 88

2 Answers2

0

You need to start HKWorkoutSession and in info.plist enable workout background processing. Don't need the audio to play in background, just continue to play once it comes back from background.

Lim Thye Chean
  • 8,704
  • 9
  • 49
  • 88
coolioxlr
  • 278
  • 1
  • 7
  • Thanks. Searching for HKWorkoutSession leads me to https://developer.apple.com/documentation/healthkit/hkworkoutsession and instruction is "To run in the background, you must add the WKBackgroundModes key to your WatchKit extension’s Info.plist file. This key’s value is an array containing the workout-processing value.". But adding WKBackgroundModes a value of workout-processing (claimed to be the only value) cause compilation problem: info.list contains a key that is not in the whitelist of allowed keys for a WatchKit app. Any idea? – Lim Thye Chean Apr 27 '18 at 07:07
0

Use AVAudioPlayer instead of SKAction. This works.

Only SKAction.playSoundFileNamed has this issue.

Lim Thye Chean
  • 8,704
  • 9
  • 49
  • 88