Joh*_*n S 10 iphone audio volume avfoundation ios
我需要检测用户何时按下硬件音量键,(App Store安全方法)我已经尝试了许多没有运气的事情.你知道如何实现这样的功能吗?目前我正在注册通知,但它们似乎没有被调用.这是我的代码:
AudioSessionInitialize(NULL, NULL, NULL, NULL);
NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
[notificationCenter addObserver:self
selector:@selector(volumeChanged:)
name:@"AVSystemController_SystemVolumeDidChangeNotification"
object:nil];
Run Code Online (Sandbox Code Playgroud)
接收方法是:
-(void)volumeChanged:(NSNotification *)notification{
NSLog(@"YAY, VOLUME WAS CHANGED");}
Run Code Online (Sandbox Code Playgroud)
任何提示将非常感谢.
您需要在通知触发之前启动音频会话:
AudioSessionInitialize(NULL, NULL, NULL, NULL);
AudioSessionSetActive(true);
Run Code Online (Sandbox Code Playgroud)
现在您可以订阅通知:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(volumeChanged:)
name:@"AVSystemController_SystemVolumeDidChangeNotification"
object:nil];
Run Code Online (Sandbox Code Playgroud)
获取音量:
float volume = [[[notification userInfo]
objectForKey:@"AVSystemController_AudioVolumeNotificationParameter"]
floatValue];
Run Code Online (Sandbox Code Playgroud)
您需要存储音量并将其与之前从通知中获得的值进行比较,以了解按下了哪个按钮。
当用户按下音量键时,该解决方案仍然会调整系统音量,并显示音量叠加层。如果您想避免更改系统音量并显示叠加层(本质上完全重新调整音量键的用途),请参阅此答案