Forum Archive

Getting/setting/observing sound volume?

shinyformica

Anyone have a good way to do these things? From a cursory search of the forum and internet, I'm a little surprised to learn that it appears to be pretty hard to set and observe the volume value.

I have a decent and reliable way to get the system volume:

import objc_util
AVAudioSession = objc_util.ObjCClass("AVAudioSession")
audioSession = AVAudioSession.sharedInstance()
audioSession.setActive_error_(True, None)
volume = audioSession.outputVolume()

and this, apparently, is the crazy way I found to set the volume:

import objc_util
MPVolumeView = objc_util.ObjCClass('MPVolumeView')

value = 0.5
volume_view = MPVolumeView.new().autorelease()
for subview in volume_view.subviews():
    if subview.isKindOfClass_(ObjCClass('UISlider')):
        subview.value = value
        break

does anyone have a better way?
And does anyone know a way to observe changes in the volume level? I have a need to implement a view which shows the current system volume...and was hoping not to poll via update().

JonB

The sound module has

sound.set_volume(vol)

Though that is just pythonista sound, I think, not system music player sound... You already found this and/or this

See also

https://stackoverflow.com/questions/3651252/how-to-get-audio-volume-level-and-volume-changed-notifications-on-ios

Nshipste has a good tutorial on key value observers,. Which can be used to observe many properties that don't expose notificancenter notifications

shinyformica

Thanks, I'll try the key/value observer method.
Gotta say it's awfully weird that Apple provided no reasonable API for this...at least for observing the value, if not setting it.