我想显示对vlc播放器的一些自定义控件,但是当我试图这样做时,当实际的视频开始在player中运行时,我的控件就会隐藏。
player = VLCMediaPlayer()
player.media = VLCMedia(url: URL(string: "rtmp://сс.tv/sea")!)
player.drawable = view
let abc = uiview()
view.addsubview(abc)我曾试图在我的vlc播放器上显示uiview,但没有做到。
player = VLCMediaPlayer()
player.media = VLCMedia(url: URL(string: "rtmp://сс.tv/sea")!)
player.drawable = view我需要显示一些自定义视图,每当我的视频播放时,它们应该是可见的,任何帮助都是值得注意的。
发布于 2022-10-24 08:10:47
要添加带有自定义控件的覆盖,您必须创建一个单独的UIView来保存播放机,并创建一个控件UIView。第一个应该是这样的,有你自己的框架:
let root = UIView()
root.frame.size.height = UIScreen.main.bounds.width
root.frame.size.width = UIScreen.main.bounds.height现在,我们可以将"root“设置为播放机的主要视频呈现视图:
mediaPlayer.drawable = root创建自定义控件视图,该视图将显示在播放机顶部:
var child = UIHostingController(rootView: ControlsVLC())
child.view.frame.size.height = UIScreen.main.bounds.width
child.view.frame.size.width = UIScreen.main.bounds.height
child.view.backgroundColor = .clear // You will need to add this so you can see the video behind the controls
child.view.isOpaque = false最后,您必须相应地将它们添加为子视图,以便控件将显示在视频的顶部:
self.addSubview(root)
self.addSubview(child.view)
self.bringSubviewToFront(child.view)
self.sendSubviewToBack(root) // Not sure if necessary but works请注意,这是用SwiftUI在iOS 16上完成的。
最后的代码应该如下所示:
class PlayerUIView: UIView, VLCMediaPlayerDelegate {
var mediaPlayer = VLCMediaPlayer()
override init(frame: CGRect) {
super.init(frame: frame)
let root = UIView()
root.frame.size.height = UIScreen.main.bounds.width
root.frame.size.width = UIScreen.main.bounds.height
let url = URL(string: "your.source")!//replace your resource here
mediaPlayer.media = VLCMedia(url: url)
mediaPlayer.delegate = self
mediaPlayer.drawable = root
mediaPlayer.play()
var child = UIHostingController(rootView: ControlsVLC())
child.view.frame.size.height = UIScreen.main.bounds.width
child.view.frame.size.width = UIScreen.main.bounds.height
child.view.backgroundColor = .clear
child.view.isOpaque = false
self.addSubview(root)
self.addSubview(child.view)
self.bringSubviewToFront(child.view)
self.sendSubviewToBack(root)
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func layoutSubviews() {
super.layoutSubviews()
}
}这对我有用。
https://stackoverflow.com/questions/58398678
复制相似问题