2017-01-31 7 views
0

私はカスタムカメラでビデオを録画していますが、もう1つ多くです。私は再びビデオを録画したいときは何も起こりません。どのように私はこれを修正します。私はviewWillAppear()cameraConfigration()メソッドをロードしています。また、録音開始録音のためにrecordVideo()メソッドを呼び出し、停止コールStoprecording()メソッドのためにロードしています。ここに私のコードです。スウィフト3:AVFoundationを介してビデオを再レコード

func stopRecording() { 
 
     sessionOutput.stopRecording() 
 
     captureSession.stopRunning() 
 
     previewLayer.removeFromSuperlayer() 
 
     
 
    } 
 
    func recordVideo(){ 
 
     // custom camera 
 
     let paths = NSTemporaryDirectory() 
 
     let outputFile = paths.appending("t\(Timestamp).MOV") 
 
     let outputURL = NSURL(fileURLWithPath:outputFile) 
 
     
 
     sessionOutput.startRecording(toOutputFileURL: outputURL as URL!, recordingDelegate: self) 
 
    } 
 
func cameraConfigration(){ 
 
     let deviceDiscoverySession = AVCaptureDeviceDiscoverySession(deviceTypes: [AVCaptureDeviceType.builtInDuoCamera, AVCaptureDeviceType.builtInTelephotoCamera,AVCaptureDeviceType.builtInWideAngleCamera], mediaType: AVMediaTypeVideo, position: AVCaptureDevicePosition.unspecified) 
 
     for device in (deviceDiscoverySession?.devices)! { 
 
      if(device.position == AVCaptureDevicePosition.back){ 
 
       do{ 
 
        let input = try AVCaptureDeviceInput(device: device) 
 
        if(captureSession.canAddInput(input)){ 
 
         captureSession.addInput(input); 
 
         
 
         if(captureSession.canAddOutput(sessionOutput)){ 
 
          captureSession.addOutput(sessionOutput); 
 
          previewLayer = AVCaptureVideoPreviewLayer(session: captureSession); 
 
          previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill; 
 
          previewLayer.connection.videoOrientation = AVCaptureVideoOrientation.portrait; 
 
          cameraPreview.layer.addSublayer(previewLayer); 
 
          
 
         } 
 
         captureSession.startRunning() 
 
        } 
 
       } 
 
       catch{ 
 
        print("exception!"); 
 
       } 
 
      } 
 
     } 
 
    } 
 
    func startCameraFromViewController(viewController: UIViewController, withDelegate delegate: UIImagePickerControllerDelegate & UINavigationControllerDelegate) -> Bool { 
 
     if UIImagePickerController.isSourceTypeAvailable(.camera) == false { 
 
      return false 
 
     } 
 
     let cameraController = UIImagePickerController() 
 
     cameraController.sourceType = .camera 
 
     cameraController.mediaTypes = [kUTTypeMovie as NSString as String] 
 
     cameraController.allowsEditing = false 
 
     cameraController.delegate = delegate 
 
     
 
     present(cameraController, animated: true, completion: nil) 
 
     return true 
 
    }

override func viewDidAppear(_ animated: Bool) { 
 
     cameraConfigration() 
 
    }

``

+0

あなたの質問に答えられたら、答えを受け入れることができますか? –

答えて

3

問題がstopRecording()機能であり、この機能では、あなたは録音を停止し、削除previewLayerそのスーパーpreviewLayer.removeFromSuperlayer()を形成し、再びy録音を開始しようとしましたpreviewLayerが見つからない場合、コントローラーはpreviewLayerを見つけられず、何も起こっていません。

previewLayer.removeFromSuperlayer()にコメントを投稿するか、previewLayerstartRecording()機能に追加してみてください。

previewLayer = AVCaptureVideoPreviewLayer(session: captureSession); 
previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill; 
previewLayer.connection.videoOrientation = AVCaptureVideoOrientation.portrait; 
cameraPreview.layer.addSublayer(previewLayer); 

録音を開始する前に。この行の前のsessionOutput.startRecording(toOutputFileURL: outputURL as URL!, recordingDelegate: self)

+0

ありがとう、これは正常に動作しています。 –

+0

あなたの歓迎:) –

関連する問題