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

醉酒当歌 提交于 2020-01-29 03:19:28

问题


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.


回答1:


IIRC, the presence of a MPVolumeView inhibits the display of the volume indicator overlay. 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.
  • Move it off-screen
  • Move it almost off-screen (e.g. 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
  • Set volumeView.layer.mask to a new (thus fully-transparent) CALayer. Set volumeView.userInteractionEnabled = NO.

All of these are theoretically detectable by MPVolumeView, but I suspect some of them will work.




回答2:


It works like that:

  • play a silent file
  • add a volume View to your main view
  • send the view to back

e.g

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



回答3:


- (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
   }
}


来源:https://stackoverflow.com/questions/3845222/iphone-sdk-how-to-disable-the-volume-indicator-view-if-the-hardware-buttons-ar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!