如何访问当前正在录制的视频的长度
Posted
技术标签:
【中文标题】如何访问当前正在录制的视频的长度【英文标题】:How can I access the length of the video that is CURRENTLY being recorded 【发布时间】:2018-12-26 22:04:27 【问题描述】:我有一个 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")
然而,我所拥有的是非常错误的。它几乎无法使用,绝对无法释放。
感谢您的帮助。
【问题讨论】:
【参考方案1】:使用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()
【讨论】:
【参考方案2】:不要看视频。看看时钟。
您知道录音是什么时候开始的,因为是您开始的。你知道现在几点了。减去。
【讨论】:
我们是说从 1970 年(或任何日期)开始节省时间,然后在 .ended 处进行罚款 刚开始的时候保存Date(),以后再看Date()再减去。以上是关于如何访问当前正在录制的视频的长度的主要内容,如果未能解决你的问题,请参考以下文章
使用 HTML5 和 Javascript 录制/上传带有进度条的固定长度视频