如何从 AVPlayer 录制到新控制器?
How to Segue from a AVPlayer recording to a new Controller?
我的 CameraViewController 中有这个 Delegate。它在完成捕获视频后调用 fileOutput。我想将捕获的视频的 url 传递到故事板中的下一个视图控制器,我在其中显示捕获的文件:
extension CameraViewController: AVCaptureFileOutputRecordingDelegate{
func fileOutput(_ output: AVCaptureFileOutput, didFinishRecordingTo outputFileURL: URL, from connections: [AVCaptureConnection], error: Error?) {
let destinationVC = PreviewViewController()
if (error != nil) {
print("Error recording movie: \(error!.localizedDescription)")
} else {
let videoRecorded = outputURL! as URL
}
outputURL = nil }
override func prepare(for segue: UIStoryboardSegue, sender: Any?){
if segue.identifier == "showCapture_Segue" {
let controller = segue.destination as! CameraPreviewController
controller.movieURL = videoRecorded
}
}
}
这是我现在的代码。 Xcode 有很多投诉,这没有意义,因为我没有将 videoRecorded 从 fileOutput 函数传递到 segue 覆盖函数。
我知道要创建一个 segue 并将数据传递到另一个视图控制器,我会做类似
的事情
override func prepare(for segue: UIStoryboardSegue, sender: Any?){
if segue.identifier == "showCapture_Segue" {
let controller = segue.destination as! CameraPreviewController
controller.movieURL = videoRecorded
}
}
但我不确定这如何与我的 AVCaptureFileOutputRecordingDelegate 集成。我怎样才能做到这一点?谢谢!
编辑:
class CameraViewController: UIViewController {
var outputURL: URL!
func startRecording() {
if movieOutput.isRecording == false {
let connection = movieOutput.connection(with: AVMediaType.video)
if (connection?.isVideoOrientationSupported)! {
connection?.videoOrientation = currentVideoOrientation()
}
if (connection?.isVideoStabilizationSupported)! {
connection?.preferredVideoStabilizationMode = AVCaptureVideoStabilizationMode.auto
}
let device = activeInput.device
if (device.isSmoothAutoFocusSupported) {
do {
try device.lockForConfiguration()
device.isSmoothAutoFocusEnabled = false
device.unlockForConfiguration()
} catch {
print("Error setting configuration: \(error)")
}
}
//EDIT2: And I forgot this
outputURL = tempURL()
movieOutput.startRecording(to: outputURL, recordingDelegate: self)
}
else {
stopRecording()
}
}
}
您应该声明为实例值
class CameraViewController {
var videoRecorded:URL?
}
//
extension CameraViewController: AVCaptureFileOutputRecordingDelegate{
func fileOutput(_ output: AVCaptureFileOutput, didFinishRecordingTo outputFileURL: URL, from connections: [AVCaptureConnection], error: Error?) {
let destinationVC = PreviewViewController()
if (error != nil) {
print("Error recording movie: \(error!.localizedDescription)")
} else {
self.videoRecorded = outputURL as! URL
}
outputURL = nil }
override func prepare(for segue: UIStoryboardSegue, sender: Any?){
if segue.identifier == "showCapture_Segue" {
let controller = segue.destination as! CameraPreviewController
controller.movieURL = self.videoRecorded
}
}
}
//
class CameraPreviewController {
var movieURL:URL?
}
//
self.performSegue(withIdentifier: "showCapture_Segue", sender: nil)
我的 CameraViewController 中有这个 Delegate。它在完成捕获视频后调用 fileOutput。我想将捕获的视频的 url 传递到故事板中的下一个视图控制器,我在其中显示捕获的文件:
extension CameraViewController: AVCaptureFileOutputRecordingDelegate{
func fileOutput(_ output: AVCaptureFileOutput, didFinishRecordingTo outputFileURL: URL, from connections: [AVCaptureConnection], error: Error?) {
let destinationVC = PreviewViewController()
if (error != nil) {
print("Error recording movie: \(error!.localizedDescription)")
} else {
let videoRecorded = outputURL! as URL
}
outputURL = nil }
override func prepare(for segue: UIStoryboardSegue, sender: Any?){
if segue.identifier == "showCapture_Segue" {
let controller = segue.destination as! CameraPreviewController
controller.movieURL = videoRecorded
}
}
}
这是我现在的代码。 Xcode 有很多投诉,这没有意义,因为我没有将 videoRecorded 从 fileOutput 函数传递到 segue 覆盖函数。
我知道要创建一个 segue 并将数据传递到另一个视图控制器,我会做类似
的事情override func prepare(for segue: UIStoryboardSegue, sender: Any?){
if segue.identifier == "showCapture_Segue" {
let controller = segue.destination as! CameraPreviewController
controller.movieURL = videoRecorded
}
}
但我不确定这如何与我的 AVCaptureFileOutputRecordingDelegate 集成。我怎样才能做到这一点?谢谢!
编辑:
class CameraViewController: UIViewController {
var outputURL: URL!
func startRecording() {
if movieOutput.isRecording == false {
let connection = movieOutput.connection(with: AVMediaType.video)
if (connection?.isVideoOrientationSupported)! {
connection?.videoOrientation = currentVideoOrientation()
}
if (connection?.isVideoStabilizationSupported)! {
connection?.preferredVideoStabilizationMode = AVCaptureVideoStabilizationMode.auto
}
let device = activeInput.device
if (device.isSmoothAutoFocusSupported) {
do {
try device.lockForConfiguration()
device.isSmoothAutoFocusEnabled = false
device.unlockForConfiguration()
} catch {
print("Error setting configuration: \(error)")
}
}
//EDIT2: And I forgot this
outputURL = tempURL()
movieOutput.startRecording(to: outputURL, recordingDelegate: self)
}
else {
stopRecording()
}
}
}
您应该声明为实例值
class CameraViewController {
var videoRecorded:URL?
}
//
extension CameraViewController: AVCaptureFileOutputRecordingDelegate{
func fileOutput(_ output: AVCaptureFileOutput, didFinishRecordingTo outputFileURL: URL, from connections: [AVCaptureConnection], error: Error?) {
let destinationVC = PreviewViewController()
if (error != nil) {
print("Error recording movie: \(error!.localizedDescription)")
} else {
self.videoRecorded = outputURL as! URL
}
outputURL = nil }
override func prepare(for segue: UIStoryboardSegue, sender: Any?){
if segue.identifier == "showCapture_Segue" {
let controller = segue.destination as! CameraPreviewController
controller.movieURL = self.videoRecorded
}
}
}
//
class CameraPreviewController {
var movieURL:URL?
}
//
self.performSegue(withIdentifier: "showCapture_Segue", sender: nil)