简体   繁体   English

Swift / AppleTV4:如何将AVPlayer函数连接到AVPlayerViewController?

[英]Swift/AppleTV4: How to connect AVPlayer function to AVPlayerViewController?

Here is a rudimentary playMe function calling AVPlayer , playing a MP3, MP4 or Wav via Swift with AppleTV. 这是一个名为AVPlayer的基本playMe函数,可通过AppleTV通过Swift播放MP3,MP4或Wav。 How do I combine this with the AVPlayerViewController - ie, 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? 如何将其与AVPlayerViewController结合使用-即,如何使playMe(“ video”,“ mp4”)在AVPlayerViewController播放,在Main.storyboard和AVPlayerViewController之间建立连接需要哪些步骤? GUI和Swift代码中?

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()
}

One way you could do this is by subclassing AVPlayerViewController . 一种实现方法是子类化AVPlayerViewController AVPlayerViewController has an AVPlayer property named player . AVPlayerViewController具有一个名为playerAVPlayer属性。 A subclass of AVPlayerViewController might look something like this: AVPlayerViewController的子类可能看起来像这样:

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. 此实现将显示默认的播放控件,并且可以与Siri遥控器一起使用。

Here is the code to do this via a button press using prepareForSegue: 这是使用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()
        }
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM