检测音量按钮按下并释放iOS

thi*_*zy4 4 cocoa-touch objective-c ios

可能重复:
程序访问iPhone音量按钮

是否可以在iOS应用中检测音量增大按钮按下/释放?

V-X*_*eme 5

您可以在应用程序中检测音量按钮按下,但它使用Apple的私有API.由于它们不允许您在应用程序中使用其私有API,因此直接导致拒绝您的应用程序,因此使用此风险需要您自担风险.

在你的身上viewDidLoad:,例如:

MPVolumeView *volumeView = [[MPVolumeView alloc] initWithFrame:CGRectMake(18, 340, 284, 23)];
[self.view addSubview:volumeView];

[NSNotificationCenter.defaultCenter
    addObserver:self
       selector:@selector(volumeDidChange:)
           name:@"AVSystemController_SystemVolumeDidChangeNotification"
         object:nil];
Run Code Online (Sandbox Code Playgroud)


The*_*ion 3

看看http://fredandrandall.com/blog/2011/11/18/take-control-of-the-volume-buttons-on-ios-like-camera/

本质上,您必须初始化音频会话,使其处于活动状态,然后监听更改。最后您发起回调。

但是,请小心硬件命令的实现。无意的误用可能会导致您被应用商店禁止。