Select audio device output swift (like Waze)
For those struggling on it like me, here a potential solution.
I don't know if there is a better solution. If so, please share.
I needed a custom button to make exactly the same MPVolumeView Route Button. So I could achieve the same effect and functionality masking the original button.
import UIKit
import MediaPlayer
class SelectAudioDeviceViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let avView = myView(frame: CGRect(x: 0, y: 0, width: 150, height: 50))
let button = UIButton(frame: CGRect(x: 50, y: 150, width: 150, height: 50))
avView.showsRouteButton = true
avView.showsVolumeSlider = false
avView.setRouteButtonImage(nil, for: .normal)
button.backgroundColor = UIColor.gray
button.setTitle("BLABLA", for: .normal)
button.addSubview(avView)
self.view.addSubview(button)
}
}
class myView: MPVolumeView {
override func routeButtonRect(forBounds bounds: CGRect) -> CGRect {
let newBounds = CGRect(x: 0, y: 0, width: 150, height: 50)
return newBounds
}
}
Hope it helps