簡體   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