在 swift 的给定视图中,视频无法全屏播放。需要帮助修复它
Video doesn't play full screen in a given view in swift. Need help fixing it
我有一个在应用程序中播放视频的视图。
给出的约束是正确的,但在 iPhone 11、iPhone 11 pro 等设备中,视图在尾端留下了一些 space。
添加屏幕截图以供参考。
*需要帮助将视频显示修复为完整视图。
约束条件:
对齐尾随 - 安全区域
顶部对齐 - 安全区域
对齐前导 - 安全区域
高度等于 - 150
代码:
import UIKit
import AVFoundation
class VideoViewController: UIViewController {
@IBOutlet weak var videoView: UIView!
var player: AVQueuePlayer!
var playerItem: AVPlayerItem!
var playerLooper: AVPlayerLooper!
override func viewDidLoad() {
super.viewDidLoad()
videoView()
}
func videoViewForLogin() {
//video
guard let url = Bundle.main.url(forResource: "splashpanoslice", withExtension: "mp4") else {return}
let playerPath = AVPlayer(url: url)
player = AVQueuePlayer()
let playerLayer = AVPlayerLayer(player: player)
playerItem = AVPlayerItem(url: url)
let duration = Int64( ( (Float64(CMTimeGetSeconds(AVAsset(url: url).duration)) * 10.0) - 1) / 10.0 )
playerLayer.frame = videoView.bounds
playerLooper = AVPlayerLooper(player: player, templateItem: playerItem, timeRange: CMTimeRange(start: CMTime.zero, end: CMTimeMake(value: duration, timescale: 1)) )
playerLayer.videoGravity = .resizeAspectFill
videoView.layer.insertSublayer(playerLayer, at: 1)
player.play()
}
}
功能代码取自参考 -
由于您使用的是框架,我认为您应该在 viewDidLoad()
之后尝试移动布局代码。或许,viewDidLayoutSubviews()
,当视图大小确定,显示参数改变时(例如屏幕方向改变)也失效。
我有一个在应用程序中播放视频的视图。 给出的约束是正确的,但在 iPhone 11、iPhone 11 pro 等设备中,视图在尾端留下了一些 space。 添加屏幕截图以供参考。
约束条件: 对齐尾随 - 安全区域 顶部对齐 - 安全区域 对齐前导 - 安全区域 高度等于 - 150
代码:
import UIKit
import AVFoundation
class VideoViewController: UIViewController {
@IBOutlet weak var videoView: UIView!
var player: AVQueuePlayer!
var playerItem: AVPlayerItem!
var playerLooper: AVPlayerLooper!
override func viewDidLoad() {
super.viewDidLoad()
videoView()
}
func videoViewForLogin() {
//video
guard let url = Bundle.main.url(forResource: "splashpanoslice", withExtension: "mp4") else {return}
let playerPath = AVPlayer(url: url)
player = AVQueuePlayer()
let playerLayer = AVPlayerLayer(player: player)
playerItem = AVPlayerItem(url: url)
let duration = Int64( ( (Float64(CMTimeGetSeconds(AVAsset(url: url).duration)) * 10.0) - 1) / 10.0 )
playerLayer.frame = videoView.bounds
playerLooper = AVPlayerLooper(player: player, templateItem: playerItem, timeRange: CMTimeRange(start: CMTime.zero, end: CMTimeMake(value: duration, timescale: 1)) )
playerLayer.videoGravity = .resizeAspectFill
videoView.layer.insertSublayer(playerLayer, at: 1)
player.play()
}
}
功能代码取自参考 -
由于您使用的是框架,我认为您应该在 viewDidLoad()
之后尝试移动布局代码。或许,viewDidLayoutSubviews()
,当视图大小确定,显示参数改变时(例如屏幕方向改变)也失效。