ios - Cant Figure Out Why VideoDeviceInput Isn't Being Added To My AVCaptureSession -
everything working fine. outputs added correctly, initialized correctly, camera pops up, etc. thing not working adding video device input capture session or camera , cannot gather why happening.
maybe different set of eyes can catch going on here. appreciate if can take please?
import uikit import foundation import avfoundation class cameraviewcontroller: uiviewcontroller { @iboutlet var recordingimage: uiimageview! @iboutlet var capturebutton: uibutton! @iboutlet var flipcamerabutton: uibutton! @iboutlet var switchcapturebutton: uibutton! @iboutlet weak var cameraview: uiview! let cameracaptureposition: avcapturedeviceposition = avcapturedeviceposition.back let session: avcapturesession = avcapturesession() let totaltime: float64 = 1800 // total time in seconds let preferredtimescale: int32 = 45 // 45 fps let minfreespace: int64 = 65999999 // minimum free disk space var dataobject: anyobject? var cameracapturedevice: avcapturedevice? var audiocapturedevice: avcapturedevice? var moviefileoutput: avcapturemoviefileoutput? var stillimageoutput: avcapturestillimageoutput? var videopreviewlayer: avcapturevideopreviewlayer? var captureconnection: avcaptureconnection? var wearerecording: bool = false var beenherebefore: bool = false var devicesset: bool = false //**************** view did load ***************** override func viewdidload() { super.viewdidload() registerruntimeerror() registerdeviceconnections() setsessionpreset() } //**************** view did appear ***************** override func viewdidappear(animated: bool) { //------initialize camera------ if (!self.beenherebefore) { addinputs() addoutputs() setcameraoutputproperties() startsession() self.beenherebefore = true } else { println("been here before") } self.wearerecording = false } //**************** did receive memory warning ***************** override func didreceivememorywarning() { super.didreceivememorywarning() // dispose of resources can recreated. } //**************** takes photo or video ***************** @ibaction func didpresscapture(sender: anyobject) { } //**************** switches capture modes of camera **************** @ibaction func didpressswitchcapture(sender: anyobject) { // // ----------- todo ------------ // } //**************** flips camera , forth **************** @ibaction func didpressflipcamera(sender: anyobject) { // // ----------- todo ------------ // } //**************** initialize video capture session **************** func setsessionpreset() { println("initializing video capture session") //----- set image quality / resolution ----- //options: // avcapturesessionpresethigh - highest recording quality (varies per device) // avcapturesessionpresetmedium - suitable wifi sharing (actual values may change) // avcapturesessionpresetlow - suitable 3g sharing (actual values may change) // avcapturesessionpreset640x480 - 640x480 vga (check supported before setting it) // avcapturesessionpreset1280x720 - 1280x720 720p hd (check supported before setting it) // avcapturesessionpresetphoto - full photo resolution (not supported video output) if session.cansetsessionpreset(avcapturesessionpresethigh) { println("capture session preset set high quality") session.sessionpreset = avcapturesessionpresethigh } else { //------if failed, set default preset medium------ println("capture session preset set medium quality") session.sessionpreset = avcapturesessionpresetmedium } } //**************** add capture session inputs **************** func addinputs() { println("getting array of available capture devices") //------grab of devices------ let devices = avcapturedevice.devices() //------find camera matching position------ device in devices { if device.position == self.cameracaptureposition { self.cameracapturedevice = device as? avcapturedevice println("back camera has been added") } } var error1: nserror? = nil let videodeviceinput = avcapturedeviceinput(device: self.cameracapturedevice, error: &error1) //------print error if 1 occurs------ if error1 != nil { println("error1 ---: \(error1?.description)") } //------add video , audio input------ println("trying add video input") if self.cameracapturedevice != nil { if self.session.canaddinput(videodeviceinput) { self.session.addinput(videodeviceinput) println("successfully added video input") self.devicesset = true } else { // !!!!!!!!------- video input not being added session -----------!!!!!!!!!!! println("could not add video input") } } else { println("could not create video device") } } //**************** add capture session outputs **************** func addoutputs() { //------set jpeg output------ println("setting jpeg output") self.stillimageoutput = avcapturestillimageoutput() let outputsettings = [ avvideocodeckey : avvideocodecjpeg ] self.stillimageoutput!.outputsettings = outputsettings println("successfully configured jpeg ouput") //------set movie file ouput max duration------ println("setting movie file max duration") self.moviefileoutput = avcapturemoviefileoutput() let maxduration:cmtime = cmtimemakewithseconds(self.totaltime, self.preferredtimescale) self.moviefileoutput!.maxrecordedduration = maxduration println("successully set movie file max duration") println("setting movie file minimun byte space") self.moviefileoutput!.minfreediskspacelimit = self.minfreespace println("successfully added minium free space") //------add jpeg output , movie file output session output------ println("adding still image , movie file output") if self.session.canaddoutput(self.stillimageoutput) && self.session.canaddoutput(self.moviefileoutput) { self.session.addoutput(self.stillimageoutput) self.session.addoutput(self.moviefileoutput) println("successfully added still image , movie file outputs") } else { println("could not add still image , movie file output") } } //***************** set camera properties here **************** func setcameraoutputproperties () { //------init capture connection------ println("initializing capture connection") self.captureconnection = self.moviefileoutput!.connectionwithmediatype(avmediatypevideo) println("capture connection succesfully initialized") } //***************** register runtime notifications **************** func registerruntimeerror() { println("registering runtime errors") nsnotificationcenter.defaultcenter().addobserver(self, selector: selector("didhaveruntimeerror"), name: avcapturesessionruntimeerrornotification, object: self.session) } //**************** register device connection notifications ***************** func registerdeviceconnections() { println("registering connection notifications") nsnotificationcenter.defaultcenter().addobserver(self, selector: selector("deviceconnected"), name: avcapturedevicewasconnectednotification, object: self.session) nsnotificationcenter.defaultcenter().addobserver(self, selector: selector("devicedisconnected"), name: avcapturedevicewasdisconnectednotification, object: self.session) } //**************** start capture session **************** func startsession() { println("about add session inputs...") addinputs() println("start configuring capture") //------config capture session------ if !session.running { println("displaying camera in ui") //------display camera in ui------ self.videopreviewlayer = avcapturevideopreviewlayer(session: self.session) println("video preview layer set") self.cameraview.layer.addsublayer(self.videopreviewlayer) println("video preview layer added sublayer") self.videopreviewlayer!.frame = self.cameraview.layer.frame println("video preview frame set") self.videopreviewlayer!.videogravity = avlayervideogravityresizeaspectfill println("camera successully can display") //------start camera------ self.session.startrunning() println("capture session initiated") } else { println("session running, no need start again") } } /* // mark: - navigation // in storyboard-based application, want little preparation before navigation override func prepareforsegue(segue: uistoryboardsegue, sender: anyobject?) { // new view controller using segue.destinationviewcontroller. // pass selected object new view controller. } */ }
i think missing
avcapturefileoutputrecordingdelegate
did errors print? :
println("could not add video input") println("could not create video device")
Comments
Post a Comment