1

I want to implement an Airplay button that shows the Bluetooth, iPhone option like in the iPhone music app we click on Airplay. I did some of the code but I am facing some issues.

  1. As I implement, MPVolumeView works fine but unable to change the button colour. I checked with tint colour but not working
  2. Added code of AVRoutePickerView it's not working It does not trigger so It not display that AirPlay Popup view
  3. I want to show this Airplay Popup View when clicking on UIButton but I can't find any solution regards this so I take UIView for it.

There are some links SO that work for some of the points but not for the whole thing

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?

Please check the below Screenshot for more information.

enter image description here

Please check the code

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)
    }
    
}
Yogesh Patel
  • 1,893
  • 1
  • 20
  • 55

1 Answers1

6
override func viewDidLoad() {
     super.viewDidLoad()
     setAirplayButton()
  } 
    
 func setAirplayButton(){
    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)
  }

Note : airPlayView is UIView that i added in storyboard.

Jinal Gor
  • 116
  • 2
  • 4
  • Please add more detail on your answer so OP and other users can able to get it like how and where they need to write the above code and all. – CodeChanger Jan 03 '22 at 11:56