17

I was implementing a video player with MPMoviePlayer in my iPad Application, used MPVolumeView for the volume control. My problem is that when i scroll the volume view to adjust the volume is showing the device's volume hud overlay as in the screenshot below.

enter image description here

How can i disable this system volume indicator hud? My code is :

@property (weak, nonatomic) IBOutlet MPVolumeView *playbackMPVolumeView;

//Customizing controller
- (void)customizeVolumeController
{
    _playbackMPVolumeView.showsRouteButton  = YES;
    _playbackMPVolumeView.showsVolumeSlider = YES;
    [_playbackMPVolumeView setVolumeThumbImage:[UIImage imageNamed:@"volume_slider_thumb.png"] forState:UIControlStateNormal];
}
Alex Andrews
  • 1,498
  • 2
  • 19
  • 33
  • 1
    check this http://stackoverflow.com/questions/7868457/applicationmusicplayer-volume-notification – Suhail kalathil Jun 27 '14 at 09:55
  • Actually my volume slider(shown as My Volume View Slider in the screenshot in the question) is created with MPVolumeView , the outlet named playbackMPVolumeView in the code. [MPMusicPlayerController applicationMusicPlayer] (mentioned in the link you put in the comment above) can't be used to set volume because it is deprecated. So if i set the frame to rectZero for my volumeView there will be no slider for me. – Alex Andrews Jun 27 '14 at 11:11
  • Is there any solution for iOS 16.4? It stopped working after iPhone has been updated to 16.4. – Chaki_Black Mar 20 '23 at 08:50

14 Answers14

10

Swift 3

let volumeView = MPVolumeView(frame: .zero)
view.addSubview(volumeView)
return true
  • 7,839
  • 2
  • 19
  • 35
dimohamdy
  • 2,917
  • 30
  • 28
6

Here is a solution

CGRect frame = CGRectMake(-1000, -1000, 100, 100);
MPVolumeView *volumeView = [[MPVolumeView alloc] initWithFrame:frame];
[volumeView sizeToFit];
[self.view addSubview:volumeView];
Hardik Thakkar
  • 15,269
  • 2
  • 94
  • 81
6

If you want hide the HUD over all app screens, use following universal code:

static let mpVolumeView = MPVolumeView()
static func setupHiddenMPVolume(_ view: UIView){
    if let app = UIApplication.shared.delegate as? AppDelegate, let window = app.window {
        mpVolumeView.alpha = 0.000001
        window.addSubview(mpVolumeView)
    }
}

In case if you at some poin will need to show the Volume HUD just remove it from app.window . Enjoy :-)

Vitaliy A
  • 3,651
  • 1
  • 32
  • 34
  • 2
    At least for me this was the right solution. The one above, hiding the volume view on each screen, caused problems with the app performance degrading until becoming not responsive at all after some 15 iterations of switching back and forth between views. It seems the MPVolumeView in the other solution has to be "released" somehow. – avidD Apr 12 '19 at 07:29
5
- (void) viewDidLoad 
{
    [super viewDidLoad];
    MPVolumeView *volumeView = [[MPVolumeView alloc] initWithFrame: CGRectZero];
    [self.view addSubview: volumeView];
    ...
}

taken this refrence from applicationMusicPlayer volume notification

Community
  • 1
  • 1
Sourabh Sharma
  • 8,222
  • 5
  • 68
  • 78
4

The best solution I came up with for Swift 4

import MediaPlayer

Add these 3 lines of code

    let volumeView = MPVolumeView(frame: .zero)
    volumeView.clipsToBounds = true
    view.addSubview(volumeView)
Foti Dim
  • 1,303
  • 13
  • 19
3

AppDelegate:

self.window?.insertSubview(MPVolumeView(), at: 0)
Valentin Shamardin
  • 3,569
  • 4
  • 34
  • 51
1

Just add these lines where you are making your MPVolumeView instance visible. For example : In case you have a toggle volume button

-(void)toggle
{
   self.volumeView.hidden = !self.volumeView.hidden;
   [self.volumeView willMoveToSuperview:self.volumeView.superview];
   [self.volumeView didMoveToSuperview];
}

In case you have an always visible volume slider (instance of MPVolumeView).

-(void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    self.volumeView.hidden = NO;
    [self.volumeView willMoveToSuperview:self.volumeView.superview];
    [self.volumeView didMoveToSuperview];

}
Umair
  • 1,203
  • 13
  • 15
0

Below code works for me apart from adding MPVolumeView as subview.

AudioSessionInitialize(NULL, NULL, NULL, NULL);
AudioSessionSetActive(YES);
aios
  • 405
  • 5
  • 14
0

Here is the above solution in Swift. Works with Swift 2-3.

let frame = CGRect(x: -1000, y: -1000, width: 100, height: 100)
let volumeView = MPVolumeView(frame: frame)
volumeView.sizeToFit()
self.view!.addSubview(volumeView)
Pang
  • 9,564
  • 146
  • 81
  • 122
Jerland2
  • 1,096
  • 11
  • 28
0

This works for me.

MPVolumeView *volumeView2 = [[MPVolumeView alloc] initWithFrame:CGRectMake(-28, -2, 24, 24)];
[self.view addSubview:volumeView2];
volumeView2.showsVolumeSlider = true;
volumeView2.showsRouteButton = false;
_videoPlayer.AllowsAirPlay = false;
Pang
  • 9,564
  • 146
  • 81
  • 122
0

This is what actually worked for me, for some reason if the CGRect is CGRect.zero then if there is a button in the upper left corner in won't work..., so this one worked:

let volumeView = MPVolumeView(frame: CGRect.init(x: self.view.frame.maxX, y: self.view.frame.maxY, width: 0, height: 0))
volumeView.clipsToBounds = true
volumeView.showsRouteButton = false
self.view.addSubview(volumeView)
Daniel Illescas
  • 5,346
  • 3
  • 17
  • 23
0

I had to do the following to disable the volume overlay:

AppDeletage.m:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
  ...

  // disable volume hud
  CGRect rect = CGRectMake(-500, -500, 0, 0);
  MPVolumeView *volumeView = [[MPVolumeView alloc] initWithFrame:rect];
  [self.window addSubview:volumeView];
  return YES;
}

AppDelegate.h:

@interface AppDelegate : UIResponder <UIApplicationDelegate>

...
@property (nonatomic, strong) MPVolumeView   * volumeView;

@end

Objective C only, no Swift. Works with iOS 8.1+ (not tested earlier versions). Adding this as I struggled to implement this based on the given answers without context.

Lars Graubner
  • 817
  • 4
  • 8
0

For all that just want to hide the HUD without playing any video or content don't forgot to add this:

 try! AVAudioSession.sharedInstance().setActive(true, options: [])
 try! AVAudioSession.sharedInstance().setCategory(.ambient, mode: .default, options: [])

Then those lines will work:

let frame = CGRect(x: -1000, y: -1000, width: 100, height: 100)
let volumeView = MPVolumeView(frame: frame)
volumeView.sizeToFit()
self.view.addSubview(volumeView)
arnoapp
  • 2,416
  • 4
  • 38
  • 70
0

If you wish to disable the MPVolumeSettingsAlert throughout your whole app, add this line into didFinishLaunchingWithOptions in AppDelegate:

self.window?.insertSubview(MPVolumeView(), at: 0)

If you need to disable it in just one View Controller, add this line into viewDidLoad() of the specific ViewController:

self.view.insertSubview(MPVolumeView(), at: 0)
redribben
  • 186
  • 1
  • 7