问题描述
当您测试代码时,您会看到 layoutSubviews 代码块失败。我该如何解决这个问题?
我不认为这是视频播放问题。
此代码在移动设备上运行良好,但不适用于 macOS 应用。
您可以通过复制粘贴代码自行测试。
视频播放器代码:
import AVFoundation
struct LLVideoPlayerProvider: NSViewRepresentable {
@Binding var url: URL
@Binding var playerStatus: PlayerStatus
func makeNSView(context: Context) -> some NSView {
return LLVideoUIView(playerStatus: playerStatus,url: url,frame: .zero)
}
func updateNSView(_ nsView: NSViewType,context: Context) {
}
}
class LLVideoUIView: NSView {
private var playerLayer = AVPlayerLayer()
var url: URL
var playerStatus: PlayerStatus
init(playerStatus: PlayerStatus,url: URL,frame: CGRect) {
self.url = url
self.playerStatus = playerStatus
super.init(frame: frame)
providerLLPlayer(playerLayer: playerLayer,videoURL: url,playerStatus: playerStatus)
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func providerLLPlayer(playerLayer: AVPlayerLayer,videoURL: URL,playerStatus: PlayerStatus) {
let videoURL = videoURL
let playerItem = AVPlayerItem(url: videoURL)
let player = AVPlayer(playerItem: playerItem)
playerLayer.player = player
playerLayer.videoGravity = .resizeAspectFill
layer?.addSublayer(playerLayer)
player.actionAtItemEnd = .none
NotificationCenter.default.addobserver(self,selector: #selector(rewindVideo(notification:)),name: .AVPlayerItemDidplayToEndTime,object: player.currentItem)
switch playerStatus {
case .start:
return player.play()
case .end:
return player.pause()
}
}
@objc
func rewindVideo(notification: Notification) {
playerLayer.player?.seek(to: .zero)
}
override func layoutSubviews() {
super.frame = bounds
playerLayer.frame = bounds
}
}
enum PlayerStatus {
case start
case end
}
使用视频播放器
LLVideoPlayerProvider(url: .constant(URL(fileURLWithPath: Bundle.main.path(forResource: "Test",ofType: "mp4")!)),playerStatus: .constant(.start))
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)