简体   繁体   English

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

[英]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 .我有一个 iOS 相机应用程序,当用户开始按住按钮时,我想开始录制,并且在该 longTap 方法中能够知道当前录制的时间......在它结束之前 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.这样如果录音达到了 X,它就可以做 Y。

I have currently tried:我目前尝试过:

The following in the fileOutput method for video recording (called after the user has let for of the button)用于视频录制的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)
            }
        }
    }

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.你会看到一个 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 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.谢谢你的帮助。

Use the UIControlEvents.touchDown & UIControlEvents.touchUpInside events.使用UIControlEvents.touchDownUIControlEvents.touchUpInside事件。 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()

Don't look at the video.不要看视频。 Look at the clock.看看时钟。

You know what time the recording started, because you started it.你知道录音开始的时间,因为你开始了。 You know what time it is now.你知道现在几点了。 Subtract.减去。

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

相关问题 如何使用Replaykit作为文件访问录制的视频并将其合并到另一个视频? - How to access the video recorded using Replaykit as a file and merge another video to it? 如何修剪用EZAudioRecorder录制的音频? - How can I trim an audio recorded with EZAudioRecorder? 如何查看我的GPUImage视频是否录制在iOS中? - How do I see if my GPUImage Video was recorded in iOS? 如何防止使用AVFoundation录制的视频中断当前正在播放的任何全局音频(Swift)? - How can I prevent video recording with AVFoundation from interrupting any global audio that is currently playing (Swift)? 我如何在tabBar Button按下时有一个调用函数,但仅当视图当前处于按下状态时才可以? - How can I have a function called on tabBar Button press but only if the view is currently the one being pressed? 无法播放录制的视频-iOS - Can't Play Recorded Video - iOS 如何使用本地通知播放录制的声音? - How can I play a recorded sound using a local notification? 如何在 iphone 中获取录制音频的时间? - How can i get time of my recorded audio in iphone? 如何检查当前是否正在显示 UIViewController? - How do I check if an UIViewController is currently being displayed? 如何通过ImagePickerViewController为录制的视频分配资源的url? - How to asset url for a recorded video by ImagePickerViewController?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM