Why is AVPlayer not continuing to next song while in background?

北战南征 提交于 2019-12-04 19:05:59

When the observer notifies me at the item end, I then create a new AVPlayerItem and do it all over again

But the problem is that meanwhile play stops, and the rule is that background playing is permitted only so long as you were playing in the foreground and continue to play in the background.

I would suggest using AVQueuePlayer instead of AVPlayer. This will allow you to enqueue the next item while the current item is still playing — and thus, we may hope, this will count as continuing to play.

I encountered the similar problem, and I searched lots of websites on google, but didn't find the answer.

The Phenomenon

The problem of my app is that when I start play an audio, and turn the app to background, it will finish the playing of the current audio, and when playing the second audio, it will load some data, but then it stopped, if I turn the app to foreground, it will play the audio.

Solution

My solution is to add the following call.

UIApplication.shared.beginReceivingRemoteControlEvents()

So enable background audio capabilities is not enough, we need to begin receiving remote controller events.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!