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

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

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

iOS 9は、アップル社のモバイルOSであるiOSシリーズのバージョン。特徴として検索機能の強化、Siriの機能改良、iPad向けマルチタスクなどがあります。マルチウィンドウ機能をサポートし、iPad向けマルチタスクもサポートされています。

Swift

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

Q&A

1回答

2200閲覧

Swiftでの動画撮影アプリ作成時のエラー

codack1969

総合スコア7

iOS 9

iOS 9は、アップル社のモバイルOSであるiOSシリーズのバージョン。特徴として検索機能の強化、Siriの機能改良、iPad向けマルチタスクなどがあります。マルチウィンドウ機能をサポートし、iPad向けマルチタスクもサポートされています。

Swift

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

0グッド

0クリップ

投稿2016/02/13 18:24

iOSアプリ開発を初めて1ヶ月の初心者です。
環境はXcode7.2
使用言語はSwift

こちらのサイトを元にして、最新のコードに書き換えつつ動画撮影アプリを作成していますがエラーが解消できません。
参考サイト:https://sites.google.com/a/gclue.jp/swift-docs/ni-yinki100-ios/3-avfoundation/003-dong-huano-cuo-ying
リンク内容

下記に示すコードを記述し、ビルドしたところデバッグエリアに次のエラーが表示されます。

2016-02-14 01:50:53.095 sampleVideo2[4602:3119552] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[AVCaptureDevice init:] - cannot instantiate a AVCaptureDevice directly.'

*** First throw call stack:
(0x1824cd900 0x181b3bf80 0x18874a36c 0x1000d1994 0x1000d108c 0x1000cd9c4 0x1000cfc6c 0x1871c00c0 0x1871d84a8 0x187371ce4 0x18727e9b8 0x18727e694 0x18727e5fc 0x1871bb778 0x184bcab2c 0x184bc5738 0x184bc55f8 0x184bc4c94 0x184bc49dc 0x1871be944 0x182484efc 0x182484990 0x182482690 0x1823b1680 0x1838c0088 0x187228d90 0x1000cd6f0 0x181f528b8)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)

さらにAppDelegateのクラス名の行(以下)にエラーメッセージ「signal SIGARBT」が表示されます。

