前提・実現したいこと
swiftでフロントカメラでビデオ撮るだけのアプリを作ろうとしています
発生している問題・エラーメッセージ
参考ホームページからソースコードのコピペして実行したところ
video input setting のに
Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value
と出てしまいました
該当のソースコード
import UIKit
import AVFoundation
class ViewController: UIViewController, AVCaptureFileOutputRecordingDelegate {
var videoDevice: AVCaptureDevice? let fileOutput = AVCaptureMovieFileOutput() var recordButton: UIButton! override func viewDidLoad() { super.viewDidLoad() self.view.backgroundColor = .black self.setUpCamera() } func setUpCamera() { let captureSession: AVCaptureSession = AVCaptureSession() self.videoDevice = self.defaultCamera() let audioDevice: AVCaptureDevice? = AVCaptureDevice.default(for: AVMediaType.audio) // video input setting let videoInput: AVCaptureDeviceInput = try! AVCaptureDeviceInput(device: videoDevice!) captureSession.addInput(videoInput) // audio input setting let audioInput = try! AVCaptureDeviceInput(device: audioDevice!) captureSession.addInput(audioInput) // max duration setting self.fileOutput.maxRecordedDuration = CMTimeMake(value: 60, timescale: 1) captureSession.addOutput(fileOutput) // video quality setting captureSession.beginConfiguration() if captureSession.canSetSessionPreset(.hd4K3840x2160) { captureSession.sessionPreset = .hd4K3840x2160 } else if captureSession.canSetSessionPreset(.high) { captureSession.sessionPreset = .high } captureSession.commitConfiguration() captureSession.startRunning() // video preview layer let videoLayer : AVCaptureVideoPreviewLayer = AVCaptureVideoPreviewLayer(session: captureSession) videoLayer.frame = self.view.bounds videoLayer.videoGravity = AVLayerVideoGravity.resizeAspectFill self.view.layer.addSublayer(videoLayer) // zooming slider let slider: UISlider = UISlider() let sliderWidth: CGFloat = self.view.bounds.width * 0.75 let sliderHeight: CGFloat = 40 let sliderRect: CGRect = CGRect(x: (self.view.bounds.width - sliderWidth) / 2, y: self.view.bounds.height - 200, width: sliderWidth, height: sliderHeight) slider.frame = sliderRect slider.minimumValue = 0.0 slider.maximumValue = 1.0 slider.value = 0.0 slider.addTarget(self, action: #selector(self.onSliderChanged(sender:)), for: .valueChanged) self.view.addSubview(slider) // recording button self.recordButton = UIButton(frame: CGRect(x: 0, y: 0, width: 120, height: 50)) self.recordButton.backgroundColor = UIColor.gray self.recordButton.layer.masksToBounds = true self.recordButton.setTitle("Record", for: .normal) self.recordButton.layer.cornerRadius = 20 self.recordButton.layer.position = CGPoint(x: self.view.bounds.width / 2, y:self.view.bounds.height - 100) self.recordButton.addTarget(self, action: #selector(self.onClickRecordButton(sender:)), for: .touchUpInside) self.view.addSubview(recordButton) } func defaultCamera() -> AVCaptureDevice? { if let device = AVCaptureDevice.default(.builtInDualCamera, for: AVMediaType.video, position: .front) { return device } else if let device = AVCaptureDevice.default(.builtInWideAngleCamera, for: AVMediaType.video, position: .back) { return device } else { return nil } } @objc func onClickRecordButton(sender: UIButton) { if self.fileOutput.isRecording { // stop recording fileOutput.stopRecording() self.recordButton.backgroundColor = .gray self.recordButton.setTitle("Record", for: .normal) } else { // start recording let tempDirectory: URL = URL(fileURLWithPath: NSTemporaryDirectory()) let fileURL: URL = tempDirectory.appendingPathComponent("mytemp1.mov") fileOutput.startRecording(to: fileURL, recordingDelegate: self) self.recordButton.backgroundColor = .red self.recordButton.setTitle("●Recording", for: .normal) } } @objc func onSliderChanged(sender: UISlider) { do { try self.videoDevice?.lockForConfiguration() self.videoDevice?.ramp( toVideoZoomFactor: (self.videoDevice?.minAvailableVideoZoomFactor)! + CGFloat(sender.value) * ((self.videoDevice?.maxAvailableVideoZoomFactor)! - (self.videoDevice?.minAvailableVideoZoomFactor)!), withRate: 30.0) self.videoDevice?.unlockForConfiguration() } catch { print("Failed to change zoom.") } } func fileOutput(_ output: AVCaptureFileOutput, didFinishRecordingTo outputFileURL: URL, from connections: [AVCaptureConnection], error: Error?) { // show alert let alert: UIAlertController = UIAlertController(title: "Recorded!", message: outputFileURL.absoluteString, preferredStyle: .alert) let okAction: UIAlertAction = UIAlertAction(title: "OK", style: UIAlertAction.Style.default, handler: nil) alert.addAction(okAction) self.present(alert, animated: true, completion: nil) }
}
補足情報(FW/ツールのバージョンなど)
あなたの回答
tips
プレビュー