2016-10-10 10 views
3

私はSwiftでカスタムカメラを作成しようとしています。私はこれに関する多くの記事を見つけて、カメラを開き、「録画を開始」ボタンを持っています。このエラーを検索すると、私がbeginSession機能でやったカメラの品質を設定しないことによって引き起こされることになっていますAVFoundation: "アクティブでない/有効になっている接続"のために録画が開始されない

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[AVCaptureMovieFileOutput startRecordingToOutputFileURL:recordingDelegate:] No active/enabled connections' 

:ただし、「録画開始」ボタンをクリックしたときに、私はこのエラーを持っています:ここ

func beginSession(captureDevice: AVCaptureDevice) { 
     var err : NSError? = nil 

     var input = AVCaptureDeviceInput?() 
     do { 
      input = try AVCaptureDeviceInput(device: captureDevice) 
     } catch _ { 
      //Error handling, if needed 
     } 
     captureSession.addInput(input) 
     captureSession.sessionPreset = AVCaptureSessionPresetHigh; 

     if err != nil { 
      print("error: \(err?.localizedDescription)") 
     } 

     previewLayer = AVCaptureVideoPreviewLayer(session: captureSession) 
     self.view.layer.addSublayer(previewLayer!) 
     previewLayer?.frame = self.view.layer.frame 
     captureSession.startRunning() 
     let btn: UIButton = UIButton(frame: CGRectMake((self.view.frame.size.width-350)/2, (self.view.frame.size.height-80), 350, 70)) 
     btn.backgroundColor = hexStringToUIColor("#E53935") 
     btn.setTitle("Start Recording", forState: UIControlState.Normal) 
     btn.addTarget(self, action: "takeVideoAction", forControlEvents: UIControlEvents.TouchUpInside) 
     btn.tag = 1    // change tag property 
     self.view.addSubview(btn) 

    } 

は完全なコードである:

class ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate, CLLocationManagerDelegate,AVCaptureFileOutputRecordingDelegate { 


    let imagePicker: UIImagePickerController! = UIImagePickerController() 
    let saveFileName = "/test.mp4" 
    var locationManager : CLLocationManager! = CLLocationManager() 
    var startLocation: CLLocation! 
    //let captureSession = AVCaptureSession() 
    var stillImageOutput: AVCaptureStillImageOutput? 
    var previewLayer: AVCaptureVideoPreviewLayer? 
    let captureSession = AVCaptureSession() 

    // MARK: ViewController methods 
    override func viewDidLoad() { 
     super.viewDidLoad() 
     //var timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: #selector(ViewController.update), userInfo: nil, repeats: true) 
     // Do any additional setup after loading the view, typically from a nib. 
     let btn: UIButton = UIButton(frame: CGRectMake((self.view.frame.size.width-350)/2, (self.view.frame.size.height-80), 350, 70)) 
     btn.backgroundColor = hexStringToUIColor("#E53935") 
     btn.setTitle("Go to recording", forState: UIControlState.Normal) 
     btn.addTarget(self, action: "recordVideo:", forControlEvents: UIControlEvents.TouchUpInside) 
     btn.tag = 1    // change tag property 
     self.view.addSubview(btn) // add to view as subview 
     if CLLocationManager.locationServicesEnabled() { 
      locationManager.requestAlwaysAuthorization() 
      locationManager.requestWhenInUseAuthorization() 
      locationManager.delegate = self 
      locationManager.desiredAccuracy = kCLLocationAccuracyBest 
      locationManager.startUpdatingLocation() 
      print(locationManager.location) 
     } 

    } 


    func startCameraFromViewController(viewController: UIViewController, withDelegate delegate: protocol<UIImagePickerControllerDelegate, UINavigationControllerDelegate>) -> Bool { 
     if UIImagePickerController.isSourceTypeAvailable(.Camera) == false { 
      return false 
     } 

     var cameraController = UIImagePickerController() 
     cameraController.sourceType = .Camera 
     cameraController.mediaTypes = [kUTTypeMovie as NSString as String] 
     cameraController.allowsEditing = false 
     cameraController.delegate = delegate 

     presentViewController(cameraController, animated: true, completion: nil) 
     return true 
    } 

