fni*_*isi 6 uiview ios avplayer swift xcode9.2
我试图在UIView中播放视频,但视频播放器总是出现在我嵌入AVPlayerViewController的UIView之外.
以下是我到目前为止所拥有的......
class ViewController: UIViewController {
let smallVideoPlayerViewController = AVPlayerViewController()
@IBOutlet weak var videoView: UIView!
override func viewDidLoad() {
super.viewDidLoad()
let myFileManager = FileManager.default
let mainBundle = Bundle.main
let resourcesPath = mainBundle.resourcePath!
guard let allItemsInTheBundle = try? myFileManager.contentsOfDirectory(atPath: resourcesPath) else {
return
}
let videoName = "test"
let videoPath = Bundle.main.path(forResource: videoName, ofType: "mp4")
let videoUrl = URL(fileURLWithPath: videoPath!)
smallVideoPlayerViewController.showsPlaybackControls = false
smallVideoPlayerViewController.player = AVPlayer(url: videoUrl)
videoView.addSubview(smallVideoPlayerViewController.view)
smallVideoPlayerViewController.view.frame = videoView.frame
smallVideoPlayerViewController.player?.play()
}
...
}
Run Code Online (Sandbox Code Playgroud)
UIView的背景颜色设置为白色.从屏幕截图中可以看出,AVPlayer不在UIView中.
我尝试手动设置AVPlayer的尺寸和位置,但也没有运气.
我正在使用Xcode 9.2.该项目没有任何布局问题的警告.
如何完美地对齐AVPlayer,这样就会出现在UIView中.
谢谢
Uph*_*uth 14
改变这一行:
smallVideoPlayerViewController.view.frame = videoView.frame
Run Code Online (Sandbox Code Playgroud)
对此:
smallVideoPlayerViewController.view.frame = videoView.bounds
Run Code Online (Sandbox Code Playgroud)
原因是因为videoView.frame在其超级视图中包含了您不想要的原点.videoView.bounds只是原点为0,0的大小.
当然你可能想进一步设置自动调整大小掩码,以保持视频播放器框架像这样:
smallVideoPlayerViewController.view.autoresizingMask = [.flexibleWidth, .flexibleHeight]
Run Code Online (Sandbox Code Playgroud)
甚至是全面的自动布局.
| 归档时间: |
|
| 查看次数: |
4084 次 |
| 最近记录: |