我有一个表格视图,并且在其中一个单元格中有以下代码:
import UIKit
import Foundation
import WebKit
import AVFoundation
class OnlineVideoTableViewCell: UITableViewCell {
internal var videoPlayerSuperView = createEmptyView()
required init(coder aDecoder: NSCoder) {
fatalError("init(coder:)")
}
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
setUpUI()
}
convenience init(style: UITableViewCell.CellStyle, reuseIdentifier: String?, url: String = "", title: String) {
self.init(style: style, reuseIdentifier: reuseIdentifier)
self.videoTitleLabel.text = title
setUpUI()
setUpLayout()
}
func setUpUI() {
self.backgroundColor = .white
self.translatesAutoresizingMaskIntoConstraints = false
self.contentView.addSubview(videoPlayerSuperView)
videoPlayerSuperView.translatesAutoresizingMaskIntoConstraints = false
videoPlayerSuperView.backgroundColor = .smalt
}
func setUpLayout() {
videoPlayerSuperView.leftAnchor.constraint(equalTo: contentView.leftAnchor, constant: 2).isActive = true
videoPlayerSuperView.rightAnchor.constraint(equalTo: contentView.rightAnchor, constant: -2).isActive = true
videoPlayerSuperView.bottomAnchor.constraint(equalTo: contentView.bottomAnchor, constant: -2 ).isActive = true
videoPlayerSuperView.topAnchor.constraint(equalTo: contentView.topAnchor, constant: 2 ).isActive = true
}
override func layoutSubviews() {
print("Playing ---->")
self.avPlayer = AVPlayer.init(playerItem: AVPlayerItem(url: URL(string: "https://www.youtube.com/embed/0oBx7Jg4m-o")!))
avPlayerLayer = AVPlayerLayer(player: avPlayer)
avPlayer!.volume = 3
avPlayer!.actionAtItemEnd = .none
avPlayerLayer!.bounds = self.bounds
self.videoPlayerSuperView.layer.insertSublayer(avPlayerLayer!, at: 9)
self.avPlayer!.play()
}
}
我已经简化了该单元格,因为它还具有一个标签,标题中有一些次要细节(所有这些都很好用)。
问题是我的代码没有执行任何操作。我可以看到该单元格,可以看到一个大的正方形,上面有我自定义的“ smalt”颜色,应该在其中播放视频,但是没有音频或视频被复制。
除此之外,除了播放问题之外,其他一切似乎都正常运行。
我在做什么错了?