6

I'm digging all over the web trying to find some clue to achieve this but no success.

I'm trying to show up the user a popup like Waze does to allow the user to select the audio output device, volume etc.

It seems to be some API as it shows exactly as sound widget on iOS11.

enter image description here

Any help appreciated.

GIJOW
  • 2,307
  • 1
  • 17
  • 37

3 Answers3

2

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

GIJOW
  • 2,307
  • 1
  • 17
  • 37
0

Another answer is AVRoutePickerView. See documentation here for more info. That also matches with the screenshot in your question.

bubbaspike
  • 101
  • 6
0

SWIFT5

Using AVRoutePickerView for iOS13>

import UIKit
import AVFoundation
import AVKit

let view = UIView(frame: UIScreen.main.bounds)

class ViewController: UIViewController {
    
    var AirPlayButton : AVRoutePickerView!
    
    override func loadView() {
        
        let view = UIView(frame: UIScreen.main.bounds)
        view.backgroundColor = .white
        
        AirPlayButton = AVRoutePickerView(frame: CGRect(x: 50, y: 50, width: 100, height: 100))
        view.addSubview(AirPlayButton)
        
        self.view = view
        
        let AirPlayButton = AVRoutePickerView(frame: CGRect(x: 50, y: 50, width: 100, height: 100))
        view.addSubview(AirPlayButton)
    }
    
}
DeDe Schurk
  • 113
  • 11