【问题标题】:Hide device Volume HUD view while adjusitng volume with MPVolumeView slider使用 MPVolumeView 滑块调整音量时隐藏设备音量 HUD 视图
【发布时间】:2014-08-18 02:58:45
【问题描述】:

我在我的 iPad 应用程序中使用 MPMoviePlayer 实现视频播放器,使用 MPVolumeView 进行音量控制。 我的问题是,当我滚动音量视图以调整音量时,会显示设备的音量 hud 覆盖,如下面的屏幕截图所示。

如何禁用此系统音量指示器界面? 我的代码是:

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

【问题讨论】:

  • 实际上我的音量滑块(在问题的屏幕截图中显示为我的音量视图滑块)是使用 MPVolumeView 创建的,在代码中名为 PlaybackMPVolumeView 的插座。 [MPMusicPlayerController applicationMusicPlayer](在您在上面评论中的链接中提到)不能用于设置音量,因为它已被弃用。因此,如果我将 volumeView 的框架设置为 rectZero,我将没有滑块。

标签: ios objective-c mpvolumeview


【解决方案1】:

斯威夫特 3

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

【讨论】:

    【解决方案2】:

    这里有一个解决方案

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

    【讨论】:

    • 很棒的解决方案!
    【解决方案3】:

    如果您想在所有应用屏幕上隐藏 HUD,请使用以下通用代码:

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

    如果您在某些时候需要显示音量 HUD,只需将其从 app.window 中删除。 享受:-)

    【讨论】:

    • 至少对我来说这是正确的解决方案。上面的一个,在每个屏幕上隐藏音量视图,导致应用程序性能下降,直到在视图之间来回切换大约 15 次迭代后变得完全没有响应。似乎其他解决方案中的 MPVolumeView 必须以某种方式“释放”。
    【解决方案4】:
    - (void) viewDidLoad 
    {
        [super viewDidLoad];
        MPVolumeView *volumeView = [[MPVolumeView alloc] initWithFrame: CGRectZero];
        [self.view addSubview: volumeView];
        ...
    }
    

    引用自 applicationMusicPlayer volume notification

    【讨论】:

      【解决方案5】:

      我为 Swift 4

      想出的最佳解决方案
      import MediaPlayer
      

      添加这3行代码

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

      【讨论】:

        【解决方案6】:

        AppDelegate:

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

        【讨论】:

          【解决方案7】:

          只需在使 MPVolumeView 实例可见的位置添加这些行。例如:如果您有一个切换音量按钮

          -(void)toggle
          {
             self.volumeView.hidden = !self.volumeView.hidden;
             [self.volumeView willMoveToSuperview:self.volumeView.superview];
             [self.volumeView didMoveToSuperview];
          }
          

          如果您有一个始终可见的音量滑块(MPVolumeView 的实例)。

          -(void)viewDidAppear:(BOOL)animated
          {
              [super viewDidAppear:animated];
              self.volumeView.hidden = NO;
              [self.volumeView willMoveToSuperview:self.volumeView.superview];
              [self.volumeView didMoveToSuperview];
          
          }
          

          【讨论】:

            【解决方案8】:

            除了将 MPVolumeView 添加为子视图之外,以下代码对我有用。

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

            【讨论】:

              【解决方案9】:

              这对我有用。

              MPVolumeView *volumeView2 = [[MPVolumeView alloc] initWithFrame:CGRectMake(-28, -2, 24, 24)];
              [self.view addSubview:volumeView2];
              volumeView2.showsVolumeSlider = true;
              volumeView2.showsRouteButton = false;
              _videoPlayer.AllowsAirPlay = false;
              

              【讨论】:

                【解决方案10】:

                这是 Swift 中的上述解决方案。适用于 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】:

                  这实际上对我有用,由于某种原因,如果 CGRect 是 CGRect.zero 那么如果左上角有一个按钮将不起作用...,所以这个起作用了:

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

                    我必须执行以下操作才能禁用音量覆盖:

                    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,没有 Swift。适用于 iOS 8.1+(未测试早期版本)。添加这个,因为我在没有上下文的情况下根据给定的答案努力实现这个。

                    【讨论】:

                      【解决方案13】:

                      对于所有只想隐藏 HUD 而不播放任何视频或内容的人,不要忘记添加:

                       try! AVAudioSession.sharedInstance().setActive(true, options: [])
                       try! AVAudioSession.sharedInstance().setCategory(.ambient, mode: .default, options: [])
                      

                      然后这些行将起作用:

                      let frame = CGRect(x: -1000, y: -1000, width: 100, height: 100)
                      let volumeView = MPVolumeView(frame: frame)
                      volumeView.sizeToFit()
                      self.view.addSubview(volumeView)
                      

                      【讨论】:

                        【解决方案14】:

                        如果您希望在整个应用中禁用 MPVolumeSettingsAlert,请将此行添加到 AppDelegate 中的 didFinishLaunchingWithOptions 中:

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

                        如果您只需要在一个 View Controller 中禁用它,请将此行添加到特定 ViewController 的 viewDidLoad() 中:

                        self.view.insertSubview(MPVolumeView(), at: 0)
                        

                        【讨论】:

                          猜你喜欢
                          • 1970-01-01
                          • 2018-08-18
                          • 1970-01-01
                          • 1970-01-01
                          • 1970-01-01
                          • 1970-01-01
                          • 1970-01-01
                          • 1970-01-01
                          • 2019-04-30
                          相关资源
                          最近更新 更多