【问题标题】:View Controller Doesn't Conform To Protocol AVCaptureFileOutputRecordingDelegate视图控制器不符合协议 AVCaptureFileOutputRecordingDelegate
【发布时间】:2015-10-19 03:11:57
【问题描述】:

我正在尝试制作一个可以录制视频的相机,我需要 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)

    }
}

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

【问题讨论】:

    标签: ios iphone swift avcapturesession avcapturemoviefileoutput


    【解决方案1】:

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

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

    【讨论】:

    • 我编辑了我的帖子,所以这是我的所有代码。你能告诉我你的意思吗,因为我对我应该做什么有点困惑?
    • ViewController 没有实现您指定的协议所需的方法。您承诺在 ViewController 类中实现该方法,然后没有这样做。这就是编译器抱怨的原因。
    【解决方案2】:

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

    你的 videoDelegate 类没问题.. 在那里,你正确地实现了 AVCaptureFileOutputRecordingDelegate 的方法。

    所以

    class ViewController: UIViewController
    

    而不是

    class ViewController: UIViewController, AVCaptureVideoDataOutputSampleBufferDelegate, AVCaptureFileOutputRecordingDelegate 
    

    【讨论】:

      【解决方案3】:

      这是因为你没有包含你调用这个协议时需要包含的强制函数。在代码底部添加这些函数(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)
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多