2017-12-02 9 views
-1

私はプログラミングとSwiftを学ぶのが初めてです。 カメラを使用するアプリケーションを作成していますが、フロントカメラに切り替えようとするとエラーが発生しますスレッド1w:EXC_BREAKPOINTビデオを録画しようとしました。スウィフト3でカメラを交換するときに記録エラーが発生する

エラーは、私はちょっとこのエラーで私のプログラムで立ち往生ライン

if (connection?.isVideoOrientationSupported)!{connection?.videoOrientation = currentVideoOrientation()} 

でFUNC startRecording()で発生します。 plsヘルプ!

enum CameraType { 
    case front 
    case back 
} 

var camera = CameraType.front 

func switchCamera() -> Bool { 

    captureSession.stopRunning() 
    previewLayer?.removeFromSuperlayer() 

    captureSession = AVCaptureSession() 

    captureSession.sessionPreset = AVCaptureSessionPresetHigh 

    var captureDevice:AVCaptureDevice! = AVCaptureDevice.defaultDevice(withDeviceType: .builtInWideAngleCamera, mediaType: AVMediaTypeVideo, position: .front) 


    if (camera == CameraType.front) { 
     captureDevice = AVCaptureDevice.defaultDevice(withDeviceType: .builtInWideAngleCamera, mediaType: AVMediaTypeVideo, position: .front) 
     camera = CameraType.back 
    } else { 
     captureDevice = AVCaptureDevice.defaultDevice(withMediaType: AVMediaTypeVideo) 
     camera = CameraType.front 
    } 

    let input = try? AVCaptureDeviceInput(device: captureDevice) 
    if captureSession.canAddInput(input) { 
     captureSession.addInput(input) 
     activeInput = input 
    } 


    if captureSession.canAddOutput(movieOutput) { 
     captureSession.addOutput(movieOutput) 
    } 

    print("Abidi") 

    return true 

} 

func reloadCamera() { 

    if switchCamera() { 
     setupPreview() 
     startSession() 
    } 

    view.addSubview(dismissButton) 
    dismissButton.anchor(top: view.topAnchor, left: nil, bottom: nil, right: view.rightAnchor, paddingTop: 12, paddingLeft: 0, paddingBottom: 0, paddingRight: 12, width: 50, height: 50) 

    view.addSubview(frontCameraButton) 
    frontCameraButton.anchor(top: view.topAnchor, left: view.leftAnchor, bottom: nil, right: nil, paddingTop: 12, paddingLeft: 12, paddingBottom: 0, paddingRight: 0, width: 35, height: 35) 


} 

func viewDidAppear(animated: Bool) { 

    reloadCamera() 

} 

func startCapture() { 

    startRecording() 

} 


func tempURL() -> URL? { 
    let directory = NSTemporaryDirectory() as NSString 

    if directory != "" { 
     let path = directory.appendingPathComponent(NSUUID().uuidString + ".mp4") 
     return URL(fileURLWithPath: path) 
    } 

    return nil 
} 


func startRecording() { 

    if movieOutput.isRecording == false { 

     let connection = movieOutput.connection(withMediaType: AVMediaTypeVideo) 
     if (connection?.isVideoOrientationSupported)! { 
      connection?.videoOrientation = currentVideoOrientation() 
     } 

     if (connection?.isVideoStabilizationSupported)! { 
      connection?.preferredVideoStabilizationMode = AVCaptureVideoStabilizationMode.auto 
     } 

     let device = activeInput.device 
     if (device?.isSmoothAutoFocusSupported)! { 
      do { 
       try device?.lockForConfiguration() 
       device?.isSmoothAutoFocusEnabled = false 
       device?.unlockForConfiguration() 
      } catch { 
       print("Error setting configuration: \(error)") 
      } 

     } 

     outputURL = tempURL() 
     movieOutput.startRecording(toOutputFileURL: outputURL, recordingDelegate: self) 

    } 
    else { 
     stopRecording() 
    } 

} 

func stopRecording() { 

    if movieOutput.isRecording == true { 
     movieOutput.stopRecording() 
    } 
} 

答えて

0

強制アンラッピングを使用しないでください。代わりに、真のためisVideoOritstionSupportedをチェック:

接続あなたは非常に多くの.isVideoOrientationSupported ==真

+0

感謝を!私のプログラムは現在働いています。ほんとうにありがとう。 – elrafael16

関連する問題