iPhone 5上的AVSystemController_SystemVolumeDidChangeNotification

时间:2012-12-10 11:07:19

标签: ios iphone-5 avcapturesession

每次启动AVCaptureSession时,似乎都会触发iPhone 5上的AVSystemController_SystemVolumeDidChangeNotification事件。

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(volumeChanged:) name:@"AVSystemController_SystemVolumeDidChangeNotification" object:nil];

有谁知道如何解决这个问题?我正在使用这个观察者用音量按钮拍照(我知道它是一个私有API,但它与默认相机应用程序的功能相同,Apple通常会视而不见......),但仅在iPhone上5 每次相机启动时都会拍摄一张照片。

2 个答案:

答案 0 :(得分:5)

使用此:

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(volumeChanged:)
                                             name:@"AVSystemController_SystemVolumeDidChangeNotification"
                                           object:nil];

然后:

- (void)volumeChanged:(NSNotification*)notification
{
    if([[notification.userInfo objectForKey:@"AVSystemController_AudioVolumeChangeReasonNotificationParameter"] isEqualToString:@"ExplicitVolumeChange"])
    {
        float volume = [[[notification userInfo]
                         objectForKey:@"AVSystemController_AudioVolumeNotificationParameter"]
                        floatValue];
    }
}

答案 1 :(得分:0)

抱歉,我无法理智地工作。我确信苹果公司在iPhone 5上以这种方式实现了一些很好的理由,但这是一个令人讨厌的麻烦。

我发现它的唯一方法是不使用它,而是使用音频会话属性监听器方法:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    //...
    AudioSessionInitialize(nil, nil, nil, nil);
    AudioSessionSetActive(YES);

    AudioSessionAddPropertyListener(kAudioSessionProperty_CurrentHardwareOutputVolume, volumeListenerCallbackIPhone, (__bridge void *)(self));
    //...
}

然后在回调中:

- (void)volumeChanged:(NSNotification *)notification
{
    NSLog(@"volumeChanged");
    // ...
}

...然后根据上下文进一步过滤事件。

-Ken

相关问题