[英]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... 我正在尝试制作一个可以录制视频的相机,并且我需要AVCaptureFileOutputRecordingDelegate使其正常工作,但是出于某种原因,这是在说...
Type 'ViewController' does not conform to protocol 'AVCaptureFileOutputRecordingDelegate'. 类型“ ViewController”不符合协议“ 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!
谢谢!
ViewController
doesn't conform to the protocol, which is why you're getting the warning. ViewController
不符合协议,这就是收到警告的原因。 It looks like VideoDelegate
does, not ViewController
. 看起来就像
VideoDelegate
一样,而不是ViewController
。
To resolve, either move that stuff to ViewController
, or set your capture delegate to an instance of VideoDelegate
instead of ViewController
. 要解决此问题,请将这些内容移至
ViewController
,或将捕获委托设置为VideoDelegate
的实例而不是ViewController
。
Im having the exact same issue and if I find out the way, I'll put the answer.. I took a look inside your code nd you need to remove "AVCaptureVideoDataOutputSampleBufferDelegate" and "AVCaptureFileOutputRecordingDelegate" from you ViewController, this is because you are not implementing the methods of each delegate. 我有完全相同的问题,如果我找到解决办法,我将给出答案..我在您的代码内看了一遍,您需要从ViewController中删除“ AVCaptureVideoDataOutputSampleBufferDelegate”和“ AVCaptureFileOutputRecordingDelegate”,这是因为您是没有实现每个委托的方法。
Your videoDelegate class is ok.. there, you are implementing correctly the methods of AVCaptureFileOutputRecordingDelegate. 您的videoDelegate类还可以..在那里,您正在正确实现AVCaptureFileOutputRecordingDelegate的方法。
so 所以
class ViewController: UIViewController
instead of 代替
class ViewController: UIViewController, AVCaptureVideoDataOutputSampleBufferDelegate, AVCaptureFileOutputRecordingDelegate
It's because you haven't included the compulsory function that you need to include when you call this protocol. 这是因为您没有包括调用此协议时需要包括的强制功能。 Add these functions at the bottom of your code(Swift 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)
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.