单击按钮打开记录器

open recorder with button click

我有按钮并想打开录像机,但我的代码只能打开照片捕获有什么简单的解决方案吗?

    @IBAction func uploadVideoButton(_ sender: UIButton) {
        let vc = UIImagePickerController()
        vc.sourceType = .camera
        vc.allowsEditing = true
        vc.delegate = self
        present(vc, animated: true)
    }

你需要告诉选择器你想要什么样的媒体。

vc.sourceType = .camera
vc.mediaTypes = [kUTTypeMovie as String]

记得也导入 MobileCoreServices。

刚刚快速对此进行了测试,kUTTypeMovie 已被弃用,因此您应该查看 UTTypeMovie

编译器快乐代码:

import UniformTypeIdentifiers

 func openCamera() {
    if UIImagePickerController.isSourceTypeAvailable(.camera) {
        let picker = UIImagePickerController()
        picker.allowsEditing = true
        picker.delegate = self
        picker.sourceType = .camera
        picker.showsCameraControls = true
        picker.mediaTypes = [UTType.movie.identifier]
        present(picker, animated: true)
    }
}