可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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:
Here is a solution
CGRect frame = CGRectMake(-1000, -1000, 100, 100);
MPVolumeView *volumeView = [[MPVolumeView alloc] initWithFrame:frame];
[volumeView sizeToFit];
[self.view addSubview:volumeView];
回答2:
Swift 3
let volumeView = MPVolumeView(frame: .zero)
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:
AppDelegate:
self.window?.insertSubview(MPVolumeView(), at: 0)
回答5:
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 :-)
回答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)