如何访问当前正在录制的视频的长度

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

我有一个 iOS 相机应用程序,当用户开始按住按钮时,我想开始录制,并且在该 longTap 方法中能够知道录制的时间 CURRENTLY。 .. 在它结束之前。我想知道视频到现在有多长或换句话说用户录制了多长时间

我的主要问题是如何在长按方法中知道用户录制了多长时间。这样如果录音到了X,就可以做Y了。

我目前尝试过:

下面在fileOutput方法中进行视频录制(用户按下按钮后调用)

{

        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)
            }
        }
    }

已开始录制 我得到了视频的缩略图。你会看到一个 num 变量但忽略它......它总是产生零,因为这是开始。

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 方法

        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")

    }

然而,我拥有的是非常多的错误。它几乎无法使用,绝对无法发布。

感谢您的帮助。

使用 UIControlEvents.touchDown & UIControlEvents.touchUpInside 事件。 试试这个。

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()

别看视频。看看时钟。

你知道录音是什么时候开始的,因为你开始了。你知道现在几点了。减去。