AVAudioSession OutputVolume never changes

There are many questions and answers on SO that say that [AVAudioSession sharedInstance].outputVolume is the only way to determine the volume of a device. But it does not seem to work correctly. outputVolume never changes, although this is correct when it is first installed (when the application starts).

Am I doing it wrong? I do not know what else to do by reading the value of outputVolume . My instincts would tell me that it was a mistake if it were not for the fact that other people seem to be doing it just fine. I also tested it on iOS 7 and 8, so this is not an iOS 8 bug. I reproduced the same thing in a small test project, so that nothing in my project bothers it.

Also: I know well the difference between ringer volume and sound volume. I changed both options, and the message volume still has not changed.

Here is the function that 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]; }); } 
+6
source share
3 answers

Use KVO and remember to activate sharedInstance :

 [[AVAudioSession sharedInstance] setActive:YES error:nil]; [[AVAudioSession sharedInstance] addObserver:self forKeyPath:@"outputVolume" options:NSKeyValueObservingOptionNew context:nil]; 
+19
source

I checked in iOS 12:

 [[AVAudioSession sharedInstance] setActive:YES error:nil]; float vol = [[AVAudioSession sharedInstance] outputVolume]; NSLog(@"outputVolume: %f", vol); 
0
source

I find that I need to actually output the sound so that [AVAudioSession sharedInstance] .outputVolume is accurate.

In other words - [AVAudioSession sharedInstance] .outputVolume - this is a bit strange - you need to play the sound so that it returns the correct volume.

-2
source

Source: https://habr.com/ru/post/972604/


All Articles