我有VideoViewController.swift,我想按此按钮在secondviewcontroller中播放视频(不是全屏显示)



我尝试使用模式视图控制器,但是它不起作用..什么问题?

这是源代码:
VideoViewController.swift

import UIKit
import AVKit
import AVFoundation

class VideoViewController: UIViewController {

    @IBOutlet weak var AVPlayerView: UIView!

    override func shouldAutorotate() -> Bool {
        return false
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        // yoon // from web
        let sampleURL = NSURL(string: "http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4")!
        let player = AVPlayer(URL: sampleURL)


        let playerLayer = AVPlayerLayer(player: player)

        let videoWidth = 320
        let videoHeigh = 180
        let screenWidth = self.view.frame.size.width
        let screenHeigh = self.view.frame.size.height

        playerLayer.frame = CGRectMake(
            (screenWidth/2 - CGFloat(videoWidth/2)),
            (screenHeigh/2 - CGFloat(videoHeigh/2)),
            CGFloat(videoWidth),
            CGFloat(videoHeigh))

        AVPlayerView.layer.addSublayer(playerLayer)


       player.play()

        //AVPlayerView.backgroundColor = UIColor.blackColor()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

}


viewController。迅速

import UIKit
import Social
import AVKit
import AVFoundation

class ViewController: UIViewController, SideBarDelegate {

    @IBOutlet weak var AVPlayerView: UIView!

    override func shouldAutorotate() -> Bool {
        return false
    }

    @IBAction func videoStream(sender: AnyObject) {


        self.modalTransitionStyle = UIModalTransitionStyle.CoverVertical
        // Cover Vertical is necessary for CurrentContext
        self.modalPresentationStyle = .CurrentContext
        // Display on top of    current UIView


        self.presentViewController(VideoViewController(), animated: true, completion: nil)
        //self.preferredContentSize = CGSizeMake(320, 200)
}

最佳答案

您的代码中有几个强制展开的可选选项。打开包装之前,请确保已设置插座。以下将在第二个vc中播放视频,但是您需要添加框架和其他所需的内容。第一个vc就可以了

import UIKit
import AVKit
import AVFoundation

class VideoViewController: AVPlayerViewController {

    @IBOutlet weak var AVPlayerView: UIView!

    override func shouldAutorotate() -> Bool {
        return false
    }

    private func playVideo() {
        player = AVPlayer(URL: NSURL(string: "http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4")!)
    }

    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        playVideo()
    }

}

10-07 21:07