如何获得音频的音量和音量改变iOS上的通知? [英] How to get audio volume level, and volume changed notifications on iOS?

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

问题描述

我正在写一个非常简单的应用程序可以播放声音时pressing一个按钮。由于该按钮不会使一个很大的意义,当设备设置为无声我想禁用它,当设备的音量是零。 (随后重新启用它,当音量再次拍成。)

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 ,其中所说的按钮是用来实现的。我已经与iPhone 4运行iOS 4.0.1和4.0.2以及一个iPhone 3G运行4.0.1测试这一点。内置与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.

随着苹果公司的音频服务的文件,我应该注册一个 AudioSessionAddPropertyListener kAudioSessionProperty_CurrentHardwareOutputVolume 这应该工作是这样的:

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

返回值 0 ,这意味着注册回调的工作。

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

可悲的是,我从来没有回调到我的功能 audioVolumeChangeListenerCallback 当我preSS我的设备,耳机唱首歌上的音量按钮或翻转铃声静音开关

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.

在使用完全相同的code登记为 kAudioSessionProperty_AudioRouteChange (这是作为在WWDC的视频,开发者文档类似的样本项目上,而在众多的网站interwebs)其实我的的改变音频路径(在输入/输出耳机插上或对接设备)时,得到一个回调。

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).

一个用户名为道格打开名为<一个线程href=\"http://stackoverflow.com/questions/3129073/iphone-volume-changed-event-for-volume-already-max\">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.

由于共同的信仰告诉我,如果一个人工作太努力实现与可可一个是做一些根本性的错误,我期待在这里错过什么东西。很难相信,有的没有简单的方法的到的获得的当前的音量,但我一直没能使用苹果的文档,找到一个,样本code ,谷歌,苹果开发者论坛或观看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:方法被击中每次按钮pressed时候,即使卷没有因此改变(因为它已经在最大值/最小值)

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天全站免登陆