    func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) { 
     let mediaType = info[UIImagePickerControllerMediaType] as! NSString 
     dismissViewControllerAnimated(true, completion: nil) 
     // Handle a movie capture 
     if mediaType == kUTTypeMovie { 
      guard let path = (info[UIImagePickerControllerMediaURL] as! NSURL).path else { return } 
      if UIVideoAtPathIsCompatibleWithSavedPhotosAlbum(path) { 
       UISaveVideoAtPathToSavedPhotosAlbum(path, self, #selector(ViewController.video(_:didFinishSavingWithError:contextInfo:)), nil) 
      } 
     } 
    } 

    func video(videoPath: NSString, didFinishSavingWithError error: NSError?, contextInfo info: AnyObject) { 
     var title = "Success" 
     var message = "Video was saved" 
     if let _ = error { 
      title = "Error" 
      message = "Video failed to save" 
     } 
     let alert = UIAlertController(title: title, message: message, preferredStyle: .Alert) 
     alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Cancel, handler: nil)) 
     presentViewController(alert, animated: true, completion: nil) 
    } 

    // MARK: Button handlers 
    // Record a video 
    @IBAction func recordVideo(sender: AnyObject) { 
     let devices = AVCaptureDevice.devices() 
     captureSession.sessionPreset = AVCaptureSessionPresetHigh 
     // captureSession.sessionPreset = kCaptureSessionPresetVideo 
     for device in devices { 
      // Make sure this particular device supports video 
      if (device.hasMediaType(AVMediaTypeVideo)) { 
       // Finally check the position and confirm we've got the back camera 
       if(device.position == AVCaptureDevicePosition.Back) { 
        var captureDevice = device as? AVCaptureDevice 
        if captureDevice != nil { 
         beginSession(captureDevice!) 
        } 
       } 
      } 
     } 
    } 

    func beginSession(captureDevice: AVCaptureDevice) { 
     var err : NSError? = nil 

     var input = AVCaptureDeviceInput?() 
     do { 
      input = try AVCaptureDeviceInput(device: captureDevice) 
     } catch _ { 
      //Error handling, if needed 
     } 
     captureSession.addInput(input) 
     captureSession.sessionPreset = AVCaptureSessionPresetHigh; 

     if err != nil { 
      print("error: \(err?.localizedDescription)") 
     } 

     previewLayer = AVCaptureVideoPreviewLayer(session: captureSession) 
     self.view.layer.addSublayer(previewLayer!) 
     previewLayer?.frame = self.view.layer.frame 
     captureSession.startRunning() 
     let btn: UIButton = UIButton(frame: CGRectMake((self.view.frame.size.width-350)/2, (self.view.frame.size.height-80), 350, 70)) 
     btn.backgroundColor = hexStringToUIColor("#E53935") 
     btn.setTitle("Start Recording", forState: UIControlState.Normal) 
     btn.addTarget(self, action: "takeVideoAction", forControlEvents: UIControlEvents.TouchUpInside) 
     btn.tag = 1    // change tag property 
     self.view.addSubview(btn) 

    } 

    func takeVideoAction() { 
     var fileName = "mysavefile.mp4"; 
     let documentsURL = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)[0] 
     var filePath = documentsURL.URLByAppendingPathComponent(fileName) 

     let videoFileOutput = AVCaptureMovieFileOutput() 

     let recordingDelegate:AVCaptureFileOutputRecordingDelegate? = self 
     videoFileOutput.startRecordingToOutputFileURL(filePath, recordingDelegate: recordingDelegate) 
     self.captureSession.addOutput(videoFileOutput) 
     startCameraFromViewController(self, withDelegate: self) 
    } 

    func captureOutput(captureOutput: AVCaptureFileOutput!, didFinishRecordingToOutputFileAtURL outputFileURL: NSURL!, fromConnections connections: [AnyObject]!, error: NSError!) { 
     return 
    } 

    func captureOutput(captureOutput: AVCaptureFileOutput!, didStartRecordingToOutputFileAtURL fileURL: NSURL!, fromConnections connections: [AnyObject]!) { 
     return 
    } 

} 

任意の入力がはるか

を理解されるであろう0

答えて

3

はあなたがstartRecordingToOutputFileURLを呼び出す前に、キャプチャセッションにビデオファイル出力を追加する必要があります。

self.captureSession.addOutput(videoFileOutput) 
videoFileOutput.startRecordingToOutputFileURL(filePath, recordingDelegate: recordingDelegate) 
+0

私はまさにそれを行っているが、私はまだエラーを取得します。プリセットを設定せずにビデオを録画する方法を知っていますか?場合によっては、Appleが示唆するように、activeFormatを直接設定する必要があります。 –

+0

新しい質問を表示して、いくつかのコードを表示することができますか?それとも、実行可能コードにリンクすることができますか? –

+0

https://stackoverflow.com/questions/46160407/how-do-i-record-a-video-on-ios-without-using-a-presetここに行きます。 –

関連する問題