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

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

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

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

Q&A

解決済

1回答

666閲覧

カメラを起動中にタップで写真撮影、長押しで動画撮影をしたい

Yeezy21

総合スコア21

Swift

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

0グッド

0クリップ

投稿2020/07/18 12:53

前提・実現したいこと

AVCaptureMovieFileOutputを使って動画撮影ができるカメラを作ってて、インスタグラムのストーリーのカメラのようにタップで写真撮影、長押しで動画撮影ができるカメラを作りたいです。
AVCapturePhotoOutputを使い写真を撮影できるカメラは作ったことがあるのですが、同時に撮影できないので困っています。
解決策を知ってる方教えてください。

動画撮影のカメラを作る際に参考にした記事

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

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

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

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

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

guest

回答1

0

ベストアンサー

ちょっと興味があったので試してみました。

上記の記事をみると、プレビューについては参考にされた Qiita の記事

と共通のようです。

あとは、撮影時に「ビデオにする」か「写真にする」かで処理を変えれば良さそうに思えました。

ということで、双方の記事を参考に、共通のプレビューで動画撮影と静止画撮影を個別にできるようなコードを書いてみました。

今回は個別にボタンを用意していますが、実際は通常のタップろロングタップで処理を切り替えれば良さそうです。

本来であれば、ビデオ撮影時は静止画を撮影できない(ボタンを選択できないようにする)などの処理が必要ですが、その辺りは無視していますのでご注意ください。

また、写真については露出などの設定をしていないため、暗くなってしまうようですが、そのあたりは適切に手を加えていただければと思います。

あと、Swift5 で書いていますので、Qiita の元記事(Swift4)とは一部メソッドなどが異なっている点にもご注意ください。

Swift

1// https://medium.com/@shiba1014/avfoundation%E3%82%92%E7%94%A8%E3%81%84%E3%81%9F%E3%82%AB%E3%83%A1%E3%83%A9%E3%81%AE%E5%AE%9F%E8%A3%85-a8f462392265 2 3import UIKit 4import AVFoundation 5import Photos 6 7// MARK: AVCapturePhotoCaptureDelegate を追加 8class RecordViewController: UIViewController, AVCaptureFileOutputRecordingDelegate, AVCapturePhotoCaptureDelegate { 9 let fileOutputForVideo = AVCaptureMovieFileOutput() 10 11 var recordButton: UIButton! 12 // 13 var stillButton: UIButton! 14 15 var isRecording = false 16 17 // MARK: 移動 - トップレベルで宣言 18 let captureSession = AVCaptureSession() 19 20 override func viewDidLoad() { 21 super.viewDidLoad() 22 23 setUpPreview() 24 } 25 26 func setUpPreview() { 27 let videoDevice = AVCaptureDevice.default(for: AVMediaType.video) 28 let audioDevice = AVCaptureDevice.default(for: AVMediaType.audio) 29 30 do { 31 if videoDevice == nil || audioDevice == nil { 32 throw NSError(domain: "device error", code: -1, userInfo: nil) 33 } 34 35 // MARK: 移動 - ほかの関数から参照するので、トップレベルに移動 36 //let captureSession = AVCaptureSession() 37 38 // video inputを capture sessionに追加 39 let videoInput = try AVCaptureDeviceInput(device: videoDevice!) 40 captureSession.addInput(videoInput) 41 42 // audio inputを capture sessionに追加 43 let audioInput = try AVCaptureDeviceInput(device: audioDevice!) 44 captureSession.addInput(audioInput) 45 46 // MARK: 撮影直前に設定する 47 // max 30sec 48 //fileOutputForVideo.maxRecordedDuration = CMTimeMake(value: 30, timescale: 1) 49 //captureSession.addOutput(fileOutputForVideo) 50 51 // プレビュー 52 let videoLayer : AVCaptureVideoPreviewLayer = AVCaptureVideoPreviewLayer(session: captureSession) 53 videoLayer.frame = self.view.bounds 54 videoLayer.videoGravity = AVLayerVideoGravity.resizeAspectFill 55 self.view.layer.addSublayer(videoLayer) 56 57 captureSession.startRunning() 58 59 setUpButtonForVideo() 60 // MARK: 写真用のボタン 61 setUpButtonForPhoto() 62 } catch { 63 // エラー処理 64 } 65 } 66 67 func setUpButtonForVideo() { 68 recordButton = UIButton(frame: CGRect(x: 0,y: 0,width: 120,height: 50)) 69 recordButton.backgroundColor = UIColor.gray 70 recordButton.layer.masksToBounds = true 71 recordButton.setTitle("録画開始", for: .normal) 72 recordButton.layer.cornerRadius = 20.0 73 recordButton.layer.position = CGPoint(x: self.view.bounds.width/2, y:self.view.bounds.height-50) 74 recordButton.addTarget(self, action: #selector(onClickRecordButtonForVideo(sender:)), for: .touchUpInside) 75 76 self.view.addSubview(recordButton) 77 } 78 79 // MARK: 写真撮影用のボタン 80 func setUpButtonForPhoto() { 81 stillButton = UIButton(frame: CGRect(x: 0,y: 0,width: 120,height: 50)) 82 stillButton.backgroundColor = UIColor.gray 83 stillButton.layer.masksToBounds = true 84 stillButton.setTitle("撮影", for: .normal) 85 stillButton.layer.cornerRadius = 20.0 86 stillButton.layer.position = CGPoint(x: self.view.bounds.width/2, y:self.view.bounds.height-124) 87 stillButton.addTarget(self, action: #selector(onClickRecordButtonForPhoto(sender:)), for: .touchUpInside) 88 89 self.view.addSubview(stillButton) 90 } 91 92 @objc func onClickRecordButtonForVideo(sender: UIButton) { 93 if !isRecording { 94 // MARK: 移動 - ビデオ撮影前に出力を追加 95 fileOutputForVideo.maxRecordedDuration = CMTimeMake(value: 30, timescale: 1) 96 captureSession.addOutput(fileOutputForVideo) 97 98 // 録画開始 99 let paths = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true) 100 let documentsDirectory = paths[0] as String 101 let filePath : String? = "(documentsDirectory)/temp.mp4" 102 let fileURL : NSURL = NSURL(fileURLWithPath: filePath!) 103 fileOutputForVideo.startRecording(to: fileURL as URL, recordingDelegate: self) 104 105 isRecording = true 106 changeButtonColor(target: recordButton, color: UIColor.red) 107 recordButton.setTitle("録画中", for: .normal) 108 } else { 109 // 録画終了 110 fileOutputForVideo.stopRecording() 111 112 isRecording = false 113 changeButtonColor(target: recordButton, color: UIColor.gray) 114 recordButton.setTitle("録画開始", for: .normal) 115 116 // MARK: 追加 - 撮影後にビデオ出力を削除 117 // 手持ちの iPhone だと、出力は1つしか追加できないので、使用後は削除する 118 captureSession.removeOutput(fileOutputForVideo) 119 } 120 } 121 122 // MARK: 追加 - 撮影の処理 123 @objc func onClickRecordButtonForPhoto(sender: UIButton) { 124 // 必要であれば露出などのセッティングを行う 125 let settings = AVCapturePhotoSettings() 126 let fileOutputForPhoto = AVCapturePhotoOutput() 127 128 // 撮影前にデバイスを追加し、撮影。終了後はデバイスを削除 129 captureSession.addOutput(fileOutputForPhoto) 130 fileOutputForPhoto.capturePhoto(with: settings, delegate: self) 131 captureSession.removeOutput(fileOutputForPhoto) 132 } 133 134 func changeButtonColor(target: UIButton, color: UIColor) { 135 target.backgroundColor = color 136 } 137 138 func fileOutput(_ output: AVCaptureFileOutput, didFinishRecordingTo outputFileURL: URL, from connections: [AVCaptureConnection], error: Error?) { 139 // ライブラリへ保存 140 PHPhotoLibrary.shared().performChanges({ 141 PHAssetChangeRequest.creationRequestForAssetFromVideo(atFileURL: outputFileURL) 142 }) { completed, error in 143 if completed { 144 print("Video is saved!") 145 } 146 } 147 } 148 149 // MARK: 静止画の保存 150 func photoOutput(_ output: AVCapturePhotoOutput, didFinishProcessingPhoto photo: AVCapturePhoto, error: Error?) { 151 guard let photoData = photo.fileDataRepresentation(), 152 let image = UIImage(data: photoData) else { return } 153 154 PHPhotoLibrary.shared().performChanges({ 155 PHAssetChangeRequest.creationRequestForAsset(from: image) 156 }) { completed, error in 157 if completed { 158 print("Photo is saved!") 159 } 160 } 161 } 162}

