Hide device Volume HUD view while adjusitng volume

2019-01-11 13:46发布

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

13条回答
一夜七次
2楼-- · 2019-01-11 14:24

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.

查看更多
成全新的幸福
3楼-- · 2019-01-11 14:25

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)
查看更多
祖国的老花朵
4楼-- · 2019-01-11 14:30

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

查看更多
beautiful°
5楼-- · 2019-01-11 14:31

AppDelegate:

self.window?.insertSubview(MPVolumeView(), at: 0)
查看更多
Deceive 欺骗
6楼-- · 2019-01-11 14:32

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)
查看更多
我想做一个坏孩纸
7楼-- · 2019-01-11 14:37
- (void) viewDidLoad 
{
    [super viewDidLoad];
    MPVolumeView *volumeView = [[MPVolumeView alloc] initWithFrame: CGRectZero];
    [self.view addSubview: volumeView];
    ...
}

taken this refrence from applicationMusicPlayer volume notification

查看更多
登录 后发表回答