swiftavfoundationavplayeravqueueplayer

How to set NowPlaying properties with a AVQueuePlayer in Swift?


I have an AVQueuePlayer that gets songs from a Firebase Storage via their URL and plays them in sequence.

static func playQueue() {
    for song in songs {
        guard let url = song.url else { return }
        lofiSongs.append(AVPlayerItem(url: url))
    }
    if queuePlayer == nil {
        queuePlayer = AVQueuePlayer(items: lofiSongs)
    } else {
        queuePlayer?.removeAllItems()
        lofiSongs.forEach { queuePlayer?.insert($0, after: nil) }
    }
    queuePlayer?.seek(to: .zero) // In case we added items back in
    queuePlayer?.play()
}

And this works great. I can also make the lock screen controls appear and use the play pause button like this:

private static func setRemoteControlActions() {
    let commandCenter = MPRemoteCommandCenter.shared()

    // Add handler for Play Command
    commandCenter.playCommand.addTarget { [self] event in
        queuePlayer?.play()
        return .success
    }

    // Add handler for Pause Command
    commandCenter.pauseCommand.addTarget { [self] event in
        if queuePlayer?.rate == 1.0 {
            queuePlayer?.pause()
            return .success
        }
        return .commandFailed
    }
}

The problem comes with setting the metadata of the player (name, image, etc). I know it can be done once by setting MPMediaItemPropertyTitle and MPMediaItemArtwork, but how would I change it when the next track loads?


Solution

  • I'm not sure if my approach works for AVQueueplayer, but for playing live streams with AVPlayer you can "listen" to metadata receiving.

    extension ViewController: AVPlayerItemMetadataOutputPushDelegate {
    
        func metadataOutput(_ output: AVPlayerItemMetadataOutput, didOutputTimedMetadataGroups groups: [AVTimedMetadataGroup], from track: AVPlayerItemTrack?) {
    
            //look for metadata in groups
    
        }
    
    }
    

    I added the AVPlayerItemMetadataOutputPushDelegate via an extension to my ViewController.

    I also found this post.

    I hope this gives you a lead to a solution. As said I'm not sure how this works with AVQueuePlayer.