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

Ios 如何访问当前正在录制的视频的长度,ios,iphone,swift,avfoundation,cmtime,Ios,Iphone,Swift,Avfoundation,Cmtime,我有一个iOS摄像头应用程序,当用户开始按住一个按钮时,我想开始录制,在这个长镜头方法中,我可以知道当前录制的时间。。。在它结束之前。我想知道视频到现在为止有多长时间,或者换个说法,用户录制了多长时间 我的主要问题是如何知道,在长按方法中,用户已经录制了多长时间。因此,如果录音达到X,它可以做Y 我目前已尝试: 视频录制的fileOutput方法中的以下内容(在用户允许使用按钮后调用) 然而,我所拥有的是一辆非常轻便的马车。它几乎不可用,绝对不可释放 感谢您的帮助。使用UIControlEven

我有一个iOS摄像头应用程序,当用户开始按住一个按钮时,我想开始录制,在这个长镜头方法中,我可以知道当前录制的时间。。。在它结束之前。我想知道视频到现在为止有多长时间,或者换个说法,用户录制了多长时间

我的主要问题是如何知道,在长按方法中,用户已经录制了多长时间。因此,如果录音达到X,它可以做Y

我目前已尝试:

视频录制的fileOutput方法中的以下内容(在用户允许使用按钮后调用)

然而,我所拥有的是一辆非常轻便的马车。它几乎不可用,绝对不可释放


感谢您的帮助。

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

不要看视频。看看钟


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

我们是说只保存1970年的时间(或日期是多少),然后在开始时保存日期(),然后查看日期()并减去。
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")
}
        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")

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