There are plenty of questions and answers on SO that say [AVAudioSession sharedInstance].outputVolume
is the only way to detect a device's volume. But it doesn't seem to work quite right. outputVolume
never changes, though it is correct when it is first set (at application launch).
Am I doing it wrong? I don't know what else to do besides reading the value of outputVolume
. My instincts would tell me this was a bug, if it wasn't for the fact that other people seem to be doing it just fine. I also tested it on iOS 7 and 8, so it's not an iOS 8 bug. I reproduced the same thing in a small test project, so nothing in my project is interfering with it.
Also: I am well aware of the difference between ringer-volume and sound-volume. I changed both, and the reported volume still did not change.
Here's the function I used in my test project:
- (void)checkVolume
{
float volume = [AVAudioSession sharedInstance].outputVolume;
NSLog(@"Volume: %f", volume);
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^
{
[self checkVolume];
});
}
Use KVO, and make sure to activate the sharedInstance
:
[[AVAudioSession sharedInstance] setActive:YES error:nil];
[[AVAudioSession sharedInstance] addObserver:self forKeyPath:@"outputVolume" options:NSKeyValueObservingOptionNew context:nil];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With