2015-10-19 37 views
0

我試圖做一個攝像頭,可以錄製視頻,我需要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) 

    } 
} 

我不知道爲什麼我會得到它或如何解決它。請幫忙。謝謝!

+0

我使用的Xcode 6.4 –

回答

1

ViewController不符合協議,這就是爲什麼你會收到警告。它看起來像VideoDelegate,而不是ViewController

要解決此問題,請將這些內容移至ViewController,或將您的捕獲代理設置爲VideoDelegate而不是ViewController的實例。

+0

我編輯我的職務所以這是我的所有代碼。你能告訴我你的意思嗎?因爲我對我應該做的事情感到困惑? –

+0

'ViewController'不實現您指定的協議所需的方法。你有希望在'ViewController'類中實現這個方法,然後做不到。這就是編譯器抱怨的原因。 –

0

我有完全相同的問題,如果我找到了方法,我會把答案..我看看你的代碼nd你需要從你的ViewController中刪除「AVCaptureVideoDataOutputSampleBufferDelegate」和「AVCaptureFileOutputRecordingDelegate」,this是因爲你沒有實現每個代表的方法。

你的videoDelegate類沒問題..那裏,你正在實現AVCaptureFileOutputRecordingDelegate的方法。所以

class ViewController: UIViewController 

代替

class ViewController: UIViewController, AVCaptureVideoDataOutputSampleBufferDelegate, AVCaptureFileOutputRecordingDelegate 
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) 
    } 
} 
相關問題