一.AVPlayerViewController播放视频
分两种形式
1.初始化AVPlayerViewController
1
2
3
4
5
6
7
8
9
10
11
|
func loadPlayer() -> Void {
guard let movieUrl = Bundle.main.path(forResource: "zh-zhfanti-en", ofType: "m4v") else {
return
}
let player = AVPlayer(url: URL(fileURLWithPath: movieUrl))
playerViewController = AVPlayerViewController()
playerViewController!.player = player
}
|
2.内嵌视图的形式显示
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
@IBAction func handlePlayInViewEvent(sender: AnyObject) {
loadPlayer()
guard (playerViewController != nil) else {
return
}
containerView?.addSubview(playerViewController!.view)
playerViewController?.view.frame = containerView!.bounds
self.addChild(playerViewController!)
playerViewController?.player?.play()
}
|
效果图
点击AVPlayerViewController
可以全屏播放
3.模态视图的形式显示
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
@IBAction func handlePlayWithModelViewEvent(sender: AnyObject) {
loadPlayer()
guard (playerViewController != nil) else {
return
}
self.present(playerViewController!, animated: true) { [weak self] () -> Void in
guard let self = self else {
return
}
self.playerViewController?.player?.play()
}
}
|
效果图
二.停止视频播放
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
@IBAction func handleStopPlayEvent(sender: AnyObject? ){
guard playerViewController != nil else {
return
}
if ((playerViewController?.parent) != nil) {
playerViewController?.view.removeFromSuperview()
playerViewController?.removeFromParent()
} else if playerViewController != nil && playerViewController!.isModalInPresentation {
playerViewController?.dismiss(animated: (sender != nil ? true:false), completion: nil)
}
playerViewController?.player?.pause()
playerViewController = nil
}
|
文章作者
梵梵爸
上次更新
2019-12-25
许可协议
原创文章,如需转载请注明文章作者和出处。谢谢