[description] I found answer how to hide volume HUD in StackOverflow and try it. Then it works at start up, but it doesn't work at returning from background.
I hope detect volume button press, and execute a certain process. Therefore, I would like to hide volume HUD.
I search it and get these answers.
- Hide device Volume HUD view while adjusitng volume with MPVolumeView slider
- applicationMusicPlayer volume notification
I tried it in my code. (I use the system volume change to detect volume button press)
private let audioSession: AVAudioSession = AVAudioSession.sharedInstance()
private var systemVolumeSlider: UISlider? = nil
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
let volumeView: MPVolumeView = MPVolumeView(frame: CGRect.zero)
if let volumeSlider: UISlider = volumeView.subviews.first as? UISlider {
self.systemVolumeSlider = volumeSlider
}
self.view.addSubview(volumeView)
do {
try self.audioSession.setActive(true)
} catch {
print("error: can not setActive")
}
self.audioSession.addObserver(
self, forKeyPath: "outputVolume", options: [.old, .new], context: nil
)
}
override func observeValue(
forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?
) {
// Process
}
}
It works when the application starts. However, it does not work when returning from the background (return from sleep or home).
What should I do to hide volume HUD at returning from background?