1

I'm trying to make a custom player to SoundCloud and as there is no native support for a full fledged player in Swift-iOS (forward & backward) functionality; I then decided to use AVPlayer instead of AVQueuePlayer.

I have a Tracks class which holds relevant information about my tracks (Url's). A TracksQueue which has relevant info about the queue - multiple tracks & if I can forward or reverse logic by checking array indices of tracks array.

I have also enabled background mode in App settings as well as added the following code as many had suggested to do so while App launches in my ViewController's viewDidLoad()

UIApplication.shared.beginReceivingRemoteControlEvents()

Everything is working as expected but only problem is the track will change in background but it will not play.

All I do is remove observers from AVPlayer and initiate a new one, and replace current item with new AVPlayerItem upon NSNotification.Name.AVPlayerItemDidPlayToEndTime notification.

fileprivate func restartCurrentTrack() {
    if (player != nil) {
            player?.removeObserver(self, forKeyPath: Keys.Status, context: &PGAudioPlayerManagetKVOContext)
    }

    if let _currentTrack = queue.currentTrack {
        player = AVPlayer()
        player.addObserver(self, forKeyPath: Keys.Status, options: NSKeyValueObservingOptions.new, context: &PGAudioPlayerManagetKVOContext)
        if let _playerItem = _currentTrack.getPlayerItem() {
            player?.replaceCurrentItem(with: _playerItem)
        }
    }
}

Edit 1: I've found one reason why the play is not happening when the app goes to background. I've the KVO which tells me when the player is ready to play by checking its status. Only when I receive the same I start

player.play()

But when the app is in background, it no longer receives this notification. Is there a way how I can get the notification even when the app is in background?

Edit 2: To clarify, app does play music if music starts at foreground. Problem is changing tracks in background is when KVO of player ready never gets triggered, unless I bring app to foreground.

user6083088
  • 1,047
  • 1
  • 9
  • 27
  • My app does play music in background, problem occurs when changing tracks or starting another AVPlayer instance is when KVO doesnt work – user6083088 Aug 30 '17 at 20:17
  • I retracted the flag, however, this is another duplicate: https://stackoverflow.com/questions/36348332/why-is-avplayer-not-continuing-to-next-song-while-in-background , did you try searching through the questions that exists already? maybe you will find something that you havnt tried yet –  Aug 31 '17 at 06:15
  • @Sneak Yes, I have seen this and many similar posts. But none of them propose a solution other than theory. And as of now it just doesn't work. The propose solution only talks about advancing to next item, but I also need to reverse and AVQueuePlayer doesn't support that. Many proposed AVPlayer but as soon app goes to background, and track changes all proposed solution on SO fails. – user6083088 Aug 31 '17 at 06:25
  • hmm.. I can't try it myself however takes too long time to set up, but from reading the similiar posts I linked I **think** , AVQueuePlayer might be the solution. but as you say you need AVPlayer funcitonality, I am not sure tho. GL! –  Aug 31 '17 at 07:12
  • 1
    @Sneak I got it to work using couple of threads on SO. I find it so strange that Apple makes no public announcement like this. I guess we just have to pull out the best to get it done. Thanks mate! – user6083088 Aug 31 '17 at 22:18
  • Nice one, good to know you found the answer! –  Sep 01 '17 at 07:11

1 Answers1

1

I used the information provided on SO in the following post

  1. AVPlayer Not Loading Media In Background
user6083088
  • 1,047
  • 1
  • 9
  • 27