Wie kann ich in Swift ein Video mit avfoundation aufnehmen?
Ich versuche herauszufinden, wie ich mit AVFoundation in Swift ein Video aufnehme. Ich habe es bis zur Erstellung einer benutzerdefinierten Kamera geschafft, aber ich habe nur herausgefunden, wie man Standbilder damit aufnimmt, und ich kann nicht herausfinden, wie man Videos aufnimmt. Soweit ich weiß, müssen Sie @ verwendeAVCaptureVideoDataOutput
, um die Daten aus der Aufzeichnung abzurufen, aber ich kann nicht herausfinden, wie die Aufzeichnung gestartet und die Delegierungsmethoden implementiert werden.
Die gesamte AVFoundation-Programmieranleitung / Standbild- und Videomedienerfassung befindet sich in Objective-C, und ich kann sie anscheinend nicht entziffern. Hier ist mein Versuch, diese Aufgabe zu erfüllen:
Zunächst habe ich die Kamera- / Aufnahmesitzung eingerichtet
override func viewDidLoad() {
super.viewDidLoad()
captureSession.sessionPreset = AVCaptureSessionPresetHigh
let devices = AVCaptureDevice.devices()
for device in devices {
if (device.hasMediaType(AVMediaTypeVideo)) {
if(device.position == AVCaptureDevicePosition.Back) {
captureDevice = device as? AVCaptureDevice
if captureDevice != nil {
beginSession()
}
}
}
}
}
Wenn beginSession () aufgerufen wird, richte ich den Live-Feed ein
func beginSession() {
var err : NSError? = nil
captureSession.addInput(AVCaptureDeviceInput(device: captureDevice, error: &err))
if err != nil {
println("error: \(err?.localizedDescription)")
}
previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
self.cameraView.layer.addSublayer(previewLayer)
self.cameraView.bringSubviewToFront(takePhotoButton)
self.cameraView.bringSubviewToFront(self.snappedPicture)
self.cameraView.bringSubviewToFront(self.backButton)
previewLayer?.frame = self.cameraView.layer.frame
captureSession.startRunning()
}
Hier stecke ich fest, wo der Benutzer auf Aufnahme drückt, um tatsächlich ein Video aufzunehmen und aufzunehmen:
@IBAction func takeVideoAction(sender: AnyObject) {
var recordingDelegate:AVCaptureFileOutputRecordingDelegate? = self
var videoFileOutput = AVCaptureMovieFileOutput()
self.captureSession.addOutput(videoFileOutput)
let filePath = NSURL(fileURLWithPath: "filePath")
videoFileOutput.startRecordingToOutputFileURL(filePath, recordingDelegate: recordingDelegate)
}
Dann rufe ichself.videoFileOutput.stopRecording()
.
Dann zuletzt die Delegate-Methoden
func captureOutput(captureOutput: AVCaptureFileOutput!, didFinishRecordingToOutputFileAtURL outputFileURL: NSURL!, fromConnections connections: [AnyObject]!, error: NSError!) {
return
}
func captureOutput(captureOutput: AVCaptureFileOutput!, didStartRecordingToOutputFileAtURL fileURL: NSURL!, fromConnections connections: [AnyObject]!) {
return
}
Und nichts passiert, wie Sie sich vorstellen können, weil ich nicht herausfinden kann, wie ich es richtig implementieren soll.