0

I am using an AVPlayerViewController to play an audio stream, on tvOS, and I am now wanting to have some associated artwork displayed at the same time.

I am not sure whether I should be somehow overlaying it on top or be telling the AVPlayerViewController about the artwork. If it is the latter I can't seem to see the right way to tell the AVPlayerViewController about it? Does anyone have indications as to an appropriate approach?

class StreamPlayerViewController: AVPlayerViewController 

    var playerItem:AVPlayerItem?

    override func viewDidLoad() {
        let videoURL = NSURL(string: "http://example.org/aac.m3u")
        playerItem = AVPlayerItem(URL: videoURL!)
        self.player=AVPlayer(playerItem: playerItem!)

        let playerLayer=AVPlayerLayer(player: player)
        playerLayer.frame=CGRectMake(0, 0, 300, 50)
        self.view.layer.addSublayer(playerLayer)

        // TODO add art work or other metadata, not coming from stream
        // TODO Tell player stream is of infinite time

        self.player?.play()
    }
}
Andre M
  • 6,649
  • 7
  • 52
  • 93
  • 1
    Use the `contentOverlayView`. See my answer here: [AVPlayerViewController using audio-only AVPlayer](http://stackoverflow.com/a/33768236/2415822) – JAL May 03 '16 at 18:22
  • @JAL solution was closer to what I had, but I needed to tweak it for it to centre correctly. I'll share my adaptation as answer. – Andre M May 03 '16 at 19:06
  • @JAL I'll accept your answer, but you may want to add to your example the code to centre appropriately? – Andre M May 03 '16 at 19:46

1 Answers1

0

Following off the comment provided by @JAL (original code), is the following code:

override func viewWillAppear(animated: Bool) {
    let image = UIImage(named: "stationAlbumArt")
    let albumArtView = UIImageView(image: image)
    // ensure view is screen size
    albumArtView.frame =  UIScreen.mainScreen().bounds
    // set bounds, so that image stretch to fill
    albumArtView.bounds = CGRectMake(0, 0, image!.size.width, image!.size.height)
    self.contentOverlayView?.addSubview(albumArtView)
}

The two key additions here are setting the frame and the bounds. The frame here is using the screen size, since self.view.frame is not available until the viewDidAppear() is called. The bounds need to be set to ensure the image doesn't stretch to fill the screen.

One other thing, while the AVPlayerLayer needs to be instantiated it doesn't actually seem to need to be added as a layer, at least based on experimentation.

The only thing missing at this point is the ability to mask the time bar, without losing the audio control menu.

Community
  • 1
  • 1
Andre M
  • 6,649
  • 7
  • 52
  • 93