Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
878 views
in Technique[技术] by (71.8m points)

ios - AVAudioSession's OutputVolume never changes

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];
    });
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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];

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...