如何在 iOS 上获取音频音量级别和音量更改通知? [英] How to get audio volume level, and volume changed notifications on iOS?

查看:49
本文介绍了如何在 iOS 上获取音频音量级别和音量更改通知?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个非常简单的应用程序,它在按下按钮时播放声音.由于该按钮在设备设置为静音时没有多大意义,因此我想在设备的音量为零时禁用它.(然后在再次调高音量时重新启用它.)

I'm writing a very simple application that plays a sound when pressing a button. Since that button does not make a lot of sense when the device is set to silence I want to disable it when the device's audio volume is zero. (And subsequently reenable it when the volume is cranked up again.)

我正在寻找一种有效的(和 AppStore 安全的)方法来检测当前的音量设置,并在音量级别发生变化时收到通知/回调.我不想改变音量设置.

I am seeking a working (and AppStore safe) way to detect the current volume setting and get a notification/callback when the volume level changes. I do not want to alter the volume setting.

所有这些都在我的 ViewController 中实现,其中使用了所述按钮.我已经用运行 iOS 4.0.1 和 4.0.2 的 iPhone 4 以及运行 4.0.1 的 iPhone 3G 对此进行了测试.使用 iOS SDK 4.0.2 和 llvm 1.5 构建.(使用 gcc 或 llvm-gcc 不会改进任何东西.)在构建实施过程中没有任何问题,既没有错误也没有警告.静态分析器也很高兴.

All this is implemented in my ViewController where said button is used. I've tested this with an iPhone 4 running iOS 4.0.1 and 4.0.2 as well as an iPhone 3G running 4.0.1. Built with iOS SDK 4.0.2 with llvm 1.5. (Using gcc or llvm-gcc doesn't improve anything.) There are no issues during build implementing either way, neither errors nor warnings. Static analyzer is happy as well.

这是我迄今为止尝试过的方法,但都没有成功.

Here is what I've tried so far, all without any success.

按照 Apple 的音频服务文档,我应该为 kAudioSessionProperty_CurrentHardwareOutputVolume 注册一个 AudioSessionAddPropertyListener,它应该像这样工作:

Following Apple's audio services documentation I should register an AudioSessionAddPropertyListener for kAudioSessionProperty_CurrentHardwareOutputVolume which should work like this:

// Registering for Volume Change notifications
AudioSessionInitialize(NULL, NULL, NULL, NULL);
returnvalue = AudioSessionAddPropertyListener (

kAudioSessionProperty_CurrentHardwareOutputVolume ,
      audioVolumeChangeListenerCallback,
      self
);

returnvalue0,表示注册回调成功.

returnvalue is 0, which means that registering the callback worked.

遗憾的是,当我按下设备上的音量按钮、耳机答题器或拨动铃声静音开关时,我从未收到对函数 audioVolumeChangeListenerCallback 的回调.

Sadly, I never get a callback to my function audioVolumeChangeListenerCallback when I press the volume buttons on my device, the headset clicker or flip the ringer-silent switch.

当使用完全相同的代码注册 kAudioSessionProperty_AudioRouteChange(在 WWDC 视频、开发人员文档和互联网上的许多站点中用作类似的示例项目)时,我实际上 在更改音频路由时获得回调(通过插入/拔出耳机或对接设备).

When using the exact same code for registering for kAudioSessionProperty_AudioRouteChange (which is used as an analogous sample project in WWDC videos, Developer documentation and on numerous sites on the interwebs) I actually do get a callback when changing the audio route (by plugging in/out a headset or docking the device).

名为 Doug 的用户打开了一个名为 iPhone 音量已更改事件已达到最大音量 他声称他成功地使用了这种方式(除非音量实际上不会改变,因为它已经设置为最大值).不过,它对我不起作用.

A user named Doug opened a thread titled iPhone volume changed event for volume already max where he claimed that he is sucessfully using this way (unless the volume would not actually change because it is already set to maximum). Still, it doesn't work for me.

我尝试过的另一种方法是像这样在 NSNotificationCenter 注册.

Another way I have tried is to register at NSNotificationCenter like this.

// sharedAVSystemController 
AudioSessionInitialize(NULL, NULL, NULL, NULL);
NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
[notificationCenter addObserver:self
                                         selector:@selector(volumeChanged:) 
                                             name:@"AVSystemController_SystemVolumeDidChangeNotification" 
                                           object:nil];

这应该通知我的方法 volumeChanged 任何 SystemVolume 更改,但它实际上并没有这样做.

This should notify my method volumeChanged of any SystemVolume changes but it doesn't actually do so.

因为普遍的信念告诉我,如果一个人为了用 Cocoa 实现某事而努力工作,那他就是在做一些根本错误的事情,我希望在这里错过一些东西.很难相信没有简单的方法可以获取当前的音量级别,但我还没有找到使用 Apple 文档、示例代码、Google 的方法,Apple 开发者论坛或观看 WWDC 2010 视频.

Since common belief tells me that if one is working too hard to achieve something with Cocoa one is doing something fundamentally wrong I'm expecting to miss something here. It's hard to believe that there is no simple way to get the current volume level, yet I haven't been able to find one using Apple's documentation, sample code, Google, Apple Developer Forums or by watching WWDC 2010 videos.

推荐答案

您是否有可能为 volumeChanged: 方法签名错误?这对我有用,倾倒在我的 appdelegate 中:

Any chance you did your signature wrong for the volumeChanged: method? This worked for me, dumped in my appdelegate:

- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    [[NSNotificationCenter defaultCenter]
     addObserver:self
     selector:@selector(volumeChanged:)
     name:@"AVSystemController_SystemVolumeDidChangeNotification"
     object:nil];
}

- (void)volumeChanged:(NSNotification *)notification
{
    float volume =
    [[[notification userInfo]
      objectForKey:@"AVSystemController_AudioVolumeNotificationParameter"]
     floatValue];

    // Do stuff with volume
}

我的 volumeChanged: 方法在每次按下按钮时都会被点击,即使音量没有改变(因为它已经达到了最大/最小).

My volumeChanged: method gets hit every time the button is pressed, even if the volume does not change as a result (because it's already at max/min).

这篇关于如何在 iOS 上获取音频音量级别和音量更改通知?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