I use MPVolumeView to generate a horizontal slider which allows me to manage the volume of the audio being played. It works great. However, at initialization, I want the slider takes the position corresponding to volume of the device and not 0.5, the default:
Here is the code I have done:
func volumeSlider() {
let parentVolumeView = MPVolumeView(frame: volumeParentView.bounds)
for view in parentVolumeView.subviews {
if (view as UIView).description.rangeOfString("MPVolumeSlider") != nil {
mpVolumeSlider = view as! UISlider
print("mpVolumeSlider.value : ", mpVolumeSlider.value)
// Tried to set mpVolumeSlider.value to volume device, but didn't work, show : 0.0
// mpVolumeSlider.value = AVAudioSession.sharedInstance().outputVolume
}
// Remove device volume indicator
self.view!.addSubview(parentVolumeView)
}
let thumbImage = UIImage(named: "slider-image")
slider?.setThumbImage(thumbImage, forState: .Normal)
}
Assign your slider's value
to to AVAudioSession.sharedInstance().outputVolume
on init
or in viewDidLoad
of your view controller. Remember to import AVFoundation
to use AVAudioSession
.
Alternatively, you can programmatically initialize your MPVolumeView
instead of having it as an outlet. I believe that is the source of your issue.