Hide device Volume HUD view while adjusitng volume with MPVolumeView slider

懵懂的女人 提交于 2019-12-28 04:02:11

问题


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.

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

回答1:


Swift 3

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



回答2:


Here is a solution

CGRect frame = CGRectMake(-1000, -1000, 100, 100);
MPVolumeView *volumeView = [[MPVolumeView alloc] initWithFrame:frame];
[volumeView sizeToFit];
[self.view addSubview:volumeView];



回答3:


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

taken this refrence from applicationMusicPlayer volume notification




回答4:


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




回答5:


AppDelegate:

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



回答6:


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:


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

}



回答8:


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

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



回答9:


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;



回答10:


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)



回答11:


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)



回答12:


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.




回答13:


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)



回答14:


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)


来源:https://stackoverflow.com/questions/24444376/hide-device-volume-hud-view-while-adjusitng-volume-with-mpvolumeview-slider

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