程序访问 iPhone 音量按钮 [英] program access to iPhone volume buttons

查看:18
本文介绍了程序访问 iPhone 音量按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法订阅音量按钮按下事件?

Is there any way to subscribe to volume buttons press events?

推荐答案

最近被 Apple 拒绝后

不要使用这个.Apple 现在使用了一些补丁,如果它使用任何私有 API,它会立即拒绝您的应用程序 - 但在这里应该注意,App Store 上的相当多的应用程序已经使用它并且仍然存在!

Do not use this. Apple now uses some patch which would reject your app straightaway if it uses any of the private APIs - though should note here that quite some apps on the App Store use this already and are still there!

现在唯一的方法是让 AVAudioPlayer 准备播放但不播放([player prepareToPlay]).这似乎负责根据摇杆按钮调整应用程序的音量.

The only way to do this now is to have an AVAudioPlayer prepared to play but not playing ([player prepareToPlay]). This seems to take care of adjusting the app's volume according to the rocker buttons.

目前没有其他已发布的方法来处理这个问题.

There's no other published way currently to handle this.

请阅读以上注意事项

是的,使用 MPVolumeView

Yes, Use the MPVolumeView

MPVolumeView *volume = [[[MPVolumeView alloc] initWithFrame:CGRectMake(18.0, 340.0, 284.0, 23.0)] autorelease];
  [[self view] addSubview:volume];

  [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(volumeChanged:) 
                                        name:@"AVSystemController_SystemVolumeDidChangeNotification" 
                                        object:nil];    
  for (UIView *view in [volume subviews]){
    if ([[[view class] description] isEqualToString:@"MPVolumeSlider"]) {
      volumeViewSlider = view;  //volumeViewSlider is a UIView * object
    }
  }
  [volumeViewSlider _updateVolumeFromAVSystemController];

-(IBAction)volumeChanged:(id)sender{
  [volumeViewSlider _updateVolumeFromAVSystemController];
}

这将为您提供一个滑块(与 ipod 中使用的滑块相同),其值将根据手机的音量变化

This will give you a slider (same as one used in ipod) whose value will change acc to volume of the phone

您将收到一个编译时警告,即视图可能不会响应 _updateVolumeFromAVSystemControl,而只是忽略它.

You will get a compile-time warning that view may not respond to _updateVolumeFromAVSystemControl, but just ignore it.

这篇关于程序访问 iPhone 音量按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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