0

I need the user to control the device's media volume. I've seen many solutions using volume view or AVAudioPlayer, but I want to use the device volume buttons to set the app volume, like many apps have.

Thanks!

toonice
  • 2,211
  • 1
  • 13
  • 20
douglasd3
  • 761
  • 2
  • 10
  • 27

2 Answers2

0

To use this functionality you need to set the audio session to playback. This is done with something like this:

AVAudioSession *session = [AVAudioSession sharedInstance];
[session setCategory:AVAudioSessionCategoryPlayback error:nil];
Manuel
  • 10,153
  • 5
  • 41
  • 60
  • Would you need to activate the session or does this suffice? – sooper Apr 16 '12 at 16:38
  • The actual in/decreasing of the sound level is done by the OS – Manuel Apr 16 '12 at 16:41
  • For example: My device volume is full, then i start my app and change the volume to the half. If i exit my app my device volume should be full, and if a enter my app again it should be by the half. This solution will do that? – douglasd3 Apr 16 '12 at 16:53
  • No to do that you will have to save the 'current' volume when the app starts and set it back to that volume when you exit. The volume that is set within the app is media volume, not the ringer volume. – Manuel Apr 16 '12 at 18:39
0

dumped in appdelegate

  - (BOOL)application:(UIApplication *)application
    didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
        [[NSNotificationCenter defaultCenter]
         addObserver:self
         selector:@selector(volumeChanged:)
         name:@"AVSystemController_SystemVolumeDidChangeNotification"
         object:nil];
    }

    - (void)volumeChanged:(NSNotification *)notification
    {
        float volume =
        [[[notification userInfo]
          objectForKey:@"AVSystemController_AudioVolumeNotificationParameter"]
         floatValue];

        // Do stuff with volume
    }
Binoy jose
  • 461
  • 4
  • 9