applicationMusicPlayer volume notification

限于喜欢 提交于 2019-11-26 10:54:44

问题


I am using an applicationMusicPlayer and when i try to change the volume appear the visual notification, as shown in the picture. Here the code I am using:

[MPMusicPlayerController applicationMusicPlayer] setVolume:newVolune];

Anyone knows how to hide this notification?

\"enter


回答1:


I don't know where the docs says so, but if you add a MPVolumeView view to your app the system volume overlay goes away. Even if it is not visible:

- (void) viewDidLoad 
{
    [super viewDidLoad];
    MPVolumeView *volumeView = [[MPVolumeView alloc] initWithFrame: CGRectZero];
    [self.view addSubview: volumeView];
    [volumeView release];
    ...
}

You can use the hardware volume buttons, the setVolume method or directly interact with the control (if visible) that the overlay doesn't show up.




回答2:


For iOS6 I had to set an image with alpha 0 and non-zero size to the MPVolumeView's image fields in order to get the default volume change notification to disappear.

// hide the hardware volume slider
UIImage *thumb = [[UIImage alloc] initWithCIImage:[UIImage imageNamed:@"volumeHider"].CIImage scale:0.0 orientation:UIImageOrientationUp];
MPVolumeView *hwVolume = [[MPVolumeView alloc] initWithFrame:self.frame];
[hwVolume setUserInteractionEnabled:NO];
hwVolume.showsRouteButton = NO;
[hwVolume setVolumeThumbImage:thumb forState:UIControlStateNormal];
[hwVolume setMinimumVolumeSliderImage:thumb forState:UIControlStateNormal];
[hwVolume setMaximumVolumeSliderImage:thumb forState:UIControlStateNormal];
[self addSubview:hwVolume];

This made the MPVolumeView be "visible" on the screen, but invisible to the user.




回答3:


I encountered the same issue recently. Instead of adding the MPVolumeView to current view controller's view, I add it to the application's window once at the start of the app:

CGRect rect = CGRectMake(-500, -500, 0, 0);
MPVolumeView *volumeView = [[MPVolumeView alloc] initWithFrame:rect];
[self.window addSubview:volumeView];

This works in both iOS 7 and 8.




回答4:


Swift 3

You can hide the System MPVolumeView using

override func viewDidLoad() {
    super.viewDidLoad()
    let volumeView = MPVolumeView(frame: CGRect.zero)
    self.view.addSubview(volumeView)
  }



回答5:


I had success with this in iOS 6. Although it wouldn't perform well. It caused quite a bit of lag when sliding the thumbImage. I did have to take out the last 2 lines of code in order for this to work.

[volumeView release];
    ...



回答6:


For me, on iOS 7, none of above solutions worked. Here is how I did it:

_volume = [[MPVolumeView alloc] initWithFrame: CGRectMake(-100,-100,16,16)];
_volume.showsRouteButton = NO;
_volume.userInteractionEnabled = NO;
[self.view addSubview:_volume];
[_volume release];

That is, simply set MPVolumeView's frame to an off-screen location such as (-100,-100).



来源:https://stackoverflow.com/questions/7868457/applicationmusicplayer-volume-notification

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