我試圖做一個攝像頭,可以錄製視頻,我需要AVCaptureFileOutputRecordingDelegate使它工作但SOMER原因,它在說...視圖控制器不符合協議AVCaptureFileOutputRecordingDelegate
類型「視圖控制器」不符合協議'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)
}
}
我不知道爲什麼我會得到它或如何解決它。請幫忙。謝謝!
我使用的Xcode 6.4 –