iosiphoneswiftavfoundationcmtime

How can I access the length of the video that is CURRENTLY being recorded


I have an iOS camera app where when the user starts to hold a button I want to start recording and within that longTap method be able to know how long the recording is CURRENTLY... BEFORE it has ended. I want to know how long the video is as of now or put differently how long the user has been recording for.

My main problem is how to know, within the long press method, how long the user has been recording for. So that if the recording has reached X, it can do Y.

I have currently tried:

The following in the fileOutput method for video recording (called after the user has let for of the button)

{

        let videoRecorded = outputURL! as URL

        let determineAsset = AVAsset(url: videoRecorded)
        let determineCmTime = CMTime(value: determineAsset.duration.value, timescale: 600)
        let secondsBruh = CMTimeGetSeconds(determineCmTime)
        print(secondsBruh, "<--- seconds br8h")

        if doNotRunPlayback == true {
            print("DO NIT RUN PLAYBACK WAS TRUE")
        } else {
            print("here--- ", secondsBruh)
            if secondsBruh <= 0.35 {
                print("iiiiiiiiii")
                isThisOverSeconds = true
                photoOutput?.capturePhoto(with: AVCapturePhotoSettings(), delegate: self) //, put in the

            } else {
                isSettingThumbnail = false
                playRecordedVideo(videoURL: videoRecorded)
            }
        }
    }

Did start recording I get a thumbnail for the video. U will see a num variable but disregard it... it will always yield zero given that this is the start.

func fileOutput(_ output: AVCaptureFileOutput, didStartRecordingTo fileURL: URL, from connections: [AVCaptureConnection]) {
print("U IN THIS DIDSTARRECORD???")
isSettingThumbnail = true
photoOutput?.capturePhoto(with: AVCapturePhotoSettings(), delegate: self)

let testUrl = fileURL as URL!
let testAsset = AVAsset(url: testUrl!)
let deCmTime = CMTime(value: testAsset.duration.value, timescale: 600)
let seconds = CMTimeGetSeconds(deCmTime)
print(seconds, "<--- ok this si seconds")
num = Int(seconds)

print("723648732648732658723465872:::::", Int(seconds))
    print("OUT THIS DIDSTART RECORD")
}

LongTap method

        if sender.state == .ended {
        print("UIGestureRecognizerStateEnded")
        if num == 0 {
            print("5555555555555")
            photoOutput?.capturePhoto(with: AVCapturePhotoSettings(), delegate: self)
        } else {
            print("num was greater than 0")
        }

        stopRecording()
        print("didLongTapEndedend")

    } else if sender.state == .began {
        print("UIGestureRecognizerStateBegan.")

        startCapture()
        print("didLongTapBeganend")

    }

What I have however is very buggy. It's pretty much unusable, definitely unreleasable.

Thanks for any help.


Solution

  • Use the UIControlEvents.touchDown & UIControlEvents.touchUpInside events. Try this.

    import UIKit
    import PlaygroundSupport
    
    class MyViewController : UIViewController {
    
        var timer:Timer!
    
        override func loadView() {
            let view = UIView()
    
    
            let btn = UIButton.init(frame: CGRect(x: 150, y: 200, width: 200, height: 20))
            btn.setTitle("Record/Capture", for: UIControlState.normal)
            btn.clipsToBounds
            btn.backgroundColor = UIColor.red
            view.addSubview(btn)
            self.view = view
    
            btn.addTarget(self, action: #selector(touchDown(_:)), for: UIControlEvents.touchDown)
            btn.addTarget(self, action: #selector(touchUpInside(_:)), for: UIControlEvents.touchUpInside)
    
        }
    
       @objc func touchDown(_ sender:UIButton) {
        timer = Timer.scheduledTimer(withTimeInterval: TimeInterval.init(3), repeats: false, block: { (timer) in
            self.startRecording()
        })
    }
    
        @objc func touchUpInside(_ sender:UIButton) {
            if timer.isValid {
                self.capturePhoto()
            } else {
                self.stopRecording()
            }
            timer.invalidate()
        }
    
        func startRecording() {
            // Recording
            print("Start Recording")
            timer.invalidate()
        }
    
        func stopRecording() {
            // stopRecording
            print("Stop Recording")
    
        }
    
        func capturePhoto() {
            print("Capture Photo")
        }
    }
    // Present the view controller in the Live View window
    PlaygroundPage.current.liveView = MyViewController()