How to change volume programmatically on iOS 11.4

前端 未结 4 1102
深忆病人
深忆病人 2021-01-17 18:52

Before, I was setting sound volume programmatically using this approach:

MPVolumeView *volumeView = [[MPVolumeView alloc] init];
UISlider *volumeViewSlider =         


        
4条回答
  •  北海茫月
    2021-01-17 19:45

    I had to have a MPVolumeView as subview to a view in the hierarchy for the hud not to show up on iOS 12. It needs to be slightly visible:

    let volume = MPVolumeView(frame: .zero)
    volume.setVolumeThumbImage(UIImage(), for: UIControl.State())
    volume.isUserInteractionEnabled = false
    volumelume.alpha = 0.0001
    volume.showsRouteButton = false
    view.addSubview(volume)
    

    When setting the volume I get the slider from MPVolumeView as with previous posters and set the value:

    func setVolumeLevel(_ volumeLevel: Float) {
        guard let slider = volume.subviews.compactMap({ $0 as? UISlider }).first else {
            return
        }
    
        slider.value = volumeLevel
    }
    

提交回复
热议问题