0

Here is a rudimentary playMe function calling AVPlayer, playing a MP3, MP4 or Wav via Swift with AppleTV. How do I combine this with the AVPlayerViewController - i.e., how do I make the playMe("video", "mp4") play inside an AVPlayerViewController, what are the required steps to make a connection between the Main.storyboard and the AVPlayerViewController, in the GUI and in the Swift code?

func playMe(inputfile: String, inputtype: String) {
    let path = NSBundle.mainBundle().pathForResource(inputfile, ofType:inputtype)!
    let videoURL = NSURL(fileURLWithPath: path)
    let player = AVPlayer(URL: videoURL)
    let playerLayer = AVPlayerLayer(player: player)
    playerLayer.frame = self.view.bounds
    self.view.layer.addSublayer(playerLayer)
    player.play()
}
esaruoho
  • 896
  • 1
  • 7
  • 25
  • I'm not familiar with tvOS but googling "[tvos video playback controls](https://www.google.com/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=tvos%20video%20playback%20controls)" shows [that Apple says](https://developer.apple.com/library/tvos/documentation/AVFoundation/Reference/AVPlayerViewController_Class/index.html) `AVPlayerViewController` "displays the video content of an AVPlayer object along with system-supplied playback controls." So I think you have to go that direction... at least in 'the apple way' – New Alexandria Jan 05 '16 at 15:01

1 Answers1

1

One way you could do this is by subclassing AVPlayerViewController. AVPlayerViewController has an AVPlayer property named player. A subclass of AVPlayerViewController might look something like this:

import UIKit
import AVKit

class MyPlayerViewController: AVPlayerViewController {

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        let path = NSBundle.mainBundle().pathForResource("myVideo", ofType:"mov")!
        let videoURL = NSURL(fileURLWithPath: path)
        player = AVPlayer(URL: videoURL)
    }

}

This implementation would show the default playback controls and would work out of the box with the Siri remote.

Here is the code to do this via a button press using prepareForSegue:

import UIKit
import AVFoundation
import AVKit
let playerViewControllerSegue = "play";
class MyViewController: UIViewController {    
    @IBAction func playMovie(sender: UIButton) {
        self.performSegueWithIdentifier(playerViewControllerSegue, sender: self);
    }

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        if (segue.identifier == playerViewControllerSegue){
            let path = NSBundle.mainBundle().pathForResource("7second", ofType:"mp4")!
            let videoURL = NSURL(fileURLWithPath: path)
            let player = AVPlayer(URL: videoURL)
            let playerViewController = segue.destinationViewController as! AVPlayerViewController
            playerViewController.player = player
            playerViewController.player?.play()
        }
    }

}
Jonathan
  • 291
  • 2
  • 6
  • Hi.. How would this be tied into being opened via a playMe(filename, filetype) inside a button? Just wondering, like. – esaruoho Jan 13 '16 at 19:25
  • 1
    You should be able to either use a segue from a button (probably the easiest way) or use presentViewController to present MyPlayerViewController. If you are going to use a segue, in your playMe function you would simple do self.performSegueWithIdentifier(identifier, sender: self) and then in prepareForSegue you could setup your player and set your player on the destinationViewController. – Jonathan Jan 13 '16 at 23:09