繁体   English   中英

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

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

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

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

我目前尝试过:

用于视频录制的fileOutput方法中的以下内容(在用户让for按钮后调用)

{

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

长按方法

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

不要看视频。 看看时钟。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM