Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
877 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 Reply

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...