AVAudioSession的OutputVolume永远不会改变 [英] AVAudioSession's OutputVolume never changes
问题描述
关于SO的很多问题和答案都说[AVAudioSession sharedInstance].outputVolume
是检测设备音量的唯一方法.但这似乎不太正确. outputVolume
永远不会更改,尽管第一次设置时是正确的(在应用程序启动时).
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).
我做错了吗?除了读取outputVolume
的值外,我不知道还能做什么.我的直觉会告诉我这是一个错误,如果不是因为其他人似乎做得很好.我也在iOS 7和8上进行了测试,因此它不是iOS 8的错误.我在一个小型测试项目中复制了相同的内容,因此项目中没有任何干扰.
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];
});
}
推荐答案
使用KVO,并确保激活sharedInstance
:
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];
这篇关于AVAudioSession的OutputVolume永远不会改变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!