Avplayer添加按钮
目前,我尝试在Avplayer中添加一个按钮。从文档中,我不清楚如何做到这一点。
这是我的意思:
但是我不清楚如何仅通过阅读文档来添加一个。它显然是一个Avplayer,而不是自定义控制器实现,因为香草播放器看起来几乎相同:
有没有知道我如何在不自己实现控制器UI的情况下进行存档?我只想添加一种调整视频质量的方法,而无需重新发明轮子。
我能够在那里放一个按钮,但是我无法调整大小:
它:
// uiViewController -> AVPlayerViewController
// self.child -> A SwiftUI view
let childViewController = SelfSizingHostingController(rootView: self.child)
childViewController.view.translatesAutoresizingMaskIntoConstraints = false
childViewController.view.contentMode = .scaleAspectFit
if let avMobileAuxiliaryControlsView = uiViewController.view.subviews.first?.subviews[1].subviews[4], let parentVC = avMobileAuxiliaryControlsView.parentViewController {
if !avMobileAuxiliaryControlsView.subviews.map({ String(describing: type(of: $0.self)) }).contains("AVControlOverflowButton") {
return
}
uiViewController.buttonIsAdded = true
// Remove all constraints
avMobileAuxiliaryControlsView.removeConstraints(avMobileAuxiliaryControlsView.constraints)
// Save subviews in dictionary for constraints(withVisualFormat:)
var viewDictionary: [String: UIView] = [:]
for view in avMobileAuxiliaryControlsView.subviews {
let className = String(describing: type(of: view.self))
viewDictionary[className] = view;
}
viewDictionary["QualitySelectorView"] = childViewController.view;
print("dict: \(viewDictionary.keys.joined(separator: ", "))")
// Add to view
parentVC.addChild(childViewController)
avMobileAuxiliaryControlsView.addSubview(childViewController.view)
childViewController.didMove(toParent: parentVC)
// Update constraints
avMobileAuxiliaryControlsView.addConstraints( NSLayoutConstraint.constraints(withVisualFormat: "H:|\(viewDictionary.keys.map({ "[\($0)]" }).joined(separator: "-"))", options: [], metrics: nil, views: viewDictionary))
}
At the moment I try to add a Button to an AVPlayer. From the documentation it isn't clear to me how you can do this.
Here is an example what I mean:
The apple developer app has this copy button. But it isn't clear to me how to add one by reading just the documentation. It is clearly an AVPlayer and not a custom controller implementation, because a vanilla player looks nearly the same:
Any idea how I can archive this without implementing a controller ui by myself? I just want to add a way to adjust video quality without reinventing the wheel.
I was able to place a button there, but I was not able to resize it:
This is roughly how I did it:
// uiViewController -> AVPlayerViewController
// self.child -> A SwiftUI view
let childViewController = SelfSizingHostingController(rootView: self.child)
childViewController.view.translatesAutoresizingMaskIntoConstraints = false
childViewController.view.contentMode = .scaleAspectFit
if let avMobileAuxiliaryControlsView = uiViewController.view.subviews.first?.subviews[1].subviews[4], let parentVC = avMobileAuxiliaryControlsView.parentViewController {
if !avMobileAuxiliaryControlsView.subviews.map({ String(describing: type(of: $0.self)) }).contains("AVControlOverflowButton") {
return
}
uiViewController.buttonIsAdded = true
// Remove all constraints
avMobileAuxiliaryControlsView.removeConstraints(avMobileAuxiliaryControlsView.constraints)
// Save subviews in dictionary for constraints(withVisualFormat:)
var viewDictionary: [String: UIView] = [:]
for view in avMobileAuxiliaryControlsView.subviews {
let className = String(describing: type(of: view.self))
viewDictionary[className] = view;
}
viewDictionary["QualitySelectorView"] = childViewController.view;
print("dict: \(viewDictionary.keys.joined(separator: ", "))")
// Add to view
parentVC.addChild(childViewController)
avMobileAuxiliaryControlsView.addSubview(childViewController.view)
childViewController.didMove(toParent: parentVC)
// Update constraints
avMobileAuxiliaryControlsView.addConstraints( NSLayoutConstraint.constraints(withVisualFormat: "H:|\(viewDictionary.keys.map({ "[\($0)]" }).joined(separator: "-"))", options: [], metrics: nil, views: viewDictionary))
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
上图上看到的是完整的自定义PlayerController。如果您想掌握
avplayerviewController
的当前实现,并具有某些功能,例如交互式解雇,双重敲击,演示模式等。但是,丢失了一些播放控件,则有一种实施的官方方法 Apple WWDC 2019 AVKIT演示从8min35sec开始。您需要做的事情:avplayerviewController
showplaybackcontrols
tofalse> false
contemoverlayview
以获取更多信息。 WWDC视频。
What is seen on the pictures above is complete custom playerController. If you want to take hold of current implementation of
AVPlayerViewController
with some features such as interactive dismissal, double-tap, presentation modes etc.. however loosing some of the playback controls, there is an official way for implementing Custom Playback Controls that was presented in Apple WWDC 2019 AVKit presentation starting at 8min35sec. What you need to do:AVPlayerViewController
showsPlaybackControls
tofalse
contentOverlayView
For more info check the WWDC video.