以前の回答

1つのボタンに対し、通常のタップ時の処理と、ロングタップ時の処理両方をつけ、それぞれの動作に応じて写真撮影、あるいは動画撮影と分ければ可能なように思えます。

通常のボタンの動作は StoryBoard、ロングタップはコードで追加するなら下記のような感じでしょうか。

Swift

1import UIKit 2 3class ViewController: UIViewController { 4 @IBOutlet weak var button: UIButton! 5 6 override func viewDidLoad() { 7 super.viewDidLoad() 8 // Do any additional setup after loading the view. 9 let longGesture = UILongPressGestureRecognizer(target: self, action: #selector(longTap)) 10 button.addGestureRecognizer(longGesture) 11 } 12 13 @IBAction func normalTouch(_ sender: Any) { 14 print("通常のタッチ") 15 } 16 17 @objc func longTap(_ sender: UIGestureRecognizer){ 18 if sender.state == .began { 19 print("ロングタッチ開始") 20 } 21 } 22}

あるいは、Long Press Gresture Recognizer も StoryBoard で追加し、アウトレットを引っ張ってくる方法もあるかと思います。

Swift

1 @IBAction func longTouch(_ sender: UILongPressGestureRecognizer) { 2 if sender.state == .began { 3 print("ロングタッチ開始") 4 } 5 }

投稿2020/07/18 16:18

編集2020/07/19 06:44
TsukubaDepot

総合スコア5086

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

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

Yeezy21

2020/07/19 04:05

回答ありがとうございます! タッチとロングタッチのアクション切り分けはできるのですが、一つのViewControllerでAVCaptureMovieFileOutputとAVCapturePhotoOutputを使うことはできるのでしょうか? AVFoundation周りが難しくてハマっています。
TsukubaDepot

2020/07/19 06:45

そういうことなんですね。 理解しました。 本文に追記しましたが、プレビューは共通にして、あとは出力時に出力先を変えれば良さそうな感じです。
Yeezy21

2020/07/19 12:26

本当にありがとうございます!! まさに求めていた回答です! こちら試させていただきますね!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問