简体   繁体   English

iPhone SDK - 如果按下硬件按钮,如何禁用音量指示器视图?

[英]iPhone SDK - How to disable the volume indicator view if the hardware buttons are pressed?

Is there a way to prevent the volume indicator view form showing if you press the volume up/down hardware buttons? 有没有办法阻止音量指示器视图窗体显示您是否按下音量增大/减小硬件按钮?

It's needed for a demo app only. 它仅用于演示应用程序。 So the method don't need to be App Store safe. 因此该方法不需要是App Store安全的。

It works like that: 它的工作方式如下:

  • play a silent file 播放一个无声的文件
  • add a volume View to your main view 将视图添加到主视图中
  • send the view to back 发送视图回来

eg 例如

 NSString *url = [[NSBundle mainBundle]
                       pathForResource:@"silent" ofType:@"mp3"];
 MPMoviePlayerController *moviePlayer = [[MPMoviePlayerController alloc]
                       initWithContentURL:[NSURL URLWithString:url]];
 [moviePlayer play];

 MPVolumeView *volumeView = [[[MPVolumeView alloc] initWithFrame:
                       CGRectMake(0, 0, 1, 1)] autorelease];
 [self.view addSubview:volumeView]; 
 [self.view sendSubviewToBack:volumeView];

IIRC, the presence of a MPVolumeView inhibits the display of the volume indicator overlay. IIRC,MPVolumeView的存在禁止显示音量指示器覆盖。 Try sticking it the relevant view and seeing if this is the case. 尝试坚持相关视图,看看是否是这种情况。

Then you can try various tricks to make it effectively invisible: 然后你可以尝试各种技巧使其有效隐形:

  • Make it hidden (or make a superview hidden). 隐藏(或隐藏超级视图)。
  • Set its alpha (or the alpha of a superview) to 0, or 0.01, or so. 将其alpha(或superview的alpha)设置为0或0.01,左右。
  • Move it off-screen 将其移出屏幕
  • Move it almost off-screen (eg so only the top-left pixel is on screen) 几乎在屏幕外移动它(例如,只有左上角的像素在屏幕上)
  • Stick it under another view. 坚持下另一种观点。
  • Stick it in a subview with clipsToBounds=ON, and move it outside those bounds 使用clipsToBounds = ON将其粘贴在子视图中,并将其移到这些边界之外
  • Set volumeView.layer.mask to a new (thus fully-transparent) CALayer. 将volumeView.layer.mask设置为新的(因此完全透明的)CALayer。 Set volumeView.userInteractionEnabled = NO. 设置volumeView.userInteractionEnabled = NO。

All of these are theoretically detectable by MPVolumeView, but I suspect some of them will work. 所有这些在理论上都可以被MPVolumeView检测到,但我怀疑它们中的一些会起作用。

- (void)viewDidLoad
 {
  [super viewDidLoad];

  //get current volume level
  oldVolume= [[MPMusicPlayerController applicationMusicPlayer] volume];

  //hide volume indicator         
  MPVolumeView *volumeView = [[[MPVolumeView alloc] initWithFrame:
                             CGRectMake(0, 0, 1, 1)] autorelease];

  musicController=[MPMusicPlayerController applicationMusicPlayer];
  [self.view addSubview:volumeView];
  [self.view sendSubviewToBack:volumeView];
  [NSTimer scheduledTimerWithTimeInterval:0.02 target:self selector:@selector(volume) userInfo:nil repeats:YES];
}

- (void)volume
{
  if ([musicController volume]>oldVolume || [musicController volume]<oldVolume) {
    [musicController setVolume:oldVolume];
    // do some stuff here and the volume level never changes, just like volume action in camera app
   }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM