Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select audio device output swift (like Waze)

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.

like image 482
GIJOW Avatar asked Nov 07 '17 19:11

GIJOW


1 Answers

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

like image 95
GIJOW Avatar answered Nov 12 '22 22:11

GIJOW