5

I know you cannot control device volume from within your application, but I would like the device volume to be able to affect the UIScrollBar I have in my application to control volume.

I know this is possible because the Last.fm application does it, I would like to implement this behaviour.

I can find very little information on the interwebs. Anyone here can help me maybe? :)

adam
  • 22,404
  • 20
  • 87
  • 119

1 Answers1

15

It's easy with a listener callback

void audioVolumeChangeListenerCallback (void *inUserData, AudioSessionPropertyID inID, UInt32 inDataSize, const void *inData)
{
    RootViewController *controller = (RootViewController *) inUserData;
    Float32 newGain = *(Float32 *)inData;
    [controller setGainManual:newGain]; 
}

which gets initialized in my view controller's viewDidLoad like this

AudioSessionAddPropertyListener (kAudioSessionProperty_CurrentHardwareOutputVolume ,audioVolumeChangeListenerCallback, self );

This is all SDK/App Store friendly too.

Dinesh Raja
  • 8,501
  • 5
  • 42
  • 81
John Fricker
  • 3,304
  • 21
  • 21
  • Doing it in viewDidLoad is risky as your view can unload if you get a memory warning, and you will sign up for the callback twice. – Maciej Swic Jul 27 '11 at 19:34
  • i want this to work in background as well. Is there method by which i can achieve this. – Swati Aug 01 '13 at 05:23