【问题标题】:How to Add an AirPlay button to an app in Swift 5如何在 Swift 5 中将 AirPlay 按钮添加到应用程序
【发布时间】:2021-06-03 11:11:26
【问题描述】:

我想实现一个 Airplay 按钮,该按钮显示蓝牙、iPhone 选项,就像我们在 iPhone 音乐应用中单击 Airplay 一样。我做了一些代码,但我遇到了一些问题。

  1. 在我实施时,MPVolumeView 工作正常,但无法更改按钮颜色。我检查了色调,但没有工作
  2. 添加了 AVRoutePickerView 的代码它不起作用它没有触发所以它不显示那个 AirPlay Popup 视图
  3. 我想在单击 UIButton 时显示此 Airplay 弹出视图,但我找不到任何解决方案,因此我使用 UIView。

有一些链接适用于某些点,但不适用于整个事情

AirPlay button on custom view

https://developer.jwplayer.com/jwplayer/docs/ios-add-an-airplay-button-to-an-app

iOS - How can I display an 'AirPlay' popup menu in Swift?

请查看以下屏幕截图以获取更多信息。

请检查代码

import UIKit
import MediaPlayer
import AVKit

class TempDemoViewController: UIViewController {
    @IBOutlet weak var airPlayView: UIView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setupAirPlayButton()
    }
    
    func setupAirPlayButton() {
        var buttonView: UIView = UIView()
        let buttonFrame = CGRect(x: 0, y: 0, width: 50, height: 50)
                
        if #available(iOS 11, *) {
            let routerPickerView = AVRoutePickerView(frame: buttonFrame)
            buttonView.addSubview(routerPickerView)
            
            routerPickerView.tintColor = UIColor(named: "PrimaryColor")
            routerPickerView.activeTintColor = .white
            routerPickerView.prioritizesVideoDevices = true
        }else{
            let airplayButton = MPVolumeView(frame: buttonFrame)
            airplayButton.showsVolumeSlider = false
            buttonView = airplayButton
        }
        airPlayView.addSubview(buttonView)
    }
    
}

【问题讨论】:

    标签: ios swift avaudioplayer swift5 airplay


    【解决方案1】:
    let buttonView  = UIView(frame: CGRect(x: 0, y: 0, width: 50, height: 50))
            
            let routerPickerView =  AVRoutePickerView(frame: buttonView.bounds)
            
            routerPickerView.tintColor = UIColor.green
            routerPickerView.activeTintColor = .white
            buttonView.addSubview(routerPickerView)
            self.airPlayView.addSubview(buttonView)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-10-04
      • 2021-01-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多