applicationMusicPlayer volume notification
Asked Answered
C

6

14

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 image description here

Corrugate answered 23/10, 2011 at 19:8 Comment(0)
H
28

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.

Hybrid answered 25/10, 2011 at 12:2 Comment(7)
I had a problem with this showing the AirPlay icon, even in a CGRectZero, which did not happen during testing because there were no AirPlay devices on our wifi network. You need to set volumeView.showsRouteButton = NO;Sunset
@AshFurrow, madmw, When I tried to access above code in my app, it showing me error like Use of undefined object volumeView. How to fix it? Any header file or framework that required?Brote
@Brote search MPVolumeView in the doc, you'll see it's part of MediaPlayer.framework Repeat until there is no compiler complains.Hybrid
@madmw, I have already added it to my app! but it still showing errors.Brote
With iOS 6 you cannot have a CGRectZero, the view has to be visible. To get around this set the volumeView's thumb image and min/max volume slider image to an image with a scale of 0.Embodiment
@zshooter: Could you please give some sample code for iOS 6? ThanksSuzysuzzy
@ZuzooVn: Answer added with example.Embodiment
E
4

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.

Embodiment answered 25/6, 2013 at 22:58 Comment(3)
I'm having a heck of a time getting this to work in iOS 6.1.4 alongside mpmusicplayercontroller setvolume. I'm just trying to set my app's volume to a state on my initial viewcontroller, but hide the system volume overlay when the setvolume occurs...do I need to be putting the code below somewhere else?Counterirritant
edit: I'm putting your code above into viewDidLoad, I'm putting the MPMusicPlayerController setvolume in super viewDidLoad.Counterirritant
Got this working by putting the above code in my initial VC, and then performing the setvolume on subsequent VC's. Definitely not the desirable approach, and not sure if that's how others got it working, but the setup happens to work w/ my app layout for now.Counterirritant
E
2

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.

Epochal answered 14/10, 2014 at 21:16 Comment(1)
Thanks, worked for me. Not sure why Apple decided to change that for iOS 8.Carburetor
B
1

Swift 3

You can hide the System MPVolumeView using

override func viewDidLoad() {
    super.viewDidLoad()
    let volumeView = MPVolumeView(frame: CGRect.zero)
    self.view.addSubview(volumeView)
  }
Bum answered 14/2, 2017 at 16:40 Comment(0)
T
0

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];
    ...
Trilogy answered 1/6, 2013 at 8:39 Comment(0)
C
0

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).

Calctufa answered 4/2, 2014 at 21:52 Comment(1)
didnt work for me either. I tried it with lazy loading and with self. in the init method. I didn't get any closer :(Stith

© 2022 - 2024 — McMap. All rights reserved.