Is WKAudioFilePlayer broken?

It's not just in my app, it appears to be nonfunctioning in other apps as well (e.g. Outcast). I've done a full system restore. Audio stored in the shared container directory plays perfectly fine through the media player or AVAudioPlayer, but any attempted use of WKAudioFilePlayer/WKAudioFileQueuePlayer brings up the prompt to choose your earbuds, and it never "connects" even when already connected.


I'll be submitting a proper bug report but wanted to know if other people were experiencing it. This is an app destroyer.

Replies

Outcast developer here. When it works, it works well, but it seems to get into a state sometimes where it seems nothing will get playback working again. The weird thing is that when this happens, playback is fine in Apple Music / Apple Radio. There’s a whole range of ways it appears to fail but nothing conclusive. * sometimes freezes app * sometimes progresses playback but no audio * sometimes the separate AirPods are it of sync with each other * sometimes playback happens for a second or two then won’t work Probably unrelated: One interesting thing is that I often get “AVPlayerItem cannot be associated with more than one instance of AVPlayer”, but then it’ll begin playback anyway. Unsure if this is related.

Thank you! I'm following up a request on the bug report for this with a sysdiagnose, and I'll do my best to give the devs an overview of my watch & app's state currently, as it's stuck like this. What you've described is the exact issue I'm having, and I've been unable to get the watch as a whole out of this state, even with a full system reset, unpairing and re-pairing earbuds, etc. The music and radio apps pipe out just fine, however. My guess is whatever strategy they took to make the WKAudioFilePlayer/Item run out of process uses some sort of persisted/hashed state that, given a specific series of events, ends up pointing to a no-longer-existing entity/something it can't reify. I have no idea, the API is unusual.

Well, for anyone facing this that wants a workaround, you can actually still make the WKAudioFilePlayer work despite the shenanigans of the non-working "Choose where to play music:" prompt.


//keep your player nullable & in global scope
var player: WKAudioFilePlayer?

class WhateverController: WKInterfaceController {

  //don't invoke play, don't add a KVO
  //set its rate to 0.999
  //optionally add a quick delay to set it 1
  @IBAction func playButtonTapped() {
      let url = Bundle.main.url(forResource: "sample", withExtension: "mp3")!

      let item = WKAudioFilePlayerItem(asset: WKAudioFileAsset(url: url))

      player = WKAudioFilePlayer(playerItem: item)

      player!.rate = 0.999

      DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.01) {
        player!.rate = 1
      }
  }
}

😠 😢 😁


Additionally, if you want to hide the prompt, add this after setting the rate:

      DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.5) {
         if player == nil || player!.currentTime == 0 { return }
     
         OperationQueue.main.addOperation {
            DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.01) {
               self.dismiss()
            }
         }
      }

Appears to be working now in this week's beta.