Hello there is a bunch of related questions but I would say none of them gives clear answer how to capture photos with volume buttons.
Currently I add observer to AVAudioSession
instance but it doesn't work on max and min volume values and seems showing volume status shadow popup over camera.
private func setupButtonsListener(){
do {
let audioSession = AVAudioSession.sharedInstance()
try audioSession.setActive(true)
audioSession.addObserver(self, forKeyPath: "outputVolume", options: NSKeyValueObservingOptions.new, context: nil)
} catch {
print("Error")
}
}
Then I handle event in function:
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
if keyPath == "outputVolume" {
capturePhoto() // Same function which I use for capturing photo for screen button.
} else {
super.observeValue(forKeyPath: keyPath, of: object, change: change, context: context)
}
}
Could you recommend me any solution which will: 1. Work even when max/min volume values reached. 2. Won't show volume value popup over screen.
EDIT:
based on @jayesh-kanzariya answer I add how to use JPSVolumeButtonHandler in Swift:
- Add
pod 'JPSVolumeButtonHandler'
to your pod file. - In project's root execute
pod install
- Configure bridging file for you project and add there:
#import <JPSVolumeButtonHandler/JPSVolumeButtonHandler.h>
In you ViewController add following code:
import JPSVolumeButtonHandler class YourViewController { var volumeHandler: JPSVolumeButtonHandler? override func viewDidLoad() { super.viewDidLoad() self.volumeHandler = JPSVolumeButtonHandler(up: {self.capturePhoto()}, downBlock: {self.capturePhoto()}) ... } }
Done!