2

I use MPVolumeView and UISlider to control my device audio and below is my code:

extension MPVolumeView {
    var volumeSlider: UISlider {
        self.showsRouteButton = false
        self.showsVolumeSlider = true

        var slider = UISlider()
        for subview in self.subviews {
            if subview.isKind(of: UISlider.self){
                slider = subview as! UISlider
                slider.isContinuous = false
                slider.minimumTrackTintColor = UIColor.outlineStrokeColor
                slider.setThumbImage(#imageLiteral(resourceName: "volume"), for: .normal)
                slider.maximumTrackTintColor = UIColor.lightGray
                (subview as! UISlider).value = AVAudioSession.sharedInstance().outputVolume
                return slider
            }
        }
        return slider
    }
}


let mpVolumeView: MPVolumeView = {
    let view = MPVolumeView()
    return view
}()

override func viewDidLoad() { 
    _ = mpVolumeView.volumeSlider
    view.addSubview(mpVolumeView)
}

Everything works great but everytime when I open my app, the volume HUD appears and fades away after a few seconds. It won't show up again after that but I wish there would be an option to hide it all the time. I have tried to set MPVolumeView's frame to .zero but it still doesn't work. Is there a way that I can do so?

sample image

Dharmesh Kheni
  • 71,228
  • 33
  • 160
  • 165
  • you created a weak reference where are you calling in didLoad or WillAppear ? – iOS Geek Jan 03 '18 at 04:49
  • I didn't create a weak reference and I called it in viewDidLoad –  Jan 03 '18 at 04:52
  • if you calling this In viewDidLoad it Will always appear for the first time when your respective screen is loaded to memory stack , so avoid calling it unnecessarily in didLoad and just call it where you need it, I think this Line "view.addSubview(mpVolumeView)" show it , so just add it in view only when it is necessary no need to use this line in didLoad – iOS Geek Jan 03 '18 at 04:54
  • I mean I don't need the volume HUD. I just need to show user the volume slider with other UIs in ViewDidLoad. Any suggestions on where I should call it? –  Jan 03 '18 at 04:57
  • Yes. I want to hide the volume UI which has a speaker image in my app and only shows the slider at the bottom of the screen for user to control volume. –  Jan 03 '18 at 05:01
  • where are you setting its frame to zero ? – iOS Geek Jan 03 '18 at 05:03
  • 1
    Possible can you share sample code ? – iOS Geek Jan 03 '18 at 05:05
  • I tried let view = MPVolumeView(frame: .zero) –  Jan 03 '18 at 05:05

0 Answers0