How to change device Volume on iOS - not music volume

后端 未结 9 701
旧时难觅i
旧时难觅i 2020-11-28 10:30

I want to change the device volume on iOS (iphone).

I know that i can change the volume of music library with this lines below:

//implement at firs         


        
9条回答
  •  自闭症患者
    2020-11-28 11:23

    Creating MPVolumeView on the fly to get the slider and set the volume doesn't work in iOS 11.4 anymore. I solved the issue by adding new MPVolumeView to my UIViewController view, otherwise it didn't set the volume. As I added it to the controller I also need to set the volume view position to be outside of the screen.

    The code is in Swift 4:

    let volumeControl = MPVolumeView(frame: CGRect(x: 0, y: 0, width: 120, height: 120))
    
    override func viewDidLoad() {
       self.view.addSubview(volumeControl);
    }
    
    override func viewDidLayoutSubviews() {
       volumeControl.frame = CGRect(x: -120, y: -120, width: 100, height: 100);
    }
    
    func setMaxVolume() {
        let lst = volumeControl.subviews.filter{NSStringFromClass($0.classForCoder) == "MPVolumeSlider"}
        let slider = lst.first as? UISlider
    
        slider?.setValue(1, animated: false)
    }
    

提交回复
热议问题