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];
}
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];
- (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);
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;
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)
来源:https://stackoverflow.com/questions/24444376/hide-device-volume-hud-view-while-adjusitng-volume-with-mpvolumeview-slider