我有1个AVPlayerViewController和AVPlayer,我向其中添加了视频,并且可以正常工作。但是,当开始加载视频时,会出现黑屏约0.5秒钟的问题,然后新视频才能开始。我已经将AVPlayerViewController添加到UIView。我已经用视频的颜色查看了UIView介质的背景,但是每次加载时它都会显示黑屏,然后它才能启动视频。有人能帮我吗。这是我的代码
class WalkViewController: UIViewController{
@IBOutlet weak var view_player: UIView!
var avPlayer: AVPlayer!
let avPlayerController = AVPlayerViewController()
var sourc_video : String!
var filepath: String!
var fileURL: NSURL!
override func viewDidLoad() {
//video view payer
self.filepath = NSBundle.mainBundle().pathForResource(arr_video[0], ofType: "mp4")
self.fileURL = NSURL.init(fileURLWithPath:filepath!)
self.avPlayer = AVPlayer(URL: fileURL)
self.avPlayerController.player = avPlayer
self.avPlayerController.view.frame = CGRect(x: 0, y: 0, width: 316, height: 316)
self.avPlayerController.showsPlaybackControls = false
self.avPlayerController.player?.play()
avPlayerController.prepareForInterfaceBuilder()
self.addChildViewController(avPlayerController)
self.view_player.addSubview(avPlayerController.view)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
答案 0 :(得分:2)
首先,您的代码是错误的。将子视图控制器的视图放入界面后,必须调用didMoveToParentViewController
。
第二,视频需要时间来准备。将视图放入界面,但将其隐藏。使用KVO观察isReadyForDisplay
。当它变为true时,现在显示视图。