gpt4 book ai didi

ios - View Controller 不符合协议(protocol) AVCaptureFileOutputRecordingDelegate

转载 作者:行者123 更新时间:2023-11-29 01:33:31 25 4
gpt4 key购买 nike

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

类型“ViewController”不符合协议(protocol)“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 不符合协议(protocol),这就是您收到警告的原因。它看起来像 VideoDelegate,而不是 ViewController

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

关于ios - View Controller 不符合协议(protocol) AVCaptureFileOutputRecordingDelegate,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33206062/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com