【问题标题】:AVSystemController_SystemVolumeDidChangeNotification on iPhone 5iPhone 5 上的 AVSystemController_SystemVolumeDidChangeNotification
【发布时间】:2012-12-10 11:07:18
【问题描述】:

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

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

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

【问题讨论】:

  • 你还有这个问题吗?我想使用AVSystemController_SystemVolumeDidChangeNotification,但我没有 iPhone 5 进行测试。
  • 是的,不幸的是我尝试在 iPhone5 上测试解决这个问题,但似乎很难做到:(
  • 您有没有找到其他检测音量按钮按下的好方法? (除了整个 Audio Session 监听器方法。)
  • 不,目前。我只是停用了 iPhone5 设备的功能

标签: ios iphone-5 avcapturesession


【解决方案1】:

使用这个:

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

【讨论】:

    【解决方案2】:

    抱歉,我无法让它正常工作。我确信 Apple 在 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");
        // ...
    }
    

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

    -肯

    【讨论】:

    • AudioSessionAddPropertyListener 在 iOS 7.0 中已被弃用,但我找不到替代品。
    猜你喜欢
    • 2012-11-29
    • 2013-01-13
    • 2017-12-16
    • 2012-10-17
    • 1970-01-01
    • 1970-01-01
    • 2015-02-11
    • 2014-11-05
    相关资源
    最近更新 更多