我将n avplayer放在视图控制器中以自定义视图控制器的其他一些元素,但我仍然想要AVPlayerViewController中使用的播放和清理控件。当我不使用AvPlayerViewcontroller时,有没有办法为Avplayer启用这些控件?
答案 0 :(得分:25)
没有。通常的解决方案(Apple明确建议)是使用AVPlayerViewController作为嵌入式视图控制器 - 即,使您的视图控制器成为自定义父视图控制器,将AVPlayerViewController作为其子视图,现在您可以放置其视图(电影和控件) )在你的视图中作为一个良好的子视图。示例(来自我的书; self
是您的视图控制器):
let url = // whatever
let player = AVPlayer(URL:url)
let av = AVPlayerViewController()
av.player = player
av.view.frame = // whatever
self.addChildViewController(av)
self.view.addSubview(av.view)
av.didMoveToParentViewController(self)
答案 1 :(得分:1)
要使用Swift 5更新此帖子,您可以尝试以下代码:
let videoURL = URL(string: "https://your_video_in_internet.mp4")
let player = AVPlayer(url: videoUrl)
let playerLayer = AVPlayerLayer(player: player)
playerLayer.frame = self.view.bounds
self.view.layer.addSublayer(playerLayer)
player.play()
答案 2 :(得分:0)
let player = AVPlayer(url: url)
let playerViewController = AVPlayerViewController()
playerViewController.view.frame = self.videoPlayerView.bounds
playerViewController.player = player
self.addChild(playerViewController)
self.videoPlayerView.addSubview(playerViewController.view)
playerViewController.didMove(toParent: self)