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条回答
SAY GOODBYE
2楼-- · 2019-01-11 14:38

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)
查看更多
Bombasti
3楼-- · 2019-01-11 14:38

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)
查看更多
走好不送
4楼-- · 2019-01-11 14:41

Swift 3

let volumeView = MPVolumeView(frame: .zero)
view.addSubview(volumeView)
查看更多
干净又极端
5楼-- · 2019-01-11 14:43

Here is a solution

CGRect frame = CGRectMake(-1000, -1000, 100, 100);
MPVolumeView *volumeView = [[MPVolumeView alloc] initWithFrame:frame];
[volumeView sizeToFit];
[self.view addSubview:volumeView];
查看更多
男人必须洒脱
6楼-- · 2019-01-11 14:45

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

AudioSessionInitialize(NULL, NULL, NULL, NULL);
AudioSessionSetActive(YES);
查看更多
迷人小祖宗
7楼-- · 2019-01-11 14:49

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

}
查看更多
登录 后发表回答