By using the private framework, Celestial, we can simply get and set the volume or monitor its change. The class used is AVSystemController in Celestial framework.
We can get or set the audio of either the active audio category or a specific audio category.
1) Active audio category:
float volume; [[AVSystemController sharedAVSystemController] getActiveCategoryVolume:&volume andName:nil];
[[AVSystemController sharedAVSystemController] setActiveCategoryVolumeTo:1.0];
(Optional) Also get the name of the active audio category:
float volume; NSString *categoryName; [[AVSystemController sharedAVSystemController] getActiveCategoryVolume:&volume andName:&categoryName];
2) Specific audio category:
float volume; [[AVSystemController sharedAVSystemController] getVolume:&volume forCategory:@"CATEGORY_HERE"];
[[AVSystemController sharedAVSystemController] setVolumeTo:1.0 forCategory:@"CATEGORY_HERE"];
Furthermore, we can also monitor the change of device volume with the following code.
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(volumeChanged:) name:@"AVSystemController_SystemVolumeDidChangeNotification" object:nil]; - (void)volumeChanged:(NSNotification *)notification { float volume = [[[notification userInfo] objectForKey:@"AVSystemController_AudioVolumeNotificationParameter"] floatValue]; }
Reference: List of available audio categories
This works… Any thoughts on how mute the phone completely… With 1.0 for sound level you can still here the ring and 0.0 doesn’t work….