Jihwan Kim
Jihwan Kim

Reputation: 23

iOS Volume Control via Bluetooth

I'm fairly new to iOS dev, and I am currently developing an app that is to be controlled by a Bluetooth peripheral. If a peripheral sends a signal, the phone needs to increment its system volume. I am only finding examples with sliders. Is there any way to increase system volume? Help will be appreciated. Thank you.

Upvotes: 0

Views: 445

Answers (2)

Sunil Kumar
Sunil Kumar

Reputation: 91

var currentVolume: Float = 0.0
let mpVolumeView = MPVolumeView()
func volumeSliderControl(_ volume: Float) {
    let volumeSlider = (mpVolumeView.subviews.filter { NSStringFromClass($0.classForCoder) == "MPVolumeSlider" }.first as! UISlider)
    currentVolume = currentVolume + volume
    currentVolume = currentVolume > 1.0 ? 1.0 : currentVolume < 0.0 ? 0.0 : currentVolume
    debugLog("currentVolume: \(currentVolume)")
    volumeSlider.setValue(currentVolume, animated: false)
    volumeSlider.sendActions(for: .valueChanged)
}

For above function, just pass volume range from 0 to 1 in Float This code to control music/media volume only.

Upvotes: 0

Rob Napier
Rob Napier

Reputation: 299345

There is no supported way to modify the system volume. This has been intentionally removed (there used to be supported ways).

There is a deprecated way (since iOS 7) that currently works, but may go away in any future release. You can use [[MPMusicPlayerController systemMusicPlayer] setVolume:] to set the master volume.

The new supported way to change the system volume is to open an MPVolumeView and let the user modify the volume. But there's no programmatic interface. You can hunt around in the view for the sliders and adjust them through, though. This is really fragile; sometimes the structure changes between OS versions, and Apple may move this view into another process in the future (as they've done with several other views that they don't want you messing with). I do not recommend this approach and am not going to write up code to do it (since the deprecated setVolume: still works). But it is possible.

Again, this is all unsupported stuff, and Apple may reject your app over it.

Upvotes: 1

Related Questions