视图控制器不符合协议AVCaptureFileOutputRecordingDelegate

时间:2015-10-19 03:11:57

标签: ios iphone swift avcapturesession avcapturemoviefileoutput

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

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

我在课程标题之上有这个,但仍然没有解决问题......

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)

    }
}

我不知道为什么我会得到它或如何解决它。请帮忙。谢谢!

3 个答案:

答案 0 :(得分:1)

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

要解决此问题,请将这些内容移至ViewController,或将捕获代理设置为VideoDelegate而非ViewController的实例。

答案 1 :(得分:0)

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

你的videoDelegate类没关系..你正在正确实现AVCaptureFileOutputRecordingDelegate的方法。

所以

class ViewController: UIViewController

而不是

class ViewController: UIViewController, AVCaptureVideoDataOutputSampleBufferDelegate, AVCaptureFileOutputRecordingDelegate 

答案 2 :(得分:0)

这是因为您还没有包含调用此协议时需要包含的必修功能。在代码底部添加这些函数(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)
    }
}