Search code examples
swiftvolumeairplayspeakermpvolumeview

How can i set MPVolumeView with swift?


i want to set Iphone Speaker default. My volume control swift code is below. It is works fine but when i tapped airplay button i see automatically select the second.(Iphone)

var wrapperView = UIView(frame: CGRectMake(10,40,(self.view.bounds.size.width)-20, 20))           
        wrapperView.backgroundColor=UIColor.purpleColor()         
        self.view.addSubview(wrapperView)       
        var volumeView = MPVolumeView(frame: wrapperView.bounds)
        volumeView.tintColor=UIColor.purpleColor()  
        wrapperView.addSubview(volumeView)

airplay button options


Solution

  • Don't know if you've solved this... Just set the default to be to the speaker (add any others you want as well, eg Bluetooth, headphones...)

        do {
            try AVAudioSession.sharedInstance().setCategory(
                AVAudioSessionCategoryPlayAndRecord,
                withOptions: [.DefaultToSpeaker])
            success = true
        } catch _ {
        }