質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

Q&A

1回答

1011閲覧

【Swift】深度マップを使ったカメラでなぜか audio の Input が無いと動かない・・・

Hayato1201

総合スコア220

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

0グッド

0クリップ

投稿2021/03/02 14:40

編集2021/03/02 15:02

以下の様な深度マップを使ったカメラを使いたいです。

Swift

1class ViewController: UIViewController { 2 3 var captureSession = AVCaptureSession() 4 private var photoOutput: AVCapturePhotoOutput? 5 private var currentDevice : AVCaptureDevice? 6 private var videoDeviceInput : AVCaptureDeviceInput! 7 private let videoDeviceDiscoverySession = AVCaptureDevice.DiscoverySession(deviceTypes: [.builtInWideAngleCamera, .builtInDualCamera, .builtInTrueDepthCamera], 8 mediaType: .video, position: .unspecified) 9 private var cameraPreviewLayer : AVCaptureVideoPreviewLayer? 10 var previewView : UIView! 11 12 override func viewDidLoad() { 13 super.viewDidLoad() 14 captureSession.sessionPreset = AVCaptureSession.Preset.photo 15 setupCaptureSession(withPosition: .front) 16 previewView = UIView(frame: CGRect(x: 0, y: 44, width: self.view.frame.width, height: self.view.frame.height - 188)) 17 setupPreviewLayer(previewView) 18 self.view.addSubview(previewView) 19 } 20 21 22 func setupCaptureSession(withPosition cameraPosition: AVCaptureDevice.Position) { 23 24 self.captureSession = AVCaptureSession() 25 photoOutput = AVCapturePhotoOutput() 26 guard let photoOutput = photoOutput else { return } 27 28 do { 29 captureSession.beginConfiguration() 30 captureSession.sessionPreset = .photo 31 let devices = self.videoDeviceDiscoverySession.devices 32 for device in devices { 33 if device.position == cameraPosition { 34 currentDevice = device 35 } 36 } 37 38 guard let videoDevice = currentDevice else { return } 39 videoDeviceInput = try AVCaptureDeviceInput(device: videoDevice) 40 41 if captureSession.canAddInput(videoDeviceInput) { 42 captureSession.addInput(videoDeviceInput) 43 } 44 45 46 } catch { 47 print(error) 48 } 49 50// let audioDevice = AVCaptureDevice.default(for: AVMediaType.audio) 51// let audioInput = try! AVCaptureDeviceInput(device: audioDevice!) 52// self.captureSession.addInput(audioInput) 53 54 if captureSession.canAddOutput(photoOutput) { 55 captureSession.addOutput(photoOutput) 56 photoOutput.isHighResolutionCaptureEnabled = true 57 photoOutput.isLivePhotoCaptureEnabled = photoOutput.isLivePhotoCaptureSupported 58 //DepthDataDeliveryEnabled を ture に 59 photoOutput.isDepthDataDeliveryEnabled = photoOutput.isDepthDataDeliverySupported 60 captureSession.commitConfiguration() 61 } 62 63 self.captureSession.startRunning() 64 } 65 66 private func setupPreviewLayer(_ view: UIView) { 67 self.cameraPreviewLayer = AVCaptureVideoPreviewLayer(session: captureSession) 68 self.cameraPreviewLayer?.videoGravity = AVLayerVideoGravity.resizeAspectFill 69 self.cameraPreviewLayer?.connection?.videoOrientation = AVCaptureVideoOrientation.portrait 70 self.cameraPreviewLayer?.frame = view.frame 71 view.layer.insertSublayer(self.cameraPreviewLayer ?? AVCaptureVideoPreviewLayer(), at: 0) 72 } 73}

しかしこれだとなぜかpreviewが表示されません。
しかしなぜか上記コメントアウトを外してaudioInputを追加するとpreviewが表示できます。
因みにこれはおそらくphotoOutput.isDepthDataDeliveryEnabledのせいで起きていてphotoOutput.isDepthDataDeliveryEnabledの部分をコメントアウトすると表示されます。
また、これはfrontのカメラだけの現象の様でsetupCaptureSessionの引数に.backを渡すと表示できます。
これはなぜでしょうか?audioInputを追加しなくてもphotoOutput.isDepthDataDeliveryEnabledをtrueにして使用したいのですが可能でしょうか??


追記
動作をさらに確認したところ、isDepthDataDeliveryEnabledの方ではなくisLivePhotoCaptureEnabledが原因かもしれません・・・
isLivePhotoCaptureEnabledの方をコメントアウトした場合も動きます。isLivePhotoCaptureEnabledの方は確かに音声情報も必要とするらしいです。
しかしではなぜisDepthDataDeliveryEnabledの方のみをコメントアウトした場合も動いたのかよくわかりません。。。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

videoDeviceDiscoverySession に誤りがあります。
現在販売されている iPhoneやiPadでは、フロントカメラでdualCameraに対応するデバイスは存在しません。
すなわち下記の定義ではカメラデバイスを得ることができません。
.builtInWideAngleCamera
.builtInDualCamera
.builtInTrueDepthCamera

提示されたコードではカメラデバイスがない場合はカメラを追加しないようにguard処理を入れているので、プレビューが表示されないのは必然です。

投稿2021/03/02 23:36

errolizer

総合スコア441

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問