视图控制器不符合协议 AVCaptureFileOutputRecordingDelegate

View Controller Doesn't Conform To Protocol AVCaptureFileOutputRecordingDelegate

我正在尝试制作一个可以录制视频的相机,我需要 AVCaptureFileOutputRecordingDelegate 才能使其正常工作,但出于某种原因它说...

类型 'ViewController' 不符合协议 'AVCaptureFileOutputRecordingDelegate'。

我的 class 标题上面有这个,但仍然没有解决问题...

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)")

    }
}

这是我的全部代码:

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)

    }
}

我不确定为什么会收到它或如何修复它。请帮忙。谢谢!

ViewController 不符合协议,这就是您收到警告的原因。看起来像 VideoDelegate,而不是 ViewController

要解决,请将这些内容移至 ViewController,或将您的捕获委托设置为 VideoDelegate 的实例而不是 ViewController

我遇到了完全相同的问题,如果我找到了方法,我会给出答案。我查看了你的代码,你需要删除 "AVCaptureVideoDataOutputSampleBufferDelegate" 和 "AVCaptureFileOutputRecordingDelegate"来自您 ViewController,这是因为您没有实现每个委托的方法。

您的 videoDelegate class 没问题.. 您正在正确实现 AVCaptureFileOutputRecordingDelegate 的方法。

所以

class ViewController: UIViewController

而不是

class ViewController: UIViewController, AVCaptureVideoDataOutputSampleBufferDelegate, AVCaptureFileOutputRecordingDelegate 

是因为你没有包含调用这个协议时需要包含的强制函数。在代码底部添加这些函数(Swift 3):

func capture(_ captureOutput: AVCaptureFileOutput!, didStartRecordingToOutputFileAt fileURL: URL!, fromConnections connections: [Any]!) {
    //Show User recording has started
}

func capture(_ captureOutput: AVCaptureFileOutput!, didFinishRecordingToOutputFileAt outputFileURL: URL!, fromConnections connections: [Any]!, error: Error!) {
    print("Finished \(error)")
    //Save Video to Camera Roll
    if error == nil{
        UISaveVideoAtPathToSavedPhotosAlbum(outputFileURL.path, nil, nil, nil)
    }
}