iosiphoneswiftavcapturesessionavcapturemoviefileoutput

View Controller Doesn't Conform To Protocol AVCaptureFileOutputRecordingDelegate


I'm trying to make a camera that can record video, and I need AVCaptureFileOutputRecordingDelegate to make it work but for somer reason it's saying...

Type 'ViewController' does not conform to protocol 'AVCaptureFileOutputRecordingDelegate'.

I have this above my class title, but still doesn't fix the problem...

class VideoDelegate : NSObject, AVCaptureFileOutputRecordingDelegate {
func captureOutput(captureOutput: AVCaptureFileOutput!, didFinishRecordingToOutputFileAtURL outputFileURL: NSURL!, fromConnections connections: [AnyObject]!, error: NSError!) {
    println("capture output : finish recording to \(outputFileURL)")
}
func captureOutput(captureOutput: AVCaptureFileOutput!, didStartRecordingToOutputFileAtURL fileURL: NSURL!, fromConnections connections: [AnyObject]!) {
    println("capture output: started recording to \(fileURL)")

    }
}

Here's all my code:

import UIKit
import MediaPlayer
import MobileCoreServices
import AVFoundation

class VideoDelegate : NSObject, AVCaptureFileOutputRecordingDelegate {
func captureOutput(captureOutput: AVCaptureFileOutput!, didFinishRecordingToOutputFileAtURL outputFileURL: NSURL!, fromConnections connections: [AnyObject]!, error: NSError!) {
    println("capture output : finish recording to \(outputFileURL)")
}

func captureOutput(captureOutput: AVCaptureFileOutput!, didStartRecordingToOutputFileAtURL fileURL: NSURL!, fromConnections connections: [AnyObject]!) {
    println("capture output: started recording to \(fileURL)")

    }
}

class ViewController: UIViewController, AVCaptureVideoDataOutputSampleBufferDelegate, AVCaptureFileOutputRecordingDelegate {

var previewLayer : AVCaptureVideoPreviewLayer?
var captureDevice : AVCaptureDevice?
var videoCaptureOutput = AVCaptureVideoDataOutput()
let captureSession = AVCaptureSession()    // var videoCaptureOutputF = AVCaptureFileOutput()

override func viewDidLoad() {
    super.viewDidLoad()
    captureSession.sessionPreset = AVCaptureSessionPreset640x480
    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()
                }

            }

        }

    }

}

func beginSession() {
    var err : NSError? = nil
    captureSession.addInput(AVCaptureDeviceInput(device: captureDevice, error: &err))
    if err != nil {
        println("Error: \(err?.localizedDescription)")
    }
    videoCaptureOutput.videoSettings = [kCVPixelBufferPixelFormatTypeKey:kCVPixelFormatType_32BGRA]
    //videoCaptureOutput.sampleBufferDelegate=self
    videoCaptureOutput.alwaysDiscardsLateVideoFrames = true
    captureSession.addOutput(videoCaptureOutput)
    previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
    self.view.layer.addSublayer(previewLayer)
    previewLayer?.frame = CGRectMake(0, 20, self.view.bounds.width,  self.view.bounds.height)
    var startVideoBtn = UIButton(frame: CGRectMake(10,40, 40, 40))
    startVideoBtn.backgroundColor=UIColor.greenColor()
    startVideoBtn.addTarget(self, action: "startVideoRecording", forControlEvents:
        UIControlEvents.TouchUpInside)
    self.view.addSubview(startVideoBtn)
    var stopVideoBtn = UIButton(frame: CGRectMake(200, 40, 40, 40))
    stopVideoBtn.backgroundColor=UIColor.redColor()
    stopVideoBtn.addTarget(self, action: "stopVideoRecording", forControlEvents: UIControlEvents.TouchUpInside)
    self.view.addSubview(stopVideoBtn)
}
func startVideoRecording(){
    captureSession.startRunning()
}
func stopVideoRecording(){
    captureSession.stopRunning()
    print(videoCaptureOutput)

    // here i am getting problems that how to save recorded   video
    let videoDelegate = VideoDelegate()
    let fileOutput = AVCaptureMovieFileOutput()
    // captureSession.addOutput(videoCaptureOutput)
    let documentsPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as! NSString
    let outputPath = "\(documentsPath)/output.mp4"
    let outputFileUrl = NSURL(fileURLWithPath: outputPath)
    fileOutput.startRecordingToOutputFileURL(outputFileUrl, recordingDelegate: videoDelegate)

    }
}

I'm not sure why I'm getting it or how to fix it. Please help. Thanks!


Solution

  • ViewController doesn't conform to the protocol, which is why you're getting the warning. It looks like VideoDelegate does, not ViewController.

    To resolve, either move that stuff to ViewController, or set your capture delegate to an instance of VideoDelegate instead of ViewController.