0

I'm creating a music app and I'm using an UISlider for the music volume control. the problem is when I press the volume button on the iPhone / iPad UISlider does not change or move. I've been looking for where to get no referrals.

This is the code I use:

override func viewDidLoad() {
        super.viewDidLoad()
        
        DispatchQueue.main.async {
            self.slider.setValue(AVAudioSession.sharedInstance().outputVolume, animated: true)
        }
}

@IBAction func volumeControlSlider(_ sender: UISlider) {
        slider = (MPVolumeView().subviews.filter { NSStringFromClass($0.classForCoder) == "MPVolumeSlider" }.first as! UISlider)
        slider.setValue(sender.value, animated: false)
    }
halfer
  • 19,824
  • 17
  • 99
  • 186
Syahrir
  • 109
  • 3
  • 10
  • Don't use a `UISlider`, use an `MPVolumeView`. You can turn off the icon used to change the output device. – Gary Makin Dec 12 '17 at 04:44
  • [Stack overflow Link](https://stackoverflow.com/questions/28193626/cleanest-way-of-capturing-volume-up-down-button-press-on-ios-8) Please refer this link. – Kandhal Bhutiya Dec 12 '17 at 05:48
  • I've used MPVolumeView but it still does not change if pressed volume up / down button – Syahrir Dec 12 '17 at 07:17
  • func setupVolumeSlider() { volumeParentView.backgroundColor = UIColor.clear let volumeView = MPVolumeView(frame: volumeParentView.bounds) for view in volumeView.subviews { let uiview: UIView = view as UIView if (uiview.description as NSString).range(of: "MPVolumeSlider").location != NSNotFound { mpSlider = (uiview as! UISlider) } } } @IBAction func volumeChanged(_ sender: UISlider) { mpSlider.value = sender.value } – Syahrir Dec 12 '17 at 07:18
  • In that code you are not adding the `MPVolumeView` you create to the view hierarchy, so it is not going to be on the screen. – Gary Makin Dec 12 '17 at 22:13
  • Forgive me. I do not understand what you mean ... can give a simple code example – Syahrir Dec 18 '17 at 11:00

0 Answers0