I have to change the volume on iPad and using this code:
[[MPMusicPlayerController applicationMusicPlayer] setVolume:0];
But this changing volume and showing system volume bar on iPad. How to change the sound without showing the volume bar?
I know, setVolume:
is deprecated, and everybody says to use MPVolumeView
. If this is the only way to solve my problem, then how to change the volume using MPVolumeView
? I don't see any method in MPVolumeView
that changes the sound.
Should I use some another class together with MPVolumeView
?
But it's preferable to use MPMusicPlayerController
.
Thank you for advice!
For 2018, working on iOS 11.4
You need to change
slider.value
after a small delay.Usage:
Objective-C version
Version: Swift 3 & Xcode 8.1
Here's a solution in Swift. It might be a shady one, so I'll let you know if Apple approved this when I publish. Meantime, this works just fine for me:
Define an MPVolumeView and an optional UISlider in your View Controller
In the storyboard, define a view that's hidden from the user (height=0 should do the trick), and set an outlet for it (we'll call it hiddenView here). This step is only good if you want NOT to display the volume HUD when changing the volume (see note below):
In viewDidLoad() or somewhere init-y that runs once, catch the UISlider that actually controls the volume into the optional UISlider from step (1):
When you want to set the volume in your code, just set volumeSlider?.value to be anywhere between 0.0 and 1.0, e.g. for increasing the volume:
Important note: This solution will prevent the iPhone's Volume HUD from appearing - either when you change the volume in your code, or when the user clicks the external volume buttons. If you do want to display the HUD, then skip all the hidden view stuff, and don't add the MPVolumeView as a subview at all. This will cause iOS to display the HUD when the volume changes.
@udjat 's answer in Swift 3
Usage:
Swift > 2.2, iOS > 8.0,
I didn't find any solution I was looking but I end up doing this as solution: