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
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)
}