Avplayer添加按钮

发布于 2025-02-13 01:13:37 字数 2462 浏览 3 评论 0原文

目前,我尝试在Avplayer中添加一个按钮。从文档中,我不清楚如何做到这一点。

这是我的意思:

​但是我不清楚如何仅通过阅读文档来添加一个。它显然是一个Avplayer,而不是自定义控制器实现,因为香草播放器看起来几乎相同:

“

有没有知道我如何在不自己实现控制器UI的情况下进行存档?我只想添加一种调整视频质量的方法,而无需重新发明轮子。

我能够在那里放一个按钮,但是我无法调整大小:

”我的try“

它:

// 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:

Apple Developer App example

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:

My AVPlayerView

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:

My try

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

英雄似剑 2025-02-20 01:13:37

上图上看到的是完整的自定义PlayerController。如果您想掌握avplayerviewController的当前实现,并具有某些功能,例如交互式解雇,双重敲击,演示模式等。但是,丢失了一些播放控件,则有一种实施的官方方法 Apple WWDC 2019 AVKIT演示从8min35sec开始。您需要做的事情:

  1. 模态呈现avplayerviewController
  2. set showplaybackcontrols to false> false
  3. 将控件添加到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:

  1. modally present AVPlayerViewController
  2. set showsPlaybackControls to false
  3. add controls to contentOverlayView

For more info check the WWDC video.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文