Hide device Volume HUD view while adjusitng volume with MPVolumeView slider

大城市里の小女人 提交于 2019-11-27 14:03:41
dimo hamdy

Swift 3

let volumeView = MPVolumeView(frame: .zero)
view.addSubview(volumeView)

Here is a solution

CGRect frame = CGRectMake(-1000, -1000, 100, 100);
MPVolumeView *volumeView = [[MPVolumeView alloc] initWithFrame:frame];
[volumeView sizeToFit];
[self.view addSubview:volumeView];
Sourabh Sharma
- (void) viewDidLoad 
{
    [super viewDidLoad];
    MPVolumeView *volumeView = [[MPVolumeView alloc] initWithFrame: CGRectZero];
    [self.view addSubview: volumeView];
    ...
}

taken this refrence from applicationMusicPlayer volume notification

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

AppDelegate:

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

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)

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

}

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

AudioSessionInitialize(NULL, NULL, NULL, NULL);
AudioSessionSetActive(YES);
Devendra Kamal

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

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)

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)

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.

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)

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