使用外部源的子视图中的视频播放器不进行缩放

时间:2016-02-03 00:48:37

标签: ios xcode swift avkit

我关注此视频: https://developer.apple.com/videos/play/wwdc2014-503/

所以我做了一个快速演示并尝试添加容器View并将子视图更改为他们展示的avplayer。然后我添加了代码,虽然速度很快。

区别在于prepareForSegue函数中AVPlayer的构造函数,我写了以下内容:

playerViewController.player = AVPlayer(URL: 
    NSURL(string: "http://jplayer.org/video/m4v/Big_Buck_Bunny_Trailer.m4v")!)

视频非常广泛,发生的事情是视频播放器扩展到屏幕的一半 ios emulator

我尝试了playerViewController.view.sizeToFit()并且在视频控制器的属性上我尝试了所有不同的videoGravity设置

(免责声明:我对IDE很不好。我一般都会避开它们,但是对iOS来说似乎是不可能的)

所有代码:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "showMovie" {
        NSLog("im in!!");
        //self.view.translatesAutoresizingMaskIntoConstraints = false
        let playerViewController = segue.destinationViewController as! AVPlayerViewController
  //playerViewController.view.translatesAutoresizingMaskIntoConstraints = false;

        //setup player view controller for movie
        playerViewController.player = AVPlayer(URL: NSURL(string: "http://jplayer.org/video/m4v/Big_Buck_Bunny_Trailer.m4v")!)

        playerViewController.view.sizeToFit()

        //playerViewController.showsPlaybackControls = true
        //playerViewController.player?.play();
    }
}

这是故事板的截图。我不知道这有多大帮助。

enter image description here

1 个答案:

答案 0 :(得分:0)

我会尝试仅在segue中传递视频的网址。因为在prepareForSegue中,视图控制器的视图可能无法完全初始化并适合使用自动布局的屏幕。

所以在你的实际viewController中:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "showMovie" {
        NSLog("im in!!");
        //self.view.translatesAutoresizingMaskIntoConstraints = false
        let playerViewController = segue.destinationViewController as! AVPlayerViewController

    //setup player view controller for movie
    playerViewController.movieURL= "http://jplayer.org/video/m4v/Big_Buck_Bunny_Trailer.m4v"

}

AVPlayerViewController的viewWillAppear中,使用代码

override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated);
        //setup player view controller for movie
        playerViewController.player = AVPlayer(URL: NSURL(string: self.movieURL)!)

        playerViewController.view.sizeToFit()
}