2

I need to get current sound volume under my IOS5 application. The app is supposed to be used in cinemas so I want to notify the user that he/she should turn the volume down unless it is already turned down.

Parag Bafna
  • 22,812
  • 8
  • 71
  • 144
mgamer
  • 13,580
  • 25
  • 87
  • 145

2 Answers2

1

try this...

musicPlayer = [[MPMusicPlayerController iPodMusicPlayer];

currentVolume = musicPlayer.volume;
Ankit Srivastava
  • 12,347
  • 11
  • 63
  • 115
  • This looks great but when the device is muted I still get the positive volume value. Looks as though muted state didn't affect volume value. Would it be possible to read also muted state under IOS5? – mgamer Nov 28 '11 at 09:33
  • try using applicationMusicPlayer instead of iPodMusicPlayer and then check.. otherwise jbat100's answer looks pretty decent to me. – Ankit Srivastava Nov 28 '11 at 09:40
  • are you creating your own music player object or something like that... can you give us some code. – Ankit Srivastava Nov 28 '11 at 09:42
  • I am creating a software that will display content relevant to a movie being displayed in cinema. The requirement is to warn the user having unmuted device that he should mute it. – mgamer Nov 28 '11 at 09:46
  • seems like this solution is no longer valid since iOS 7.0 :( _@property(nonatomic) float volume NS_DEPRECATED_IOS(3_0, 7_0);"_ – tommys Oct 17 '13 at 15:01
1

You can get the volume like this

-(Float32)audioVolume
{
    Float32 state;
    UInt32 propertySize = sizeof(CFStringRef);
    OSStatus n = AudioSessionGetProperty(kAudioSessionProperty_CurrentHardwareOutputVolume, &propertySize, &state);
    if( n )
    {
        // something didn't work...
    }
    return state;
}

You can get system volume updates like this

[[NSNotificationCenter defaultCenter] addObserver:self 
                                         selector:@selector(volumeChanged:) 
                                             name:@"AVSystemController_SystemVolumeDidChangeNotification" 
                                           object:nil];

You can work out if the phone is in silent mode if this returns an empty string (this will crash if in the simulator hence the compile time guards).

#ifndef TARGET_IPHONE_SIMULATOR
-(NSString*)audioRoute
{
    CFStringRef state;
    UInt32 propertySize = sizeof(CFStringRef);
    OSStatus n = AudioSessionGetProperty(kAudioSessionProperty_AudioRoute, &propertySize, &state);
    if( n )
    {
        // something didn't work...
    }
    NSString *result = (NSString*)state;
    [result autorelease];
    return result;
} 
#endif

Although apparently, this will not work in iOS 5. This post is related also.

Community
  • 1
  • 1
jbat100
  • 16,757
  • 4
  • 45
  • 70