Question

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!

Was it helpful?

Solution

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];

OTHER TIPS

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
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top