iphoneobjective-ciosvolumeavsystemcontroller

Detect iPhone Volume Button Up Press?


Is there a notification that I can listen to that will tell me when an iPhone's volume is turned up?

I know about the AVSystemController_SystemVolumeDidChangeNotification, but it is essential that the notification only be triggered when the volume has been turned up, not up or down.

Secondly, how can I hide the translucent view that appears when the volume up button is pressed, showing the system's volume? Camera+ has implemented this.


Solution

  • There is no documented way to to this, but you can use this workaround. Register for AVSystemController_SystemVolumeDidChangeNotification notification and add an MPVolumeView which will prevent the system volume view from showing up.

    MPVolumeView *volumeView = [[MPVolumeView alloc] initWithFrame:CGRectMake(-100, 0, 10, 0)];
    [volumeView sizeToFit];
    [self.view addSubview:volumeView];
    

    And don't forget to start an Audio Session

    AudioSessionInitialize(NULL, NULL, NULL, NULL);
    AudioSessionSetActive(true);
    

    In This case, the MPVolumeView is hidden from the user.

    As for checking if volume up or down was pressed, just grab the current application's volume

    float volumeLevel = [[MPMusicPlayerController applicationMusicPlayer] volume];  
    

    and compare it with new volume after the button was pressed in notification callback

    If you don't want to do it by yourself, there's a drop-in class available in github

    https://github.com/blladnar/RBVolumeButtons