0

我有一个 tvOS 应用程序的以下代码。它可以按我的意愿播放视频,但是我设置的 contentOverlayView 没有出现。从我看过的例子来看,我认为它应该可以工作。有人可以解释我做错了什么吗?

import Foundation
import UIKit
import AVKit

class videoPlayer:  AVPlayerViewController {

var thePlayer: AVPlayer?
var movieToPlay: AVPlayerItem?
var movieURL: String?
let theVideoPlayer = AVPlayerViewController()

@IBOutlet weak var videoInfo: UIView!

override func viewDidLoad(){
    theVideoPlayer.contentOverlayView?.addSubview(videoInfo)
    theVideoPlayer.player = thePlayer

    self.view.addSubview(theVideoPlayer.view)
    theVideoPlayer.view.frame = self.view.frame
    NotificationCenter.default.addObserver(self, selector: #selector(videoPlayer.videoDone), name: NSNotification.Name(rawValue: "AVPlayerItemDidPlayToEndTimeNotification"), object: thePlayer?.currentItem)
    playVideo(movieURL!)

}

func playVideo(_ movieURL: String){
    let movieToPlay = AVPlayerItem(url: URL(string: movieURL)!)
    thePlayer = AVPlayer(playerItem: movieToPlay)
    thePlayer!.play()
}

func videoDone(){
    dismiss(animated: true, completion: nil)
}
}
4

1 回答 1

0

我对一些事情感到困惑。这是有效的:

import Foundation
import UIKit
import AVKit

class videoPlayer: UIViewController {

    var movieURL:String = ""
    var movieTitle:String = ""
    var playerController:AVPlayerViewController?

    @IBOutlet weak var videoInfo: UIView!
    @IBOutlet var titleLabel: UILabel!


    override func viewDidLoad(){
        titleLabel.text = movieTitle
        playerController = AVPlayerViewController()
        playerController?.player = AVPlayer()
        self.addChildViewController(playerController!)
        self.view.addSubview((playerController?.view)!)
        playerController?.didMove(toParentViewController: self)

        playerController?.view.frame = self.view.frame

        playerController?.contentOverlayView?.addSubview(videoInfo)

        let movieToPlay = AVPlayerItem(url: URL(string: movieURL)!)
        playerController?.player = AVPlayer(playerItem: movieToPlay)
        playerController?.player?.play()

        NotificationCenter.default.addObserver(self, selector: #selector(videoDone), name: NSNotification.Name(rawValue: "AVPlayerItemDidPlayToEndTimeNotification"), object: playerController?.player?.currentItem)
            }


    func videoDone(){
        print("DONE")
        dismiss(animated: true, completion: nil)
    }

}

UIView 按预期显示。

于 2017-07-29T19:20:28.300 回答