class AppDelegate: UIResponder, UIApplicationDelegate {

自力で調べても解決できず苦戦しておりますのでアドバイスいただけますと幸いです。
よろしくお願いします。

swift

1import UIKit 2import AVFoundation 3 4class ViewController: UIViewController, AVCaptureFileOutputRecordingDelegate { 5 6 //ビデオのアウトプット 7 private var myVideoOutPut:AVCaptureMovieFileOutput! 8 9 //スタート&ストップボタン 10 private var myButtonStart : UIButton! 11 private var myButtonStop : UIButton! 12 13 override func viewDidLoad() { 14 super.viewDidLoad() 15 16 //セッションの作成 17 let mySession : AVCaptureSession = AVCaptureSession() 18 19 //デバイス 20 var myDevice : AVCaptureDevice = AVCaptureDevice() 21 22 //出力先の設定 23 let myImageOutput : AVCaptureStillImageOutput = AVCaptureStillImageOutput() 24 25 26 //デバイス一覧の取得 27 let devices = AVCaptureDevice.devices() 28 29 //マイクを取得 30 //マイクをセッションのInputに追加 31 do{ 32 let audioCaptureDevice = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeAudio) 33 let audioInput = try AVCaptureDeviceInput(device: audioCaptureDevice) 34 if (mySession.canAddInput(audioInput)){ 35 mySession.addInput(audioInput) 36 } 37 }catch let error as NSError{ 38 print(error) 39 } 40 41 //バックライトをmyDeviceに格納 42 for device in devices { 43 if (device.position == AVCaptureDevicePosition.Back){ 44 myDevice = device as! AVCaptureDevice 45 } 46 } 47 48 //バックカメラを取得 49 do{ 50 let videoCaptureDevice = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeVideo) 51 let videoInput = try AVCaptureDeviceInput(device: videoCaptureDevice) 52 //ビデオをセッションのInputに追加 53 if (mySession.canAddInput(videoInput)){ 54 mySession.addInput(videoInput) 55 } 56 }catch let error as NSError{ 57 print(error) 58 } 59 60 61 //セッションに出力先を追加 62 mySession.addOutput(myImageOutput) 63 64 //動画の保存 65 myVideoOutPut = AVCaptureMovieFileOutput() 66 67 //ビデオ出力をOutputに追加 68 mySession.addOutput(myVideoOutPut) 69 70 //画像を表示するレイヤーを生成 71 let myVideoLayer:AVCaptureVideoPreviewLayer = AVCaptureVideoPreviewLayer(session: mySession) 72 myVideoLayer.frame = self.view.bounds 73 myVideoLayer.videoGravity = AVLayerVideoGravityResizeAspectFill 74 75 //viewに追加 76 self.view.layer.addSublayer(myVideoLayer) 77 78 //セッション開始 79 mySession.startRunning() 80 81 // UIボタン作成 82 myButtonStart = UIButton(frame: CGRectMake(0,0,120,50)) 83 myButtonStop = UIButton(frame: CGRectMake(0,0,120,50)) 84 85 myButtonStart.backgroundColor = UIColor.redColor() 86 myButtonStop.backgroundColor = UIColor.grayColor() 87 88 myButtonStart.layer.masksToBounds = true 89 myButtonStop.layer.masksToBounds = true 90 91 myButtonStart.setTitle("撮影", forState: .Normal) 92 myButtonStop.setTitle("停止", forState: .Normal) 93 94 myButtonStart.layer.cornerRadius = 20 95 myButtonStop.layer.cornerRadius = 20 96 97 98 myButtonStart.layer.position = CGPoint(x: self.view.bounds.width/2 - 70, y: self.view.bounds.height-50) 99 myButtonStop.layer.position = CGPoint(x: self.view.bounds.width/2 - 70, y: self.view.bounds.height-50) 100 101 myButtonStart.addTarget(self, action: "onClickMyButton:", forControlEvents: .TouchUpInside) 102 myButtonStop.addTarget(self, action: "onClickMyButton:", forControlEvents: .TouchUpInside) 103 104 //UIボタンをViewに追加 105 self.view.addSubview(myButtonStart) 106 self.view.addSubview(myButtonStop) 107 } 108 109 //ボタンイベント 110 internal func onClickMyButton(sender:UIButton){ 111 //撮影開始 112 if sender == myButtonStart { 113 let paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true) 114 115 //フォルダ 116 let documentsDirectory = paths[0] as! String 117 118 //ファイル名 119 let filePath:String? = "\(documentsDirectory)/test.mp4" 120 121 //URL 122 let fileURL:NSURL = NSURL(fileURLWithPath: filePath!) 123 124 //録画開始 125 myVideoOutPut.startRecordingToOutputFileURL(fileURL, recordingDelegate: self) 126 127 } else if sender == myButtonStop { 128 myVideoOutPut.stopRecording() 129 130 } 131 } 132 133 //動画がキャプチャーされた後に呼ばれるメソッド 134 func captureOutput(captureOutput: AVCaptureFileOutput!, didFinishRecordingToOutputFileAtURL outputFileURL: NSURL!, fromConnections connections: [AnyObject]!, error: NSError!) { 135 print("didFinishRecordingToOutputFileAtURL") 136 } 137 138 //動画のキャプチャーが開始された時に呼ばれるメソッド 139 func captureOutput(captureOutput: AVCaptureFileOutput!, didStartRecordingToOutputFileAtURL fileURL: NSURL!, fromConnections connections: [AnyObject]!) { 140 print("didStartRecordingToOutputFileAtURL") 141 } 142 143 override func didReceiveMemoryWarning() { 144 super.didReceiveMemoryWarning() 145 // Dispose of any resources that can be recreated. 146 } 147 148 149 /* 150 // MARK: - Navigation 151 152 // In a storyboard-based application, you will often want to do a little preparation before navigation 153 override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { 154 // Get the new view controller using segue.destinationViewController. 155 // Pass the selected object to the new view controller. 156 } 157 */ 158 159} 160

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

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

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

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

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

guest

回答1

0

背面カメラを使用するコードになっていますが、実行しているiPhone/iPod touchには背面カメラはついていますでしょうか。

投稿2016/02/13 23:54

sekitaka_1214

総合スコア509

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

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

codack1969

2016/02/14 03:40

背面カメラついています。iPhone6、iOS9.2.1で実機テストしています。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

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

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

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問