skip to Main Content

I’ve been officially notified that iOS 17.2 contains the new API to officially support volume button actuation for photo capture in a camera app.

API is called AVCaptureEventInteraction
Link

So If I use the AVCam sample code, How would I be able to attach this interaction?

    if #available(iOS 17.2, *) {
       let interaction = AVCaptureEventInteraction { event in
             print ("AVCaptureEventInteraction Fired")
       }
    }

API does not contain any details.

2

Answers


  1. You would need to integrate AVCaptureEventInteraction into the AVCaptureSession or a related component.
    The API usage could look like this:

    if #available(iOS 17.2, *) {
        let interaction = AVCaptureEventInteraction { event in
            // Handle the volume button press event here
            // e.g., Trigger photo capture
            print("AVCaptureEventInteraction Fired")
        }
        // Add the interaction to your capture session or related component
        // That might depend on further details from the API documentation
        captureSession.addInteraction(interaction)
    }
    

    But, since the AVCaptureEventInteraction API documentation is not detailed, you might need to experiment with where to add this interaction within the AVCaptureSession setup. A likely place is right after initializing the session and before starting it.
    I tried a quick search on GitHub… and found nothing(!)

    You could try and add the AVCaptureEventInteraction initialization in the configureSession method of the AVCam sample, after setting up the session but before starting it.
    Inside the closure of AVCaptureEventInteraction, implement the logic to capture a photo when the volume button is pressed. Add the interaction to the AVCaptureSession.

    func configureSession() {
        // existing setup code for AVCaptureSession 
    
        if #available(iOS 17.2, *) {
            let interaction = AVCaptureEventInteraction { event in
                // Implement the logic to capture a photo
                print("AVCaptureEventInteraction Fired")
                // Example: self.capturePhoto()
            }
            captureSession.addInteraction(interaction)
        }
    
        // rest of the session configuration 
    }
    

    Replace self.capturePhoto() with the actual method used in AVCam to capture a photo.

    Login or Signup to reply.
  2. AVCaptureEventInteraction implements the UIInterAction protocol. So you should attach the AVCaptureEventInteraction to a UI element (e.g. your shutter button) that is part of the responder chain.

    Example:

    if #available(iOS 17.2, *) {
        let shutterEventInteraction = AVCaptureEventInteraction.init { event in
            print(event)
            if (event.phase == AVCaptureEventPhase.began) {
                self.performCapture()
            }
        }
        shutterEventInteraction.isEnabled = true
        self.shutterButton.addInteraction(shutterEventInteraction)
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search