Simon McLoughlin
Simon McLoughlin

Reputation: 8465

When using AVPlayerViewController can't display MPMediaItemPropertyArtwork during Airplay

A section of my app displays various forms of media from an API, images, videos and songs (audio + an image). I'm using AVPlayerController and using an UIImageView on top of its contentOverlayView to display song album images. I am setting the nowPlayingInfo dictionary, something like this when the media starts playing:

private func setupNowPlaying() {
        
    let elapsedTime = CMTimeGetSeconds(playerController?.player?.currentTime() ?? CMTime(value: 0, timescale: CMTimeScale()))
    let duration = CMTimeGetSeconds(playerController?.player?.currentItem?.duration ?? CMTime(value: 0, timescale: CMTimeScale()))
        
    MPNowPlayingInfoCenter.default().nowPlayingInfo = [
        MPMediaItemPropertyAlbumTitle: airPlayAlbum,
        MPNowPlayingInfoPropertyPlaybackRate: 1.0,
        MPMediaItemPropertyTitle: airPlayName,
        MPMediaItemPropertyArtist: airPlayArtist,
        MPNowPlayingInfoPropertyElapsedPlaybackTime: elapsedTime,
        MPMediaItemPropertyPlaybackDuration: duration,
        MPNowPlayingInfoPropertyMediaType: 1
    ]
        
    if let image = imageView?.image {
        MPNowPlayingInfoCenter.default().nowPlayingInfo?[MPMediaItemPropertyArtwork] = MPMediaItemArtwork(boundsSize: image.size) { size in
            return image.resizedImage(size: size)
        }
    }
        
    setupCommands()
    hasSetupNowPlaying = true
}

This correctly displays everything in the iOS command centre for videos and songs. However when using the airplay button inside the AVPlayerViewController to cast to an external TV, it only displays the song title and a black screen with playback controls while the audio is playing, no album artwork. Videos display correctly.

Following a recommendation online I tried setting:

playerController?.player?.allowsExternalPlayback = false

This hides the inbuilt airplay button on the playerController, but when using the command centre to trigger airplay, it now correctly displays album artwork, song title, album name etc on the external TV. With this setting on, videos no longer cast to the external TV, so that's not really a solution

Is there a solution to leaving allowsExternalPlayback set to true, but also displaying the album artwork? There are lots of complaints online about this, but there seems to be no resolution.

Upvotes: 0

Views: 134

Answers (0)

Related Questions