Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MPNowPlayingInfoCenter Disappears when audio stream stalls

My app plays streaming audio via AVPlayer, and uses MPNowPlayingInfoCenter to display info about the stream on the device lock screen.

This works fine when audio is actually playing, but if the stream stalls due to network slowdowns (i.e. I receive AVPlayerItemPlaybackStalledNotification) the information disappears from the lock screen. But then if the stream resumes playing, it reappears.

This is confusing because when the now-playing info disappears from the lock screen it gives the appearance that the app has stopped playback. But then it resumes playback, when the lock screen UI seems to indicate that this won't happen.

Is there something I can do to make sure the now playing info remains visible whenever the stream should be playing but currently is not due to network speed issues? It seems like the only way to keep a consistent lock screen UI is to actually kill the network connection when it stalls, which is kind of stupid but at least not confusing.

In case more detail would help:

  • When the app gets that notification, the only thing it does is update the UI.
  • The app never clears MPNowPlayingInfoCenter when there's a current program, so as long as the stream is supposed to be playing, there's non-empty data that should be getting displayed.
like image 873
Tom Harrington Avatar asked Apr 21 '15 00:04

Tom Harrington


1 Answers

If I were to take a guess (and it's been a while since I've used AVFoundation) I would assume your audio session is being deactivated by the OS as soon as data stop flowing through the audio buffer. One trick would be to maintain a second AVPlayer which plays back silence to fill in the dead spots until you've buffered enough data to resume playback or reached some timeout and just give up. Use the notification to switch between player objects.

like image 87
Marco Monteiro Avatar answered Sep 26 '22 10:09

Marco Monteiro